-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathshackle_queue.erl
More file actions
80 lines (64 loc) · 1.84 KB
/
Copy pathshackle_queue.erl
File metadata and controls
80 lines (64 loc) · 1.84 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
-module(shackle_queue).
-compile(inline).
-compile({inline_size, 512}).
%% internal
-export([
add/5,
clear/2,
delete/1,
new/1,
remove/3,
table_name/1
]).
%% internal
-spec add(shackle:table(), shackle_server:id(), shackle:external_request_id(), shackle:cast(), reference()) ->
ok.
add(Table, ServerId, ExtRequestId, Cast, TimerRef) ->
Object = {{ServerId, ExtRequestId}, {Cast, TimerRef}},
ets:insert(Table, Object),
ok.
-spec clear(shackle:table(), shackle_server:id()) ->
[{shackle:cast(), reference()}].
clear(Table, ServerId) ->
Match = {{ServerId, '_'}, '_'},
case ets_match_take(Table, Match) of
[] ->
[];
Objects ->
[{Cast, TimerRef} || {_, {Cast, TimerRef}} <- Objects]
end.
-spec delete(shackle_pool:name()) ->
ok.
delete(PoolName) ->
ets:delete(table_name(PoolName)),
ok.
-spec new(shackle_pool:name()) ->
ok.
new(PoolName) ->
Table = ets:new(table_name(PoolName), shackle_utils:ets_options()),
ets:give_away(Table, whereis(shackle_ets_manager), undefined),
ok.
-spec remove(shackle:table(), shackle_server:id(), shackle:external_request_id()) ->
{ok, shackle:cast(), reference()} | {error, not_found}.
remove(Table, ServerId, ExtRequestId) ->
case ets_take(Table, {ServerId, ExtRequestId}) of
[] ->
{error, not_found};
[{_, {Cast, TimerRef}}] ->
{ok, Cast, TimerRef}
end.
%% private
ets_match_take(Table, Match) ->
case ets:match_object(Table, Match) of
[] ->
[];
Objects ->
ets:match_delete(Table, Match),
Objects
end.
ets_take(Table, Key) ->
ets:take(Table, Key).
-spec table_name(shackle_pool:name()) ->
shackle:table().
table_name(PoolName) ->
list_to_atom("shackle_queue_" ++ atom_to_list(PoolName)).