Skip to content

Commit 339b5bf

Browse files
Merge pull request #1900 from rabbitmq/binding-lock-optimisation
Less coarse-grained locking for bindings add/remove operations
2 parents be587ad + e494ea4 commit 339b5bf

File tree

1 file changed

+8
-10
lines changed

1 file changed

+8
-10
lines changed

src/rabbit_binding.erl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,8 @@ add(Binding, InnerFun, ActingUser) ->
154154
fun (Src, Dst, B) ->
155155
case rabbit_exchange:validate_binding(Src, B) of
156156
ok ->
157-
lock_resource(Src),
158-
lock_resource(Dst),
157+
lock_resource(Src, read),
158+
lock_resource(Dst, read),
159159
%% this argument is used to check queue exclusivity;
160160
%% in general, we want to fail on that in preference to
161161
%% anything else
@@ -174,8 +174,6 @@ add(Binding, InnerFun, ActingUser) ->
174174
end, fun not_found_or_absent_errs/1).
175175

176176
add(Src, Dst, B, ActingUser) ->
177-
lock_resource(Src),
178-
lock_resource(Dst),
179177
[SrcDurable, DstDurable] = [durable(E) || E <- [Src, Dst]],
180178
ok = sync_route(#route{binding = B}, SrcDurable, DstDurable,
181179
fun mnesia:write/3),
@@ -198,8 +196,8 @@ remove(Binding, InnerFun, ActingUser) ->
198196
binding_action(
199197
Binding,
200198
fun (Src, Dst, B) ->
201-
lock_resource(Src),
202-
lock_resource(Dst),
199+
lock_resource(Src, read),
200+
lock_resource(Dst, read),
203201
case mnesia:read(rabbit_route, B, write) of
204202
[] -> case mnesia:read(rabbit_durable_route, B, write) of
205203
[] -> rabbit_misc:const(ok);
@@ -216,8 +214,6 @@ remove(Binding, InnerFun, ActingUser) ->
216214
end, fun absent_errs_only/1).
217215

218216
remove(Src, Dst, B, ActingUser) ->
219-
lock_resource(Src),
220-
lock_resource(Dst),
221217
ok = sync_route(#route{binding = B}, durable(Src), durable(Dst),
222218
fun delete/3),
223219
Deletions = maybe_auto_delete(
@@ -536,9 +532,11 @@ remove_for_destination(DstName, OnlyDurable, Fun) ->
536532

537533
%% Instead of locking entire table on remove operations we can lock the
538534
%% affected resource only.
539-
lock_resource(Name) ->
535+
lock_resource(Name) -> lock_resource(Name, write).
536+
537+
lock_resource(Name, LockKind) ->
540538
mnesia:lock({global, Name, mnesia:table_info(rabbit_route, where_to_write)},
541-
write).
539+
LockKind).
542540

543541
%% Requires that its input binding list is sorted in exchange-name
544542
%% order, so that the grouping of bindings (for passing to

0 commit comments

Comments
 (0)