group_concurrency.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. package executor
  2. import (
  3. "fmt"
  4. "slices"
  5. "sync"
  6. config "github.com/OliveTin/OliveTin/internal/config"
  7. log "github.com/sirupsen/logrus"
  8. )
  9. type groupLimit struct {
  10. name string
  11. maxConcurrent int
  12. }
  13. type queuedExecution struct {
  14. req *ExecutionRequest
  15. wg *sync.WaitGroup
  16. }
  17. func actionGroupLimits(req *ExecutionRequest) []groupLimit {
  18. if !hasActionGroupContext(req) {
  19. return nil
  20. }
  21. limits := make([]groupLimit, 0, len(req.Binding.Action.Groups))
  22. for _, groupName := range req.Binding.Action.Groups {
  23. if limit, ok := groupLimitFromConfig(req.Cfg, groupName); ok {
  24. limits = append(limits, limit)
  25. }
  26. }
  27. return limits
  28. }
  29. func hasActionGroupContext(req *ExecutionRequest) bool {
  30. return req != nil && req.Binding != nil && req.Binding.Action != nil && req.Cfg != nil
  31. }
  32. func groupLimitFromConfig(cfg *config.Config, groupName string) (groupLimit, bool) {
  33. group, found := cfg.ActionGroups[groupName]
  34. if !found || group == nil || group.MaxConcurrent < 1 {
  35. return groupLimit{}, false
  36. }
  37. return groupLimit{name: groupName, maxConcurrent: group.MaxConcurrent}, true
  38. }
  39. func actionNeedsGroupLimit(req *ExecutionRequest) bool {
  40. return len(actionGroupLimits(req)) > 0
  41. }
  42. func actionInGroup(action *config.Action, groupName string) bool {
  43. if action == nil {
  44. return false
  45. }
  46. return slices.Contains(action.Groups, groupName)
  47. }
  48. func (e *Executor) countActiveInGroup(groupName string) int {
  49. e.logmutex.RLock()
  50. defer e.logmutex.RUnlock()
  51. return e.countActiveInGroupLocked(groupName)
  52. }
  53. func (e *Executor) countActiveInGroupLocked(groupName string) int {
  54. count := 0
  55. for _, logEntry := range e.logs {
  56. if logEntryIsActiveInGroup(logEntry, groupName) {
  57. count++
  58. }
  59. }
  60. return count
  61. }
  62. func logEntryIsActiveInGroup(logEntry *InternalLogEntry, groupName string) bool {
  63. if inactiveLogEntry(logEntry) {
  64. return false
  65. }
  66. return actionInGroup(logEntry.Binding.Action, groupName)
  67. }
  68. func inactiveLogEntry(logEntry *InternalLogEntry) bool {
  69. if logEntry == nil {
  70. return true
  71. }
  72. return logEntryIsInactive(logEntry)
  73. }
  74. func logEntryIsInactive(logEntry *InternalLogEntry) bool {
  75. if logEntry.ExecutionFinished || logEntry.Queued {
  76. return true
  77. }
  78. return logEntry.Binding == nil || logEntry.Binding.Action == nil
  79. }
  80. func (e *Executor) groupsHaveCapacityForActive(req *ExecutionRequest) bool {
  81. for _, limit := range actionGroupLimits(req) {
  82. if e.countActiveInGroup(limit.name) >= (limit.maxConcurrent + 1) {
  83. return false
  84. }
  85. }
  86. return true
  87. }
  88. func (e *Executor) groupsHaveCapacityForQueued(req *ExecutionRequest) bool {
  89. for _, limit := range actionGroupLimits(req) {
  90. if e.countActiveInGroup(limit.name) >= limit.maxConcurrent {
  91. return false
  92. }
  93. }
  94. return true
  95. }
  96. func firstFullGroupName(e *Executor, req *ExecutionRequest) string {
  97. for _, limit := range actionGroupLimits(req) {
  98. if e.countActiveInGroup(limit.name) >= (limit.maxConcurrent + 1) {
  99. return limit.name
  100. }
  101. }
  102. return ""
  103. }
  104. func firstFullGroupNameLocked(e *Executor, req *ExecutionRequest) string {
  105. for _, limit := range actionGroupLimits(req) {
  106. if e.countActiveInGroupLocked(limit.name) >= (limit.maxConcurrent + 1) {
  107. return limit.name
  108. }
  109. }
  110. return ""
  111. }
  112. func (e *Executor) queueRequest(req *ExecutionRequest, wg *sync.WaitGroup) {
  113. e.groupQueueMu.Lock()
  114. var groupName string
  115. req.mutateLogEntry(func(entry *InternalLogEntry) {
  116. groupName = firstFullGroupNameLocked(e, req)
  117. entry.Queued = true
  118. entry.QueuedForGroup = groupName
  119. entry.Output = fmt.Sprintf("Queued waiting for action group %q", groupName)
  120. })
  121. e.groupQueue = append(e.groupQueue, &queuedExecution{req: req, wg: wg})
  122. e.groupQueueMu.Unlock()
  123. e.drainGroupQueue()
  124. log.WithFields(log.Fields{
  125. "actionTitle": req.logEntry.ActionTitle,
  126. "groupName": groupName,
  127. }).Infof("Action queued due to action group concurrency limit")
  128. }
  129. func (e *Executor) drainGroupQueue() {
  130. e.groupQueueMu.Lock()
  131. if len(e.groupQueue) == 0 {
  132. e.groupQueueMu.Unlock()
  133. return
  134. }
  135. next := e.groupQueue[0]
  136. if !e.groupsHaveCapacityForQueued(next.req) {
  137. e.groupQueueMu.Unlock()
  138. return
  139. }
  140. e.groupQueue = e.groupQueue[1:]
  141. next.req.mutateLogEntry(func(entry *InternalLogEntry) {
  142. entry.Queued = false
  143. entry.QueuedForGroup = ""
  144. })
  145. e.groupQueueMu.Unlock()
  146. go e.runDequeuedExecution(next)
  147. }
  148. func (e *Executor) runDequeuedExecution(queued *queuedExecution) {
  149. req := queued.req
  150. req.skipRequestRegistration = true
  151. e.runExecutionSteps(req)
  152. e.finishExecChain(req)
  153. queued.wg.Done()
  154. }