cloudflare/pint

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
v0.18.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

internal/promapi/keylock.go

32lines · 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) { _, ok = p.s[id]; return }
17
18func (p *partitionLocker) lock(id string) {
19 p.l.Lock()
20 defer p.l.Unlock()
21 for p.locked(id) {
22 p.c.Wait()
23 }
24 p.s[id] = struct{}{}
25}
26
27func (p *partitionLocker) unlock(id string) {
28 p.l.Lock()
29 defer p.l.Unlock()
30 delete(p.s, id)
31 p.c.Broadcast()
32}
33