forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbroker.go
61 lines (50 loc) · 1.46 KB
/
broker.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 subscriptions
import (
"fmt"
"sync"
)
// Broker defines a struct for managing subscriptions clients.
type Broker struct {
mux sync.RWMutex
clients map[string]Client
}
// NewBroker initializes and returns a new Broker instance.
func NewBroker() *Broker {
return &Broker{
clients: make(map[string]Client),
}
}
// Clients returns all registered clients.
func (b *Broker) Clients() map[string]Client {
return b.clients
}
// ClientById finds a registered client by its id.
//
// Returns non-nil error when client with clientId is not registered.
func (b *Broker) ClientById(clientId string) (Client, error) {
b.mux.RLock()
defer b.mux.RUnlock()
client, ok := b.clients[clientId]
if !ok {
return nil, fmt.Errorf("No client associated with connection ID %q", clientId)
}
return client, nil
}
// Register adds a new client to the broker instance.
func (b *Broker) Register(client Client) {
b.mux.Lock()
defer b.mux.Unlock()
b.clients[client.Id()] = client
}
// Unregister removes a single client by its id.
//
// If client with clientId doesn't exist, this method does nothing.
func (b *Broker) Unregister(clientId string) {
b.mux.Lock()
defer b.mux.Unlock()
// Note:
// There is no need to explicitly close the client's channel since it will be GC-ed anyway.
// Addinitionally, closing the channel explicitly could panic when there are several
// subscriptions attached to the client that needs to receive the same event.
delete(b.clients, clientId)
}