You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

clientManager.go 2.7 kB

2 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
2 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package socketwrap
  2. import (
  3. "os"
  4. "os/signal"
  5. "syscall"
  6. "code.gitea.io/gitea/models"
  7. "code.gitea.io/gitea/modules/log"
  8. "github.com/elliotchance/orderedmap"
  9. )
  10. var opTypes = []int{1, 2, 5, 6, 7, 9, 10, 11, 12, 13, 14, 15, 17, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 35}
  11. type ClientsManager struct {
  12. Clients *orderedmap.OrderedMap
  13. Register chan *Client
  14. Unregister chan *Client
  15. }
  16. func NewClientsManager() *ClientsManager {
  17. return &ClientsManager{
  18. Register: make(chan *Client),
  19. Unregister: make(chan *Client),
  20. Clients: orderedmap.NewOrderedMap(),
  21. }
  22. }
  23. const MaxClients = 100
  24. var LastActionsQueue = NewSyncQueue(15)
  25. func (h *ClientsManager) Run() {
  26. initActionQueue()
  27. sig := make(chan os.Signal, 1)
  28. signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
  29. var signalsReceived uint
  30. for {
  31. select {
  32. case client := <-h.Register:
  33. h.Clients.Set(client, true)
  34. if h.Clients.Len() > MaxClients {
  35. h.Clients.Delete(h.Clients.Front().Key)
  36. }
  37. case client := <-h.Unregister:
  38. if _, ok := h.Clients.Get(client); ok {
  39. h.Clients.Delete(client)
  40. close(client.Send)
  41. }
  42. case message := <-models.ActionChan:
  43. if isInOpTypes(opTypes, message.OpType) {
  44. filterUserPrivateInfo(message)
  45. LastActionsQueue.Push(message)
  46. for _, client := range h.Clients.Keys() {
  47. select {
  48. case client.(*Client).Send <- message:
  49. default:
  50. close(client.(*Client).Send)
  51. h.Clients.Delete(client)
  52. }
  53. }
  54. }
  55. case s := <-sig:
  56. log.Info("received signal", s)
  57. signalsReceived++
  58. if signalsReceived < 2 {
  59. for _, client := range h.Clients.Keys() {
  60. h.Clients.Delete(client)
  61. client.(*Client).Close()
  62. }
  63. break
  64. }
  65. }
  66. }
  67. }
  68. func isInOpTypes(types []int, opType models.ActionType) bool {
  69. isFound := false
  70. for _, value := range types {
  71. if value == int(opType) {
  72. isFound = true
  73. break
  74. }
  75. }
  76. return isFound
  77. }
  78. func initActionQueue() {
  79. actions, err := models.GetLast20PublicFeeds(opTypes)
  80. if err == nil {
  81. for i := len(actions) - 1; i >= 0; i-- {
  82. user, err := models.GetUserByID(actions[i].UserID)
  83. if err == nil {
  84. if !user.IsOrganization() {
  85. filterUserPrivateInfo(actions[i])
  86. LastActionsQueue.Push(actions[i])
  87. }
  88. }
  89. }
  90. }
  91. }
  92. func filterUserPrivateInfo(action *models.Action) {
  93. action.Comment = nil
  94. if action.ActUser != nil {
  95. action.ActUser.Email = ""
  96. action.ActUser.Passwd = ""
  97. action.ActUser.PasswdHashAlgo = ""
  98. action.ActUser.PrivateKey = ""
  99. action.ActUser.PublicKey = ""
  100. action.ActUser.Salt = ""
  101. action.ActUser.FullName = ""
  102. action.ActUser.AvatarEmail = ""
  103. action.ActUser.IsAdmin = false
  104. action.ActUser.EmailNotificationsPreference = ""
  105. action.ActUser.IsOperator = false
  106. }
  107. }