FILE / ScuroNeko/mtg

hub/registry.go

Исходный файл и его история в репозитории.
FILE c9743b5675df007ed13fa7b89acd4b4cbafc960f
Files
mtg/hub/registry.go
T
2019-10-07 12:13:07 +03:00

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{},
}
}