FILE / ScuroNeko/mtg

hub/registry.go

Исходный файл и его история в репозитории.
FILE 5009859a1e3ed7131177ddddba41a3cf26ee3c03
Files
mtg/hub/registry.go
T
2019-10-11 09:41:18 +03:00

46 lines
779 B
Go

package hub
import (
"context"
"sync"
"github.com/9seconds/mtg/conntypes"
)
type registry struct {
conns map[string]*ctxChannel
ctx context.Context
mutex sync.RWMutex
}
func (r *registry) Register(id conntypes.ConnID) ChannelReadCloser {
channel := newCtxChannel(r.ctx)
r.mutex.Lock()
r.conns[string(id[:])] = channel
r.mutex.Unlock()
return channel
}
func (r *registry) 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 *registry) getChannel(id conntypes.ConnID) (*ctxChannel, bool) {
r.mutex.RLock()
defer r.mutex.RUnlock()
if value, ok := r.conns[string(id[:])]; ok {
return value, true
}
return nil, false
}