Skip to content

Commit

Permalink
feat: use rwmutex instead
Browse files Browse the repository at this point in the history
Signed-off-by: haoyun <yun.hao@daocloud.io>
  • Loading branch information
jonyhy96 committed Nov 16, 2021
1 parent 7020719 commit bef792b
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pkg/timeout/timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

var (
mu sync.Mutex
mu sync.RWMutex
timeouts = make(map[string]time.Duration)

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

// Get returns the timeout for the provided key
func Get(key string) time.Duration {
mu.Lock()
mu.RLock()
t, ok := timeouts[key]
mu.Unlock()
mu.RUnlock()
if !ok {
t = DefaultTimeout
}
Expand All @@ -57,8 +57,8 @@ func WithContext(ctx context.Context, key string) (context.Context, func()) {
// All returns all keys and their timeouts
func All() map[string]time.Duration {
out := make(map[string]time.Duration)
mu.Lock()
defer mu.Unlock()
mu.RLock()
defer mu.RUnlock()
for k, v := range timeouts {
out[k] = v
}
Expand Down

0 comments on commit bef792b

Please sign in to comment.