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
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

connection_hub.go 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package hub
  2. import (
  3. "time"
  4. "go.uber.org/zap"
  5. "github.com/9seconds/mtg/protocol"
  6. )
  7. const hubGCEvery = time.Minute
  8. type connectionHubRequest struct {
  9. request *protocol.TelegramRequest
  10. response chan<- *connection
  11. }
  12. type connectionHub struct {
  13. sockets map[int]*connection
  14. logger *zap.SugaredLogger
  15. channelBrokenSockets chan int
  16. channelConnectionRequests chan *connectionHubRequest
  17. channelReturnConnections chan *connection
  18. }
  19. func (c *connectionHub) run() {
  20. ticker := time.NewTicker(hubGCEvery)
  21. defer ticker.Stop()
  22. for {
  23. select {
  24. case <-ticker.C:
  25. c.runGC()
  26. case request := <-c.channelConnectionRequests:
  27. c.runConnectionRequest(request)
  28. case id := <-c.channelBrokenSockets:
  29. c.runBrokenSocket(id)
  30. case conn := <-c.channelReturnConnections:
  31. c.runReturnConnection(conn)
  32. }
  33. }
  34. }
  35. func (c *connectionHub) runGC() {
  36. logger := c.logger.Named("gc")
  37. for key, conn := range c.sockets {
  38. switch {
  39. case conn.closed():
  40. logger.Debugw("Delete closed socket", "key", key)
  41. delete(c.sockets, key)
  42. case conn.idle():
  43. logger.Debugw("Delete idle socket", "key", key)
  44. conn.shutdown()
  45. delete(c.sockets, key)
  46. return
  47. }
  48. }
  49. }
  50. func (c *connectionHub) runConnectionRequest(req *connectionHubRequest) {
  51. logger := c.logger.Named("request").With("connection-id", req.request.ConnID)
  52. for key, conn := range c.sockets {
  53. delete(c.sockets, key)
  54. if !conn.closed() {
  55. logger.Debugw("Choose connection",
  56. "id", conn.id,
  57. "remote_addr", conn.conn.RemoteAddr())
  58. req.response <- conn
  59. close(req.response)
  60. return
  61. }
  62. }
  63. if conn, err := newConnection(req.request, c); err == nil {
  64. logger.Debugw("New connection",
  65. "id", conn.id,
  66. "remote_addr", conn.conn.RemoteAddr())
  67. req.response <- conn
  68. }
  69. close(req.response)
  70. }
  71. func (c *connectionHub) runBrokenSocket(id int) {
  72. c.logger.Named("broken-socket").Debugw("Delete broken socket", "id", id)
  73. delete(c.sockets, id)
  74. }
  75. func (c *connectionHub) runReturnConnection(conn *connection) {
  76. c.logger.Named("return-connection").Debugw("Return connection",
  77. "id", conn.id,
  78. "remote_addr", conn.conn.RemoteAddr())
  79. c.sockets[conn.id] = conn
  80. }
  81. func newConnectionHub(logger *zap.SugaredLogger) *connectionHub {
  82. rv := &connectionHub{
  83. logger: logger.Named("connection-hub"),
  84. sockets: map[int]*connection{},
  85. channelBrokenSockets: make(chan int, 1),
  86. channelConnectionRequests: make(chan *connectionHubRequest),
  87. channelReturnConnections: make(chan *connection, 1),
  88. }
  89. go rv.run()
  90. return rv
  91. }