mirror of
https://github.com/ScuroNeko/mtg.git
synced 2026-08-31 21:04:02 +03:00
FILE / ScuroNeko/mtg
hub/registry.go
Исходный файл и его история в репозитории.
54 lines
981 B
Go
54 lines
981 B
Go
package hub
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"github.com/9seconds/mtg/conntypes"
|
|
)
|
|
|
|
var Registry *RegistryStruct
|
|
|
|
type RegistryStruct struct {
|
|
conns map[string]*closeableChannel
|
|
ctx context.Context
|
|
mutex sync.RWMutex
|
|
}
|
|
|
|
func (r *RegistryStruct) Register(id conntypes.ConnID) ChannelReadCloser {
|
|
channel := newCloseableChannel(r.ctx)
|
|
|
|
r.mutex.Lock()
|
|
r.conns[string(id[:])] = channel
|
|
r.mutex.Unlock()
|
|
|
|
return channel
|
|
}
|
|
|
|
func (r *RegistryStruct) Unregister(id conntypes.ConnID) {
|
|
r.mutex.Lock()
|
|
defer r.mutex.Unlock()
|
|
|
|
if channel, ok := r.conns[string(id[:])]; ok {
|
|
channel.Close()
|
|
delete(r.conns, string(id[:]))
|
|
}
|
|
}
|
|
|
|
func (r *RegistryStruct) getChannel(id conntypes.ConnID) (*closeableChannel, bool) {
|
|
r.mutex.RLock()
|
|
defer r.mutex.RUnlock()
|
|
|
|
if value, ok := r.conns[string(id[:])]; ok {
|
|
return value, true
|
|
}
|
|
return nil, false
|
|
}
|
|
|
|
func InitRegistry(ctx context.Context) {
|
|
Registry = &RegistryStruct{
|
|
ctx: ctx,
|
|
conns: map[string]*closeableChannel{},
|
|
}
|
|
}
|