Skip to content

Commit 0e8c9da

Browse files
committed
cosmetic changes and minor tweaks to rabbit_binding:{add,remove}
- align 'add' and 'remove' structurally, with the isomorphic final phases extracted into helper funs - call 'read' instead of 'match_object' to check for binding presence in 'remove' - cleaner and possibly slightly more efficient
1 parent 97b52fc commit 0e8c9da

File tree

1 file changed

+26
-38
lines changed

1 file changed

+26
-38
lines changed

src/rabbit_binding.erl

Lines changed: 26 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ exists(Binding) ->
124124

125125
add(Binding) -> add(Binding, fun (_Src, _Dst) -> ok end).
126126

127-
remove(Binding) -> remove(Binding, fun (_Src, _Dst) -> ok end).
128-
129127
add(Binding, InnerFun) ->
130128
binding_action(
131129
Binding,
@@ -134,56 +132,46 @@ add(Binding, InnerFun) ->
134132
%% in general, we want to fail on that in preference to
135133
%% anything else
136134
case InnerFun(Src, Dst) of
137-
ok -> add(Src, Dst, B);
135+
ok -> case mnesia:read({rabbit_route, B}) of
136+
[] -> add(Src, Dst, B);
137+
[_] -> fun rabbit_misc:const_ok/1
138+
end;
138139
{error, _} = Err -> rabbit_misc:const(Err)
139140
end
140141
end).
141142

142143
add(Src, Dst, B) ->
143-
case mnesia:read({rabbit_route, B}) of
144-
[] -> Durable = all_durable([Src, Dst]),
145-
case (not Durable orelse
146-
mnesia:read({rabbit_durable_route, B}) =:= []) of
147-
true -> ok = sync_binding(B, Durable, fun mnesia:write/3),
148-
fun (Tx) ->
149-
ok = rabbit_exchange:callback(
150-
Src, add_binding, [Tx, Src, B]),
151-
rabbit_event:notify_if(
152-
not Tx, binding_created, info(B))
153-
end;
154-
false -> rabbit_misc:const(not_found)
155-
end;
156-
[_] -> fun rabbit_misc:const_ok/1
144+
Durable = all_durable([Src, Dst]),
145+
case (not Durable orelse mnesia:read({rabbit_durable_route, B}) =:= []) of
146+
true -> ok = sync_binding(B, Durable, fun mnesia:write/3),
147+
fun (Tx) -> ok = rabbit_exchange:callback(Src, add_binding,
148+
[Tx, Src, B]),
149+
rabbit_event:notify_if(not Tx, binding_created,
150+
info(B))
151+
end;
152+
false -> rabbit_misc:const(not_found)
157153
end.
158154

155+
remove(Binding) -> remove(Binding, fun (_Src, _Dst) -> ok end).
156+
159157
remove(Binding, InnerFun) ->
160158
binding_action(
161159
Binding,
162160
fun (Src, Dst, B) ->
163-
Result =
164-
case mnesia:match_object(rabbit_route, #route{binding = B},
165-
write) of
166-
[] ->
167-
{error, binding_not_found};
168-
[_] ->
169-
case InnerFun(Src, Dst) of
170-
ok ->
171-
ok = sync_binding(B, all_durable([Src, Dst]),
172-
fun mnesia:delete_object/3),
173-
{ok, maybe_auto_delete(B#binding.source,
174-
[B], new_deletions())};
175-
{error, _} = E ->
176-
E
177-
end
178-
end,
179-
case Result of
180-
{error, _} = Err ->
181-
rabbit_misc:const(Err);
182-
{ok, Deletions} ->
183-
fun (Tx) -> ok = process_deletions(Deletions, Tx) end
161+
case mnesia:read(rabbit_route, B, write) of
162+
[] -> rabbit_misc:const({error, binding_not_found});
163+
[_] -> case InnerFun(Src, Dst) of
164+
ok -> remove(Src, Dst, B);
165+
{error, _} = Err -> rabbit_misc:const(Err)
166+
end
184167
end
185168
end).
186169

170+
remove(Src, Dst, B) ->
171+
ok = sync_binding(B, all_durable([Src, Dst]), fun mnesia:delete_object/3),
172+
Deletions = maybe_auto_delete(B#binding.source, [B], new_deletions()),
173+
fun (Tx) -> ok = process_deletions(Deletions, Tx) end.
174+
187175
list(VHostPath) ->
188176
VHostResource = rabbit_misc:r(VHostPath, '_'),
189177
Route = #route{binding = #binding{source = VHostResource,

0 commit comments

Comments
 (0)