Skip to content

Commit bef792b

Browse files
committed
feat: use rwmutex instead
Signed-off-by: haoyun <yun.hao@daocloud.io>
1 parent 7020719 commit bef792b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

pkg/timeout/timeout.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import (
2323
)
2424

2525
var (
26-
mu sync.Mutex
26+
mu sync.RWMutex
2727
timeouts = make(map[string]time.Duration)
2828

2929
// DefaultTimeout of the timeout package
@@ -39,9 +39,9 @@ func Set(key string, t time.Duration) {
3939

4040
// Get returns the timeout for the provided key
4141
func Get(key string) time.Duration {
42-
mu.Lock()
42+
mu.RLock()
4343
t, ok := timeouts[key]
44-
mu.Unlock()
44+
mu.RUnlock()
4545
if !ok {
4646
t = DefaultTimeout
4747
}
@@ -57,8 +57,8 @@ func WithContext(ctx context.Context, key string) (context.Context, func()) {
5757
// All returns all keys and their timeouts
5858
func All() map[string]time.Duration {
5959
out := make(map[string]time.Duration)
60-
mu.Lock()
61-
defer mu.Unlock()
60+
mu.RLock()
61+
defer mu.RUnlock()
6262
for k, v := range timeouts {
6363
out[k] = v
6464
}

0 commit comments

Comments
 (0)