Merge pull request #6253 from jonyhy96/feat-rwmutex

feat: use rwmutex instead
This commit is contained in:
Phil Estes 2021-11-16 11:39:29 -05:00 committed by GitHub
commit 7758cdc09a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,7 +23,7 @@ import (
)
var (
mu sync.Mutex
mu sync.RWMutex
timeouts = make(map[string]time.Duration)
// DefaultTimeout of the timeout package
@ -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
}
@ -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
}