Skip to content

Commit 8a1cd4e

Browse files
author
hhaensel
committed
prevent race condition during registation and deletion of routes/channels
1 parent 5ff9b6b commit 8a1cd4e

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

src/Router.jl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ end
105105

106106
const _routes = OrderedCollections.LittleDict{Symbol,Route}()
107107
const _channels = OrderedCollections.LittleDict{Symbol,Channel}()
108+
const _routes_lock = ReentrantLock()
109+
const _channels_lock = ReentrantLock()
108110

109111

110112
"""
@@ -230,8 +232,16 @@ function route_ws_request(req, msg::Union{String,Vector{UInt8}}, ws_client) :: S
230232
end
231233

232234

233-
function Base.push!(collection, name::Symbol, item::Union{Route,Channel})
234-
Base.delete!(collection, name)[name] = item # this is to ensure that the item is always the last one in the collection
235+
function Base.push!(collection::OrderedCollections.LittleDict{Symbol,Route}, name::Symbol, item::Route)
236+
lock(_routes_lock) do
237+
Base.delete!(collection, name)[name] = item # this is to ensure that the item is always the last one in the collection
238+
end
239+
end
240+
241+
function Base.push!(collection::OrderedCollections.LittleDict{Symbol,Channel}, name::Symbol, item::Channel)
242+
lock(_channels_lock) do
243+
Base.delete!(collection, name)[name] = item # this is to ensure that the item is always the last one in the collection
244+
end
235245
end
236246

237247

@@ -386,7 +396,9 @@ end
386396
Removes the route with the corresponding name from the routes collection and returns the collection of remaining routes.
387397
"""
388398
function delete!(key::Symbol) :: Vector{Route}
389-
OrderedCollections.delete!(_routes, key)
399+
lock(_routes_lock) do
400+
OrderedCollections.delete!(_routes, key)
401+
end
390402
return routes()
391403
end
392404

@@ -397,7 +409,9 @@ end
397409
Removes the channel with the corresponding name from the channels collection and returns the collection of remaining channels.
398410
"""
399411
function delete_channel!(key::Symbol) :: Vector{Channel}
400-
OrderedCollections.delete!(_channels, key)
412+
lock(_channels_lock) do
413+
OrderedCollections.delete!(_channels, key)
414+
end
401415
return channels()
402416
end
403417

0 commit comments

Comments
 (0)