-
Notifications
You must be signed in to change notification settings - Fork 10
/
ws_conn_unblocker.jl
51 lines (42 loc) · 1.06 KB
/
ws_conn_unblocker.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
module WSConnUnblocker
import HTTP
import Dates
import ..CacheServerHandlers
DELETE_AFTER_PERIODS = Ref(15)
running = Ref(true)
tsk = Ref{Any}(nothing)
locked = Dict()
function start()
@assert isnothing(tsk[])
tsk[] = errormonitor(@async monitor_conns())
end
function stop()
@assert !isnothing(tsk[])
running[] = false
wait(tsk[])
tsk[] = nothing
end
function monitor_conns()
while running[]
s = Set()
for c in collect(CacheServerHandlers.conns)
if !isnothing(c.second.ws.lock.locked_by)
ws = c.first
push!(s, ws)
end
end
for ws in keys(locked)
ws in s || delete!(locked, ws)
end
for ws in s
locked[ws] = get(locked, ws, 0) + 1
if locked[ws] >= DELETE_AFTER_PERIODS[]
println(Dates.now(), " closing blocked websocket connection: $(ws.id)")
delete!(locked, ws)
HTTP.WebSockets.close(ws)
end
end
sleep(1+rand()*0.5)
end
end
end