2022-07-06 23:19:05 +02:00
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-18 15:41:33 +02:00
|
|
|
// Clients returns a shallow copy of all registered clients indexed
|
|
|
|
// with their connection id.
|
2022-07-06 23:19:05 +02:00
|
|
|
func (b *Broker) Clients() map[string]Client {
|
2022-10-30 10:28:14 +02:00
|
|
|
b.mux.RLock()
|
|
|
|
defer b.mux.RUnlock()
|
|
|
|
|
2023-01-18 15:41:33 +02:00
|
|
|
copy := make(map[string]Client, len(b.clients))
|
|
|
|
|
|
|
|
for id, c := range b.clients {
|
|
|
|
copy[id] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return copy
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2022-08-14 18:30:45 +02:00
|
|
|
b.mux.RLock()
|
|
|
|
defer b.mux.RUnlock()
|
|
|
|
|
2022-07-06 23:19:05 +02:00
|
|
|
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()
|
|
|
|
|
2023-01-18 15:41:33 +02:00
|
|
|
if client, ok := b.clients[clientId]; ok {
|
|
|
|
client.Discard()
|
|
|
|
delete(b.clients, clientId)
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|