cloudflare/pint

Public

mirrored from https://github.com/cloudflare/pintAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.74.5

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

internal/promapi/keylock.go

35lines · modecode

1package promapi
2
3import "sync"
4
5// https://medium.com/@petrlozhkin/kmutex-lock-mutex-by-unique-id-408467659c24
6type partitionLocker struct {
7 c *sync.Cond
8 l sync.Locker
9 s map[string]struct{}
10}
11
12func newPartitionLocker(l sync.Locker) *partitionLocker {
13 return &partitionLocker{c: sync.NewCond(l), l: l, s: make(map[string]struct{})}
14}
15
16func (p *partitionLocker) locked(id string) (ok bool) {
17 _, ok = p.s[id]
18 return ok
19}
20
21func (p *partitionLocker) lock(id string) {
22 p.l.Lock()
23 defer p.l.Unlock()
24 for p.locked(id) {
25 p.c.Wait()
26 }
27 p.s[id] = struct{}{}
28}
29
30func (p *partitionLocker) unlock(id string) {
31 p.l.Lock()
32 defer p.l.Unlock()
33 delete(p.s, id)
34 p.c.Broadcast()
35}
36