forked from Vivino/rankdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbucket.go
37 lines (31 loc) · 822 Bytes
/
bucket.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
package rankdb
// Copyright 2019 Vivino. All rights reserved
//
// See LICENSE file for license details
// A Bucket contains a number of tokens.
// A bucket can safely be concurrently accessed.
// A user is free to access the channel directly.
type Bucket chan struct{}
// NewBucket creates a bucket with a given number of tokens.
func NewBucket(tokens int) Bucket {
b := make(chan struct{}, tokens)
for i := 0; i < tokens; i++ {
b <- struct{}{}
}
return Bucket(b)
}
// A Token from a bucket.
// Can be put buck using the Release function.
type Token struct {
b Bucket
}
// Get will return a token.
// The order in which tokens are handed out is random.
func (b Bucket) Get() Token {
<-b
return Token{b: b}
}
// Release a token and put it back in the bucket.
func (t Token) Release() {
t.b <- struct{}{}
}