-
Notifications
You must be signed in to change notification settings - Fork 0
/
gls.go
45 lines (38 loc) · 1.12 KB
/
gls.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
// +build go1.9
package gls
import (
"github.com/mgkanani/gls/goroutines"
"runtime"
"sync"
)
const factor = 10
var (
shards = runtime.GOMAXPROCS(-1) * 2 // will be executed at load time.
// most of the time, number of core are 2^x, but can be different due to virtualization/containerisation
// bitwise AND can be used when shards is 2^x.
// division = shards - 1
glsArr = make([]sync.Map, shards)
)
// Set accepts a value. key will be the current go-routine.
func Set(value interface{}) {
curRtn := goroutines.CurRoutine()
idx := int(uintptr(curRtn)>>factor) % shards
glsArr[idx].Store(curRtn, value)
}
// Get returns a value present in map for calling go-routine
func Get() interface{} {
curRtn := goroutines.CurRoutine()
idx := int(uintptr(curRtn)>>factor) % shards
val, ok := glsArr[idx].Load(curRtn)
if !ok {
return nil
}
return val
}
// Del deletes the value and key from map. Try to avoid this unless service is shutting down or pausing
// for pretty long. You can use Set(nil) instead.
func Del() {
curRtn := goroutines.CurRoutine()
idx := int(uintptr(curRtn)>>factor) % shards
glsArr[idx].Delete(curRtn)
}