-
Notifications
You must be signed in to change notification settings - Fork 2
/
zcnxadmin.go
84 lines (67 loc) · 1.54 KB
/
zcnxadmin.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package server
import (
"sync"
"log"
)
type ZConnectionAdmin interface {
Register(ZConnection)
Retrieve(uint64) ZConnection
RetrieveAll() []ZConnection
Remove(ZConnection)
Evacuate()
PoolSize() int
}
type ConnectionAdmin struct {
// A vault of connections
Pool map[uint64]ZConnection
Mutex sync.RWMutex
}
func AdmInit() ZConnectionAdmin {
return &ConnectionAdmin {
Pool: make(map[uint64]ZConnection),
}
}
// Add new connection to the vault
func (ca *ConnectionAdmin) Register(conn ZConnection) {
ca.Mutex.Lock()
defer ca.Mutex.Unlock()
ca.Pool[conn.GetID()] = conn
}
// Get the connection via its id
func (ca *ConnectionAdmin) Retrieve(cid uint64) ZConnection {
ca.Mutex.RLock()
defer ca.Mutex.RUnlock()
if conn, ok := ca.Pool[cid]; ok {
return conn
} else {
return nil
}
}
func (ca *ConnectionAdmin) RetrieveAll() []ZConnection {
ca.Mutex.RLock()
defer ca.Mutex.RUnlock()
conns := make([]ZConnection, 0, len(ca.Pool))
for _, v := range ca.Pool {
conns = append(conns, v)
}
return conns
}
// Delete the connection from the vault
func (ca *ConnectionAdmin) Remove(conn ZConnection) {
ca.Mutex.Lock()
defer ca.Mutex.Unlock()
delete(ca.Pool, conn.GetID())
}
// Called when daemon is shut down, closing all active connections to ensure a clean quit
func (ca *ConnectionAdmin) Evacuate() {
ca.Mutex.Lock()
defer ca.Mutex.Unlock()
log.Println("[INFO] Stop all connections ...")
for cid, conn := range ca.Pool {
conn.Close()
delete(ca.Pool, cid)
}
}
func (ca *ConnectionAdmin) PoolSize() int {
return len(ca.Pool)
}