log_retention.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package executor
  2. import (
  3. "time"
  4. config "github.com/OliveTin/OliveTin/internal/config"
  5. )
  6. // The in-memory log history is capped so that requests cannot grow it without
  7. // limit. The oldest finished entries are evicted first. Entries are never
  8. // evicted while they are still running or queued, or while they still count
  9. // towards their action's maxRate window, because evicting those would let
  10. // unrelated requests reset an action's rate limit. Logs saved to disk are not
  11. // affected.
  12. func (e *Executor) takeNextLogIndexLocked() int64 {
  13. index := e.nextLogIndex
  14. e.nextLogIndex++
  15. return index
  16. }
  17. func (e *Executor) logHistoryLimit() int {
  18. if e.Cfg == nil || e.Cfg.LogHistoryLimit <= 0 {
  19. return config.DefaultLogHistoryLimit
  20. }
  21. return e.Cfg.LogHistoryLimit
  22. }
  23. // evictOldLogsLocked must be called with logmutex held for writing.
  24. func (e *Executor) evictOldLogsLocked(now time.Time) {
  25. excess := len(e.logsTrackingIdsByDate) - e.logHistoryLimit()
  26. if excess <= 0 {
  27. return
  28. }
  29. kept := make([]string, 0, len(e.logsTrackingIdsByDate)-excess)
  30. evicted := make(map[*InternalLogEntry]bool, excess)
  31. for _, trackingID := range e.logsTrackingIdsByDate {
  32. if excess > 0 && canEvictLogEntry(e.logs[trackingID], now) {
  33. e.removeLogLocked(trackingID, evicted)
  34. excess--
  35. continue
  36. }
  37. kept = append(kept, trackingID)
  38. }
  39. e.logsTrackingIdsByDate = kept
  40. e.removeEvictedFromBindingsLocked(evicted)
  41. }
  42. func canEvictLogEntry(entry *InternalLogEntry, now time.Time) bool {
  43. if entry == nil {
  44. return true
  45. }
  46. if !entry.ExecutionFinished {
  47. return false
  48. }
  49. return !countsTowardsRateLimit(entry, now)
  50. }
  51. func countsTowardsRateLimit(entry *InternalLogEntry, now time.Time) bool {
  52. if entry.Binding == nil || entry.Binding.Action == nil {
  53. return false
  54. }
  55. window := longestRateWindow(entry.Binding.Action.MaxRate)
  56. if window <= 0 {
  57. return false
  58. }
  59. return shouldCountExecution(entry, now.Add(-window))
  60. }
  61. func longestRateWindow(rates []config.RateSpec) time.Duration {
  62. longest := time.Duration(0)
  63. for _, rate := range rates {
  64. longest = max(longest, parseDuration(rate))
  65. }
  66. return longest
  67. }
  68. func (e *Executor) removeLogLocked(trackingID string, evicted map[*InternalLogEntry]bool) {
  69. entry := e.logs[trackingID]
  70. delete(e.logs, trackingID)
  71. if entry == nil || entry.Binding == nil {
  72. return
  73. }
  74. evicted[entry] = true
  75. }
  76. // Each affected binding's list is rebuilt once per eviction, not once per
  77. // evicted entry, so evicting many entries of one binding stays linear.
  78. func (e *Executor) removeEvictedFromBindingsLocked(evicted map[*InternalLogEntry]bool) {
  79. bindingIDs := make(map[string]bool)
  80. for entry := range evicted {
  81. bindingIDs[entry.Binding.ID] = true
  82. }
  83. for bindingID := range bindingIDs {
  84. e.removeEvictedFromBindingLocked(bindingID, evicted)
  85. }
  86. }
  87. func (e *Executor) removeEvictedFromBindingLocked(bindingID string, evicted map[*InternalLogEntry]bool) {
  88. // Build a new slice rather than deleting in place, because callers may still
  89. // be reading the previous slice after releasing the lock.
  90. remaining := make([]*InternalLogEntry, 0, len(e.LogsByBindingId[bindingID]))
  91. for _, entry := range e.LogsByBindingId[bindingID] {
  92. if !evicted[entry] {
  93. remaining = append(remaining, entry)
  94. }
  95. }
  96. if len(remaining) == 0 {
  97. delete(e.LogsByBindingId, bindingID)
  98. return
  99. }
  100. e.LogsByBindingId[bindingID] = remaining
  101. }