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.6 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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}
  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. message.Comment = nil
  45. message.ActUser.Email = ""
  46. message.ActUser.Passwd = ""
  47. message.ActUser.PasswdHashAlgo = ""
  48. message.ActUser.PrivateKey = ""
  49. message.ActUser.PublicKey = ""
  50. message.ActUser.Salt = ""
  51. LastActionsQueue.Push(message)
  52. for _, client := range h.Clients.Keys() {
  53. select {
  54. case client.(*Client).Send <- message:
  55. default:
  56. close(client.(*Client).Send)
  57. h.Clients.Delete(client)
  58. }
  59. }
  60. }
  61. case s := <-sig:
  62. log.Info("received signal", s)
  63. signalsReceived++
  64. if signalsReceived < 2 {
  65. for _, client := range h.Clients.Keys() {
  66. h.Clients.Delete(client)
  67. client.(*Client).Close()
  68. }
  69. break
  70. }
  71. }
  72. }
  73. }
  74. func isInOpTypes(types []int, opType models.ActionType) bool {
  75. isFound := false
  76. for _, value := range types {
  77. if value == int(opType) {
  78. isFound = true
  79. break
  80. }
  81. }
  82. return isFound
  83. }
  84. func initActionQueue() {
  85. actions, err := models.GetLast20PublicFeeds(opTypes)
  86. if err == nil {
  87. for i := len(actions) - 1; i >= 0; i-- {
  88. user, err := models.GetUserByID(actions[i].UserID)
  89. if err == nil {
  90. if !user.IsOrganization() {
  91. actions[i].ActUser.Email = ""
  92. actions[i].ActUser.Passwd = ""
  93. actions[i].ActUser.PasswdHashAlgo = ""
  94. actions[i].ActUser.PrivateKey = ""
  95. actions[i].ActUser.PublicKey = ""
  96. actions[i].ActUser.Salt = ""
  97. LastActionsQueue.Push(actions[i])
  98. }
  99. }
  100. }
  101. }
  102. }