-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator_pool.go
67 lines (52 loc) · 1.1 KB
/
validator_pool.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package validator
import (
"sync"
)
type ValidatorPools struct {
pools map[string]*ValidatorPool
sync.RWMutex
}
func NewValidationPools() *ValidatorPools {
return &ValidatorPools{
pools: make(map[string]*ValidatorPool),
}
}
func (p *ValidatorPools) SetPool(address string, pool *ValidatorPool) {
p.Lock()
defer p.Unlock()
p.pools[address] = pool
}
func (p *ValidatorPools) GetPool(address string) (*ValidatorPool, bool) {
p.Lock()
defer p.Unlock()
pool, ok := p.pools[address]
return pool, ok
}
type ValidatorPool struct {
validators chan Validator
size int
length int
sync.RWMutex
}
func NewValidationPool(size int) *ValidatorPool {
return &ValidatorPool{
validators: make(chan Validator, size),
size: size,
length: 0,
}
}
func (pool *ValidatorPool) Add(validator Validator) {
pool.Lock()
defer pool.Unlock()
pool.length += 1
if pool.length > pool.size {
return
}
pool.validators <- validator
}
func (pool *ValidatorPool) GetValidator() Validator {
return <-pool.validators
}
func (pool *ValidatorPool) SetValidator(vlt Validator) {
pool.validators <- vlt
}