Debian Codegraph @

plugin/mastodon_sse_streaming/cooldown_time.rb

raw symbols SCIP
1 # frozen_string_literal: true
2
3 module Plugin::MastodonSseStreaming
4 class CooldownTime
5 DURATION_NONE = 0
6 DURATION_MIN = 1
7 DURATION_MAX = 64
8
9 def initialize
10 @duration = DURATION_NONE
11 end
12
13 def sleep
14 Kernel.sleep(@duration) if @duration != 0
15 end
16
17 def reset
18 @duration = DURATION_NONE
19 end
20
21 def meltdown
22 @duration = DURATION_MAX
23 end
24
25 def status_code(code)
26 case code
27 when 410 then meltdown # Gone 二度と戻ってくることはないだろう
28 when 200..300 then reset
29 when 400..500 then client_error
30 when 500..600 then server_error
31 else client_error
32 end
33 end
34
35 def client_error
36 modify(@duration + 0.25)
37 end
38
39 def server_error
40 modify(@duration * 2)
41 end
42
43 def modify(d)
44 @duration = d.clamp(DURATION_MIN, DURATION_MAX)
45 end
46 end
47 end