feat: use rwmutex instead

Signed-off-by: haoyun <yun.hao@daocloud.io>
This commit is contained in:
haoyun 2021-11-16 11:06:40 +08:00
parent 7020719646
commit bef792b962

View File

@ -23,7 +23,7 @@ import (
) )
var ( var (
mu sync.Mutex mu sync.RWMutex
timeouts = make(map[string]time.Duration) timeouts = make(map[string]time.Duration)
// DefaultTimeout of the timeout package // DefaultTimeout of the timeout package
@ -39,9 +39,9 @@ func Set(key string, t time.Duration) {
// Get returns the timeout for the provided key // Get returns the timeout for the provided key
func Get(key string) time.Duration { func Get(key string) time.Duration {
mu.Lock() mu.RLock()
t, ok := timeouts[key] t, ok := timeouts[key]
mu.Unlock() mu.RUnlock()
if !ok { if !ok {
t = DefaultTimeout t = DefaultTimeout
} }
@ -57,8 +57,8 @@ func WithContext(ctx context.Context, key string) (context.Context, func()) {
// All returns all keys and their timeouts // All returns all keys and their timeouts
func All() map[string]time.Duration { func All() map[string]time.Duration {
out := make(map[string]time.Duration) out := make(map[string]time.Duration)
mu.Lock() mu.RLock()
defer mu.Unlock() defer mu.RUnlock()
for k, v := range timeouts { for k, v := range timeouts {
out[k] = v out[k] = v
} }