MarkTemporary marks the key as temporarily unavailable with the specified cooldown duration. Returns true if this call transitions the key to temporary.
(cooldown time.Duration)
| 159 | // the specified cooldown duration. Returns true if this call |
| 160 | // transitions the key to temporary. |
| 161 | func (k *Key) MarkTemporary(cooldown time.Duration) bool { |
| 162 | k.mu.Lock() |
| 163 | defer k.mu.Unlock() |
| 164 | |
| 165 | // Permanent is irreversible. |
| 166 | if k.permanent { |
| 167 | return false |
| 168 | } |
| 169 | |
| 170 | if cooldown <= 0 { |
| 171 | cooldown = defaultCooldown |
| 172 | } |
| 173 | |
| 174 | now := k.clock.Now() |
| 175 | // Used to detect the valid -> temporary transition. |
| 176 | inCooldown := k.cooldownUntil.After(now) |
| 177 | newDeadline := now.Add(cooldown) |
| 178 | |
| 179 | // In case the key has a later expiry, keep it. |
| 180 | if k.cooldownUntil.After(newDeadline) { |
| 181 | return false |
| 182 | } |
| 183 | |
| 184 | k.cooldownUntil = newDeadline |
| 185 | return !inCooldown |
| 186 | } |
| 187 | |
| 188 | // MarkPermanent marks the key as permanently unavailable. This |
| 189 | // is a terminal state. Returns true if this call transitions |