log_retention.go 2.7 KB

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