-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_manager.go
165 lines (153 loc) · 3.53 KB
/
client_manager.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package server
import (
"errors"
"sync/atomic"
)
var (
ErrClientManagerClosed = errors.New("the client manager has been closed")
ErrClientAlreadyRegistered = errors.New("a client with that ID is already registered")
ErrClientNotRegistered = errors.New("no client with that ID is registered")
)
type ClientManager interface {
// Run loop for the client manager.
Run()
// Registers a client to accept requests via HTTP requests.
Register(client Client) error
// Unregisters the client from accepting HTTP requests.
// Call this function when the client is in a state where it can't
// provide content anymore, e.g. because it disconnected
// or the server closed the connection.
Unregister(client Client) error
// Returns the client for a given client ID, if it is registered.
Get(clientID UUID) (Client, error)
// Closes the manager run loop, closes all connections
// and returns once all clients have exited their run loop.
Close()
}
type getMessage struct {
clientID UUID
out chan Client
outErr chan error
}
type registerMessage struct {
client Client
outErr chan error
}
type clientManager struct {
dirty atomic.Bool
clients map[UUID]Client
get chan *getMessage
register chan *registerMessage
unregister chan Client
stop chan struct{}
done chan struct{}
runDone chan struct{}
}
func NewClientManager() ClientManager {
return &clientManager{
dirty: atomic.Bool{},
clients: make(map[UUID]Client),
get: make(chan *getMessage),
register: make(chan *registerMessage),
unregister: make(chan Client),
stop: make(chan struct{}),
done: make(chan struct{}),
runDone: make(chan struct{}),
}
}
func (m *clientManager) Run() {
if !m.dirty.CompareAndSwap(false, true) {
panic("client manager is in a dirty state")
}
defer func() {
for _, client := range m.clients {
client.Close()
}
for _, client := range m.clients {
<-client.Closed()
}
m.clients = nil
close(m.runDone)
}()
for {
select {
case <-m.stop:
close(m.done)
return
case msg := <-m.get:
client, ok := m.clients[msg.clientID]
if !ok {
msg.outErr <- ErrClientNotRegistered
continue
}
msg.out <- client
case msg := <-m.register:
key := msg.client.ID()
if _, ok := m.clients[key]; ok {
msg.outErr <- ErrClientAlreadyRegistered
continue
}
m.clients[key] = msg.client
case client := <-m.unregister:
key := client.ID()
if client, ok := m.clients[key]; ok {
delete(m.clients, key)
client.Close()
}
}
}
}
func (m *clientManager) Register(client Client) error {
errChan := make(chan error)
select {
case m.register <- ®isterMessage{
client: client,
outErr: errChan,
}:
return nil
case err := <-errChan:
return err
case <-m.done:
return ErrClientManagerClosed
}
}
func (m *clientManager) Unregister(client Client) error {
select {
case m.unregister <- client:
return nil
case <-m.done:
return ErrClientManagerClosed
}
}
func (m *clientManager) Get(clientID UUID) (Client, error) {
outChan := make(chan Client)
errChan := make(chan error)
select {
case m.get <- &getMessage{
clientID: clientID,
out: outChan,
outErr: errChan,
}:
case <-m.done:
return nil, ErrClientManagerClosed
}
select {
case client := <-outChan:
return client, nil
case err := <-errChan:
return nil, err
case <-m.done:
return nil, ErrClientManagerClosed
}
}
func (m *clientManager) Close() {
if !m.dirty.Load() {
return
}
select {
case m.stop <- struct{}{}:
case <-m.done:
return
}
<-m.runDone
}