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
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package hub
  2. import (
  3. "fmt"
  4. "sort"
  5. "github.com/9seconds/mtg/config"
  6. )
  7. type connectionList struct {
  8. connections []*connection
  9. }
  10. func (c *connectionList) get(conn *ProxyConn) (*connection, error) {
  11. if len(c.connections) > 0 && c.connections[0].Len() < config.C.MultiplexPerConnection {
  12. if err := c.connections[0].Attach(conn); err == nil {
  13. return c.connections[0], nil
  14. }
  15. }
  16. newConn, err := newConnection(conn.req)
  17. if err != nil {
  18. return nil, fmt.Errorf("cannot allocate a new connection: %w", err)
  19. }
  20. if err = newConn.Attach(conn); err != nil {
  21. newConn.Close()
  22. return nil, fmt.Errorf("cannot attach to the newly created connection: %w", err)
  23. }
  24. c.connections = append(c.connections, newConn)
  25. lastIndex := len(c.connections) - 1
  26. c.connections[0], c.connections[lastIndex] = c.connections[lastIndex], c.connections[0]
  27. return newConn, nil
  28. }
  29. func (c *connectionList) gc() {
  30. prevLen := len(c.connections)
  31. if prevLen == 0 {
  32. return
  33. }
  34. for i := len(c.connections) - 1; i >= 0; i-- {
  35. lastIndex := len(c.connections) - 1
  36. if c.connections[i].Done() {
  37. c.connections[i].Close()
  38. if len(c.connections)-1 == i {
  39. c.connections = c.connections[:lastIndex]
  40. } else {
  41. c.connections[i], c.connections[lastIndex] = c.connections[lastIndex], c.connections[i]
  42. }
  43. }
  44. }
  45. if prevLen != len(c.connections) {
  46. c.sort()
  47. }
  48. }
  49. func (c *connectionList) sort() {
  50. if len(c.connections) > 1 {
  51. sort.Slice(c.connections, func(i, j int) bool {
  52. return c.connections[i].Len() < c.connections[j].Len()
  53. })
  54. }
  55. }