-
Notifications
You must be signed in to change notification settings - Fork 2
/
keydatastore.go
65 lines (54 loc) · 1.6 KB
/
keydatastore.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
package main
type backendKeyDataMessage struct {
processId int32
secretKey int32
}
type registerBackendKeyMessage struct {
key backendKeyDataMessage
backend string
}
type deregisterBackendKeyMessage struct {
key backendKeyDataMessage
}
type getBackendForBackendKeyMessage struct {
key backendKeyDataMessage
returnChan chan *string
}
var registerBackendKeyChan chan registerBackendKeyMessage = make(chan registerBackendKeyMessage)
var deregisterBackedKeyChan chan deregisterBackendKeyMessage = make(chan deregisterBackendKeyMessage)
var getBackendForBackendKeyChan chan getBackendForBackendKeyMessage = make(chan getBackendForBackendKeyMessage)
func registerBackendKey(backendKeyData backendKeyDataMessage, backend string) {
registerBackendKeyChan <- registerBackendKeyMessage{
backendKeyData,
backend,
}
}
func deregisterBackedKey(backendKeyData backendKeyDataMessage) {
deregisterBackedKeyChan <- deregisterBackendKeyMessage{backendKeyData}
}
func getBackendForBackendKeyData(backendKeyData backendKeyDataMessage) *string {
msg := getBackendForBackendKeyMessage{
backendKeyData,
make(chan *string),
}
getBackendForBackendKeyChan <- msg
return <-msg.returnChan
}
func manageBackendKeyDataStorage() {
store := make(map[backendKeyDataMessage]string)
for {
select {
case register := <-registerBackendKeyChan:
store[register.key] = register.backend
case deregister := <-deregisterBackedKeyChan:
delete(store, deregister.key)
case get := <-getBackendForBackendKeyChan:
v, ok := store[get.key]
if ok {
get.returnChan <- &v
} else {
get.returnChan <- nil
}
}
}
}