forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlepool.go
More file actions
90 lines (67 loc) · 1.26 KB
/
Copy pathhandlepool.go
File metadata and controls
90 lines (67 loc) · 1.26 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
elektra "github.com/ElektraInitiative/libelektra/src/bindings/go-elektra/kdb"
)
type handle struct {
kdb elektra.KDB
keySet elektra.KeySet
}
type handlePool struct {
handles chan *handle
doRefill chan int
size int
}
func initPool(size int) *handlePool {
pool := &handlePool{
handles: make(chan *handle, size),
doRefill: make(chan int, 1),
size: size,
}
go pool.refillLoop()
pool.refill()
return pool
}
func newHandle() (*handle, error) {
kdb := elektra.New()
err := kdb.Open()
if err != nil {
return nil, err
}
parentKey, err := elektra.NewKey("/")
if err != nil {
return nil, err
}
defer parentKey.Close()
ks := elektra.NewKeySet()
if _, err = kdb.Get(ks, parentKey); err != nil {
return nil, err
}
return &handle{
kdb: kdb,
keySet: ks,
}, nil
}
func (p *handlePool) refill() {
select {
case p.doRefill <- 1:
default:
}
}
func (p *handlePool) pop() *handle {
return <-p.handles
}
func (p *handlePool) refillLoop() {
for range p.doRefill {
for l := len(p.handles); l < p.size; {
h, err := newHandle()
if err != nil {
panic("could not create new handle: " + err.Error())
}
p.handles <- h
}
}
}
func (p *handlePool) Get() *handle {
p.refill()
return p.pop()
}