49 lines
795 B
Go
49 lines
795 B
Go
package mqttx
|
|
|
|
import (
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
var (
|
|
ErrClientNotFound = errors.New("client not found")
|
|
ErrClientRequired = errors.New("client required")
|
|
manager = &clientManager{
|
|
pool: make(map[string]*Client),
|
|
}
|
|
)
|
|
|
|
func Get(clientId string) (*Client, error) {
|
|
return manager.Get(clientId)
|
|
}
|
|
|
|
func Push(c *Client) error {
|
|
return manager.Push(c)
|
|
}
|
|
|
|
type clientManager struct {
|
|
pool map[string]*Client
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
func (m *clientManager) Get(clientId string) (*Client, error) {
|
|
m.mu.RLock()
|
|
defer m.mu.RUnlock()
|
|
|
|
if c, ok := m.pool[clientId]; ok {
|
|
return c, nil
|
|
}
|
|
return nil, ErrClientNotFound
|
|
}
|
|
|
|
func (m *clientManager) Push(c *Client) error {
|
|
m.mu.Lock()
|
|
defer m.mu.Unlock()
|
|
|
|
if c == nil {
|
|
return ErrClientRequired
|
|
}
|
|
m.pool[c.GetId()] = c
|
|
return nil
|
|
}
|