-
Notifications
You must be signed in to change notification settings - Fork 18
/
url_lock.go
50 lines (42 loc) · 1.27 KB
/
url_lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package httpcache
import (
"hash/crc32"
"math"
"sync"
)
// URLLock is a lock to control the incoming request in a request-response cycle
type URLLock struct {
globalLocks []*sync.Mutex
keys []map[string]*sync.Mutex
urlLockBucketsSize int
}
// NewURLLock new a request lock
func NewURLLock(config *Config) *URLLock {
globalLocks := make([]*sync.Mutex, config.CacheBucketsNum)
keys := make([]map[string]*sync.Mutex, config.CacheBucketsNum)
for i := 0; i < config.CacheBucketsNum; i++ {
globalLocks[i] = new(sync.Mutex)
keys[i] = make(map[string]*sync.Mutex)
}
return &URLLock{
globalLocks: globalLocks,
keys: keys,
urlLockBucketsSize: config.CacheBucketsNum,
}
}
// Acquire a lock for given key
func (allLocks *URLLock) Acquire(key string) *sync.Mutex {
bucketIndex := allLocks.getBucketIndexForKey(key)
allLocks.globalLocks[bucketIndex].Lock()
defer allLocks.globalLocks[bucketIndex].Unlock()
lock, exists := allLocks.keys[bucketIndex][key]
if !exists {
lock = new(sync.Mutex)
allLocks.keys[bucketIndex][key] = lock
}
lock.Lock()
return lock
}
func (allLocks *URLLock) getBucketIndexForKey(key string) uint32 {
return uint32(math.Mod(float64(crc32.ChecksumIEEE([]byte(key))), float64(allLocks.urlLockBucketsSize)))
}