|
| 1 | +package handler |
| 2 | + |
| 3 | +import ( |
| 4 | + "sync" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/git-pkgs/cooldown" |
| 8 | +) |
| 9 | + |
| 10 | +// A rewrite made with cooldown on stays right until a version it held back |
| 11 | +// leaves cooldown: versions it let through stay allowed as time passes, and |
| 12 | +// new versions upstream change the document and so the cache key. So the |
| 13 | +// cached rewrite expires when the earliest version it withheld becomes |
| 14 | +// available, instead of after a fixed time. |
| 15 | +// |
| 16 | +// Handlers ask cooldown through Proxy.cooldown, which tells every rewrite |
| 17 | +// running at that moment when a withheld version becomes available. A |
| 18 | +// rewrite may hear of versions another rewrite withheld at the same time; |
| 19 | +// that only makes it expire earlier than it has to, never later. |
| 20 | + |
| 21 | +// maxCooldownRewriteTTL is the longest a rewrite made with cooldown on is |
| 22 | +// served, even when it held nothing back. |
| 23 | +const maxCooldownRewriteTTL = 24 * time.Hour |
| 24 | + |
| 25 | +// minCooldownRewriteTTL is the shortest cap on a rewrite made with cooldown |
| 26 | +// on, however short the cooldown. |
| 27 | +const minCooldownRewriteTTL = time.Minute |
| 28 | + |
| 29 | +// cooldownRewriteTTL returns the longest a rewrite made with cooldown on is |
| 30 | +// served: the shortest cooldown configured, between a minute and a day. It |
| 31 | +// only matters when a rewrite held nothing back, or held back a version |
| 32 | +// whose availability is unknown. |
| 33 | +func (p *Proxy) cooldownRewriteTTL() time.Duration { |
| 34 | + ttl := p.ShortestCooldown |
| 35 | + switch { |
| 36 | + case ttl <= 0 || ttl > maxCooldownRewriteTTL: |
| 37 | + return maxCooldownRewriteTTL |
| 38 | + case ttl < minCooldownRewriteTTL: |
| 39 | + return minCooldownRewriteTTL |
| 40 | + } |
| 41 | + return ttl |
| 42 | +} |
| 43 | + |
| 44 | +// cooldownWatch holds the rewrites running with cooldown on. |
| 45 | +type cooldownWatch struct { |
| 46 | + mu sync.Mutex |
| 47 | + active map[*cooldownRecorder]struct{} |
| 48 | +} |
| 49 | + |
| 50 | +// cooldownRecorder collects when the versions withheld during one rewrite |
| 51 | +// become available. |
| 52 | +type cooldownRecorder struct { |
| 53 | + // earliest is when the first withheld version becomes available, or |
| 54 | + // zero if none was withheld. |
| 55 | + earliest time.Time |
| 56 | + // unknown is set when a version was withheld without a time it becomes |
| 57 | + // available. |
| 58 | + unknown bool |
| 59 | +} |
| 60 | + |
| 61 | +// watchCooldown starts recording the versions cooldown holds back for a |
| 62 | +// rewrite about to run. unwatchCooldown ends it. |
| 63 | +func (p *Proxy) watchCooldown() *cooldownRecorder { |
| 64 | + rec := &cooldownRecorder{} |
| 65 | + w := &p.cooldownWatch |
| 66 | + w.mu.Lock() |
| 67 | + defer w.mu.Unlock() |
| 68 | + if w.active == nil { |
| 69 | + w.active = make(map[*cooldownRecorder]struct{}) |
| 70 | + } |
| 71 | + w.active[rec] = struct{}{} |
| 72 | + return rec |
| 73 | +} |
| 74 | + |
| 75 | +func (p *Proxy) unwatchCooldown(rec *cooldownRecorder) { |
| 76 | + w := &p.cooldownWatch |
| 77 | + w.mu.Lock() |
| 78 | + defer w.mu.Unlock() |
| 79 | + delete(w.active, rec) |
| 80 | +} |
| 81 | + |
| 82 | +// cooldownExpiry returns when a rewrite that started at start and recorded |
| 83 | +// into rec should expire: when the first version it held back leaves |
| 84 | +// cooldown, and no later than cooldownRewriteTTL after start. |
| 85 | +func (p *Proxy) cooldownExpiry(rec *cooldownRecorder, start time.Time) time.Time { |
| 86 | + w := &p.cooldownWatch |
| 87 | + w.mu.Lock() |
| 88 | + earliest, unknown := rec.earliest, rec.unknown |
| 89 | + w.mu.Unlock() |
| 90 | + |
| 91 | + limit := start.Add(p.cooldownRewriteTTL()) |
| 92 | + if unknown || earliest.IsZero() || earliest.After(limit) { |
| 93 | + return limit |
| 94 | + } |
| 95 | + return earliest |
| 96 | +} |
| 97 | + |
| 98 | +// withheld tells the running rewrites that a version is held back until |
| 99 | +// availableAt, zero when that is unknown. |
| 100 | +func (p *Proxy) withheld(availableAt time.Time) { |
| 101 | + w := &p.cooldownWatch |
| 102 | + w.mu.Lock() |
| 103 | + defer w.mu.Unlock() |
| 104 | + for rec := range w.active { |
| 105 | + switch { |
| 106 | + case availableAt.IsZero(): |
| 107 | + rec.unknown = true |
| 108 | + case rec.earliest.IsZero() || availableAt.Before(rec.earliest): |
| 109 | + rec.earliest = availableAt |
| 110 | + } |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// cooldown returns the proxy's cooldown policy, telling the running rewrites |
| 115 | +// about each version it holds back. Handlers use it rather than p.Cooldown. |
| 116 | +func (p *Proxy) cooldown() CooldownPolicy { //nolint:ireturn // nil when cooldown is not configured, as p.Cooldown is |
| 117 | + if p.Cooldown == nil { |
| 118 | + return nil |
| 119 | + } |
| 120 | + return recordingCooldown{p: p} |
| 121 | +} |
| 122 | + |
| 123 | +// recordingCooldown is the proxy's cooldown policy, reporting the versions it |
| 124 | +// holds back to the running rewrites. |
| 125 | +type recordingCooldown struct { |
| 126 | + p *Proxy |
| 127 | +} |
| 128 | + |
| 129 | +func (r recordingCooldown) IsAllowed(ecosystem, packagePURL string, publishedAt time.Time) bool { |
| 130 | + if r.p.Cooldown.IsAllowed(ecosystem, packagePURL, publishedAt) { |
| 131 | + return true |
| 132 | + } |
| 133 | + r.p.withheld(r.availableAt(ecosystem, packagePURL, publishedAt)) |
| 134 | + return false |
| 135 | +} |
| 136 | + |
| 137 | +func (r recordingCooldown) Evaluate(ecosystem, packagePURL string, publishedAt, evaluatedAt time.Time) cooldown.Decision { |
| 138 | + d := r.p.Cooldown.Evaluate(ecosystem, packagePURL, publishedAt, evaluatedAt) |
| 139 | + if !d.Allowed { |
| 140 | + r.p.withheld(d.AvailableAt) |
| 141 | + } |
| 142 | + return d |
| 143 | +} |
| 144 | + |
| 145 | +func (r recordingCooldown) For(ecosystem, packagePURL string) time.Duration { |
| 146 | + return r.p.Cooldown.For(ecosystem, packagePURL) |
| 147 | +} |
| 148 | + |
| 149 | +func (r recordingCooldown) Enabled() bool { |
| 150 | + return r.p.Cooldown.Enabled() |
| 151 | +} |
| 152 | + |
| 153 | +// availableAt is when a version IsAllowed held back leaves cooldown, or zero |
| 154 | +// when that is unknown. |
| 155 | +func (r recordingCooldown) availableAt(ecosystem, packagePURL string, publishedAt time.Time) time.Time { |
| 156 | + if publishedAt.IsZero() { |
| 157 | + return time.Time{} |
| 158 | + } |
| 159 | + d := r.p.Cooldown.For(ecosystem, packagePURL) |
| 160 | + if d <= 0 { |
| 161 | + return time.Time{} |
| 162 | + } |
| 163 | + return publishedAt.Add(d) |
| 164 | +} |
0 commit comments