Highly-opinionated (ex-bullshit-free) MTPROTO proxy for Telegram. If you use v1.0 or upgrade broke you proxy, please read the chapter Version 2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

registry.go 981B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. package hub
  2. import (
  3. "context"
  4. "sync"
  5. "github.com/9seconds/mtg/conntypes"
  6. )
  7. var Registry *RegistryStruct
  8. type RegistryStruct struct {
  9. conns map[string]*closeableChannel
  10. ctx context.Context
  11. mutex sync.RWMutex
  12. }
  13. func (r *RegistryStruct) Register(id conntypes.ConnID) ChannelReadCloser {
  14. channel := newCloseableChannel(r.ctx)
  15. r.mutex.Lock()
  16. r.conns[string(id[:])] = channel
  17. r.mutex.Unlock()
  18. return channel
  19. }
  20. func (r *RegistryStruct) Unregister(id conntypes.ConnID) {
  21. r.mutex.Lock()
  22. defer r.mutex.Unlock()
  23. if channel, ok := r.conns[string(id[:])]; ok {
  24. channel.Close()
  25. delete(r.conns, string(id[:]))
  26. }
  27. }
  28. func (r *RegistryStruct) getChannel(id conntypes.ConnID) (*closeableChannel, bool) {
  29. r.mutex.RLock()
  30. defer r.mutex.RUnlock()
  31. if value, ok := r.conns[string(id[:])]; ok {
  32. return value, true
  33. }
  34. return nil, false
  35. }
  36. func InitRegistry(ctx context.Context) {
  37. Registry = &RegistryStruct{
  38. ctx: ctx,
  39. conns: map[string]*closeableChannel{},
  40. }
  41. }