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

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