-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpool.go
61 lines (49 loc) · 910 Bytes
/
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
package kuy
import (
"sync"
)
type (
pool struct {
id string
maxItem int
m sync.Mutex
items []interface{}
expireWaitCount int
respChan chan PoolResp
}
// PoolResp function define response when item joining the pool
PoolResp struct {
PoolID string
IsFull bool
TimeIsUp bool
Items []interface{}
}
)
// NewPool func create new pool
func newPool(id string, maxItem int) *pool {
return &pool{
id: id,
maxItem: maxItem,
respChan: make(chan PoolResp, maxItem),
}
}
func (p *pool) add(item interface{}) chan PoolResp {
p.m.Lock()
defer func() {
if len(p.items) == p.maxItem {
p.respChan <- PoolResp{
PoolID: p.id,
IsFull: true,
Items: p.items,
}
}
p.m.Unlock()
}()
p.items = append(p.items, item)
return p.respChan
}
func (p *pool) ableToJoin() bool {
p.m.Lock()
defer p.m.Unlock()
return len(p.items) < p.maxItem
}