-
Notifications
You must be signed in to change notification settings - Fork 0
/
sign_manager.go
52 lines (43 loc) · 987 Bytes
/
sign_manager.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
51
52
package signer
import (
"encoding/binary"
"sync"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/open-bytestack/levelstore/gopkg/goproto"
)
type BlobSignManager struct {
c *lru.Cache[blobKey, *BlobSign]
mu sync.RWMutex
}
func NewBlobSignManager() *BlobSignManager {
c, err := lru.New[blobKey, *BlobSign](1000)
if err != nil {
panic(err)
}
return &BlobSignManager{
c: c,
}
}
type blobKey [8 + 4]byte // uint64 + uint32
func byteBlobKey(in *goproto.BlobKey) (res blobKey) {
binary.BigEndian.PutUint64(res[:8], in.VolumeId)
binary.BigEndian.PutUint32(res[8:], in.Seq)
return
}
func (bsm *BlobSignManager) Get(in *goproto.BlobKey) (bs *BlobSign, ok bool) {
if in == nil {
return nil, false
}
bsm.mu.RLock()
defer bsm.mu.RUnlock()
v, ok := bsm.c.Get(byteBlobKey(in))
return v, ok
}
func (bsm *BlobSignManager) Add(in *goproto.BlobKey, bs *BlobSign) {
if in == nil {
return
}
bsm.mu.Lock()
defer bsm.mu.Unlock()
bsm.c.Add(byteBlobKey(in), bs)
}