-
Notifications
You must be signed in to change notification settings - Fork 42
/
ocs_radius_acct_port_server.erl
571 lines (547 loc) · 20.3 KB
/
ocs_radius_acct_port_server.erl
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
%%% ocs_radius_acct_port_server.erl
%%% vim: ts=3
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @copyright 2016 - 2017 SigScale Global Inc.
%%% @end
%%% Licensed under the Apache License, Version 2.0 (the "License");
%%% you may not use this file except in compliance with the License.
%%% You may obtain a copy of the License at
%%%
%%% http://www.apache.org/licenses/LICENSE-2.0
%%%
%%% Unless required by applicable law or agreed to in writing, software
%%% distributed under the License is distributed on an "AS IS" BASIS,
%%% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%%% See the License for the specific language governing permissions and
%%% limitations under the License.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% @doc This {@link //stdlib/gen_server. gen_server} behaviour callback
%%% module receives {@link //radius. radius} messages on a port assigned
%%% for accounting in the {@link //ocs. ocs} application.
%%%
%%% @reference <a href="http://tools.ietf.org/rfc/rfc3579.txt">
%%% RFC3579 - RADIUS Support For EAP</a>
%%%
-module(ocs_radius_acct_port_server).
-copyright('Copyright (c) 2016 - 2017 SigScale Global Inc.').
-behaviour(gen_server).
%% export the call backs needed for gen_server behaviour
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-include_lib("radius/include/radius.hrl").
-include("ocs_eap_codec.hrl").
-include("ocs.hrl").
-record(state,
{acct_sup :: pid(),
disc_sup :: undefined | pid(),
address :: inet:ip_address(),
port :: non_neg_integer(),
handlers = gb_trees:empty() :: gb_trees:tree(Key ::
({NAS :: string() | inet:ip_address(), Port :: string(),
Peer :: string()}), Value :: (Fsm :: pid())),
disc_id = 1 :: integer()}).
-type state() :: #state{}.
%%----------------------------------------------------------------------
%% The ocs_radius_acct_port_server API
%%----------------------------------------------------------------------
%%----------------------------------------------------------------------
%% The ocs_radius_acct_port_server gen_server call backs
%%----------------------------------------------------------------------
-spec init(Args) -> Result
when
Args :: list(),
Result :: {ok, State}
| {ok, State, Timeout}
| {stop, Reason} | ignore,
State :: state(),
Timeout :: non_neg_integer() | infinity,
Reason :: term().
%% @doc Initialize the {@module} server.
%% Args :: [Sup :: pid(), Module :: atom(), Port :: non_neg_integer(),
%% Address :: inet:ip_address()].
%% @see //stdlib/gen_server:init/1
%% @private
%%
init([AcctSup, Address, Port, _Options]) ->
State = #state{address = Address, port = Port, acct_sup = AcctSup},
case ocs_log:acct_open() of
ok ->
process_flag(trap_exit, true),
{ok, State, 0};
{error, Reason} ->
{stop, Reason}
end.
-spec handle_call(Request, From, State) -> Result
when
Request :: term(),
From :: {Pid, Tag},
Pid :: pid(),
Tag :: any(),
State :: state(),
Result :: {reply, Reply, NewState}
| {reply, Reply, NewState, Timeout}
| {reply, Reply, NewState, hibernate}
| {noreply, NewState}
| {noreply, NewState, Timeout}
| {noreply, NewState, hibernate}
| {stop, Reason, Reply, NewState}
| {stop, Reason, NewState},
Reply :: term(),
NewState :: state(),
Timeout :: non_neg_integer() | infinity,
Reason :: term().
%% @doc Handle a request sent using {@link //stdlib/gen_server:call/2.
%% gen_server:call/2,3} or {@link //stdlib/gen_server:multi_call/2.
%% gen_server:multi_call/2,3,4}.
%% @see //stdlib/gen_server:handle_call/3
%% @private
handle_call(shutdown, _From, State) ->
{stop, normal, ok, State};
handle_call({request, Address, AccPort, Secret, ListenPort,
#radius{code = ?AccountingRequest} = Radius}, From, State) ->
request(Address, AccPort, Secret, ListenPort, Radius, From, State).
-spec handle_cast(Request, State) -> Result
when
Request :: term(),
State :: state(),
Result :: {noreply, NewState}
| {noreply, NewState, Timeout}
| {noreply, NewState, hibernate}
| {stop, Reason, NewState},
NewState :: state(),
Timeout :: non_neg_integer() | infinity,
Reason :: term().
%% @doc Handle a request sent using {@link //stdlib/gen_server:cast/2.
%% gen_server:cast/2} or {@link //stdlib/gen_server:abcast/2.
%% gen_server:abcast/2,3}.
%% @see //stdlib/gen_server:handle_cast/2
%% @private
%%
handle_cast(_Request, State) ->
{noreply, State}.
-spec handle_info(Info, State) -> Result
when
Info :: timeout | term(),
State :: state(),
Result :: {noreply, NewState}
| {noreply, NewState, Timeout}
| {noreply, NewState, hibernate}
| {stop, Reason, NewState},
NewState :: state(),
Timeout :: non_neg_integer() | infinity,
Reason :: term().
%% @doc Handle a received message.
%% @see //stdlib/gen_server:handle_info/2
%% @private
%%
handle_info(timeout, #state{acct_sup = AcctSup} = State) ->
Children = supervisor:which_children(AcctSup),
{_, DiscSup, _, _} = lists:keyfind(ocs_radius_disconnect_fsm_sup, 1, Children),
{noreply, State#state{disc_sup = DiscSup}};
handle_info({'EXIT', _Pid, {shutdown, SessionId}},
#state{handlers = Handlers} = State) ->
NewHandlers = gb_trees:delete(SessionId, Handlers),
NewState = State#state{handlers = NewHandlers},
{noreply, NewState};
handle_info({'EXIT', Fsm, _Reason},
#state{handlers = Handlers} = State) ->
Fdel = fun(_F, {Key, Pid, _Iter}) when Pid == Fsm ->
Key;
(F, {_Key, _Val, Iter}) ->
F(F, gb_trees:next(Iter));
(_F, none) ->
none
end,
Iter = gb_trees:iterator(Handlers),
case Fdel(Fdel, gb_trees:next(Iter)) of
none ->
{noreply, State};
Key ->
NewHandlers = gb_trees:delete(Key, Handlers),
NewState = State#state{handlers = NewHandlers},
{noreply, NewState}
end.
-spec terminate(Reason, State) -> any()
when
Reason :: normal | shutdown | term(),
State :: state().
%% @doc Cleanup and exit.
%% @see //stdlib/gen_server:terminate/3
%% @private
%%
terminate(_Reason, _State) ->
ocs_log:acct_close().
-spec code_change(OldVsn, State, Extra) -> Result
when
OldVsn :: (Vsn | {down, Vsn}),
Vsn :: term(),
State :: state(),
Extra :: term(),
Result :: {ok, NewState},
NewState :: state().
%% @doc Update internal state data during a release upgrade/downgrade.
%% @see //stdlib/gen_server:code_change/3
%% @private
%%
code_change(_OldVsn, State, _Extra) ->
{ok, State}.
%%----------------------------------------------------------------------
%% internal functions
%%----------------------------------------------------------------------
-spec request(Address, Port, Secret, ListenPort, Radius, From, State) -> Result
when
Address :: inet:ip_address(),
Port :: pos_integer(),
Secret :: string(),
ListenPort :: pos_integer(),
Radius :: #radius{},
From :: {Pid, Tag},
Pid :: pid(),
Tag :: term(),
State :: state(),
Result :: {reply, {ok, wait}, NewState}
| {reply, {error, ignore}, NewState},
NewState :: state().
%% @doc Handle a received RADIUS Accounting Request packet.
%% @private
request(Address, AccPort, Secret,
ListenPort, Radius, {_RadiusFsm, _Tag} = _From, State) ->
try
#radius{code = ?AccountingRequest, id = Id, attributes = Attributes,
authenticator = Authenticator} = Radius,
AttrBin = radius_attributes:codec(Attributes),
Length = size(AttrBin) + 20,
CalcAuth = crypto:hash(md5, [<<?AccountingRequest, Id, Length:16>>,
<<0:128>>, AttrBin, Secret]),
CalcAuth = list_to_binary(Authenticator),
{ok, AcctStatusType} = radius_attributes:find(?AcctStatusType, Attributes),
NasIpAddressV = radius_attributes:find(?NasIpAddress, Attributes),
NasIdentifierV = radius_attributes:find(?NasIdentifier, Attributes),
NasId = case {NasIpAddressV, NasIdentifierV} of
{{error, not_found}, {error, not_found}} ->
throw(reject);
{Value, {error, not_found}} ->
Value;
{_, {ok, Value}} ->
Value
end,
{error, not_found} = radius_attributes:find(?UserPassword, Attributes),
{error, not_found} = radius_attributes:find(?ChapPassword, Attributes),
{error, not_found} = radius_attributes:find(?ReplyMessage, Attributes),
{error, not_found} = radius_attributes:find(?State, Attributes),
{ok, AcctSessionId} = radius_attributes:find(?AcctSessionId, Attributes),
request1(AcctStatusType, AcctSessionId, Id, Authenticator,
Secret, NasId, Address, AccPort, ListenPort, Attributes, State)
catch
_:_ ->
{reply, {error, ignore}, State}
end.
%% @hidden
request1(?AccountingStart, _AcctSessionId, Id,
Authenticator, Secret, _NasId, _Address, _AccPort, _ListenPort, Attributes,
#state{address = ServerAddress, port = ServerPort} = State) ->
ok = ocs_log:acct_log(radius, {ServerAddress, ServerPort}, start, Attributes),
{reply, {ok, response(Id, Authenticator, Secret)}, State};
request1(?AccountingStop, AcctSessionId, Id,
Authenticator, Secret, NasId, Address, _AccPort, ListenPort, Attributes,
#state{address = ServerAddress, port = ServerPort} = State) ->
InOctets = radius_attributes:find(?AcctInputOctets, Attributes),
OutOctets = radius_attributes:find(?AcctOutputOctets, Attributes),
Usage = case {InOctets, OutOctets} of
{{error, not_found}, {error, not_found}} ->
0;
{{ok,In}, {ok,Out}} ->
In + Out
end,
{ok, UserName} = radius_attributes:find(?UserName, Attributes),
ok = ocs_log:acct_log(radius, {ServerAddress, ServerPort}, stop, Attributes),
Subscriber = ocs:normalize(UserName),
case update_sub_values(Subscriber, Usage, AcctSessionId, NasId, Attributes) of
{ok, OverUsed, false} when OverUsed =< 0 ->
case client_disconnect_supported(Address) of
true ->
start_disconnect(AcctSessionId, Id, Authenticator, Secret,
ListenPort, NasId, Address, Attributes, Subscriber, State);
false ->
error_logger:warning_report(["Client does not support disconnect mesages",
{module, ?MODULE}, {subscriber, Subscriber},
{username, UserName}, {nas, NasId}, {address, Address},
{session, AcctSessionId}]),
{reply, {ok, response(Id, Authenticator, Secret)}, State}
end;
{ok, _SufficientBalance, _Flag} ->
{reply, {ok, response(Id, Authenticator, Secret)}, State};
{error, not_found} ->
error_logger:warning_report(["Accounting subscriber not found",
{module, ?MODULE}, {subscriber, Subscriber},
{username, UserName}, {nas, NasId}, {address, Address},
{session, AcctSessionId}]),
{reply, {ok, response(Id, Authenticator, Secret)}, State}
end;
request1(?AccountingInterimUpdate, AcctSessionId, Id,
Authenticator, Secret, NasId, Address, _AccPort, ListenPort, Attributes,
#state{address = ServerAddress, port = ServerPort} = State) ->
InOctets = radius_attributes:find(?AcctInputOctets, Attributes),
OutOctets = radius_attributes:find(?AcctOutputOctets, Attributes),
Usage = case {InOctets, OutOctets} of
{{error, not_found}, {error, not_found}} ->
0;
{{ok,In}, {ok,Out}} ->
In + Out
end,
{ok, UserName} = radius_attributes:find(?UserName, Attributes),
ok = ocs_log:acct_log(radius, {ServerAddress, ServerPort}, interim, Attributes),
Subscriber = ocs:normalize(UserName),
case ocs:find_subscriber(Subscriber) of
{ok, #subscriber{buckets = Buckets, enabled = Enabled}} ->
case {Enabled, get_balance(Buckets)} of
{Enabled, Balance} when Enabled == false; Balance =< Usage ->
case client_disconnect_supported(Address) of
true ->
start_disconnect(AcctSessionId, Id, Authenticator, Secret,
ListenPort, NasId, Address, Attributes, Subscriber, State);
false ->
{reply, {ok, response(Id, Authenticator, Secret)}, State}
end;
_ ->
{reply, {ok, response(Id, Authenticator, Secret)}, State}
end;
{error, not_found} ->
error_logger:warning_report(["Accounting subscriber not found",
{module, ?MODULE}, {subscriber, Subscriber},
{username, UserName}, {nas, NasId}, {address, Address},
{session, AcctSessionId}]),
{reply, {ok, response(Id, Authenticator, Secret)}, State}
end;
request1(?AccountingON, _AcctSessionId, Id,
Authenticator, Secret, _NasId, _Address, _AccPort, _ListenPort, Attributes,
#state{address = ServerAddress, port = ServerPort} = State) ->
ok = ocs_log:acct_log(radius, {ServerAddress, ServerPort}, on, Attributes),
{reply, {ok, response(Id, Authenticator, Secret)}, State};
request1(?AccountingOFF, _AcctSessionId, Id,
Authenticator, Secret, _NasId, _Address, _AccPort, _ListenPort, Attributes,
#state{address = ServerAddress, port = ServerPort} = State) ->
ok = ocs_log:acct_log(radius, {ServerAddress, ServerPort}, off, Attributes),
{reply, {ok, response(Id, Authenticator, Secret)}, State};
request1(_AcctStatusType, _AcctSessionId, _Id, _Authenticator,
_Secret, _NasId, _Address, _Port, _ListenPort, _Attributes, State) ->
{reply, {error, ignore}, State}.
-spec response(Id, RequestAuthenticator, Secret) -> AccessAccept
when
Id :: byte(),
RequestAuthenticator :: [byte()],
Secret :: string() | binary(),
AccessAccept :: binary().
%% @hidden
response(Id, RequestAuthenticator, Secret) ->
Length = 20,
ResponseAuthenticator = crypto:hash(md5, [<<?AccountingResponse, Id,
Length:16>>, RequestAuthenticator, Secret]),
Response = #radius{code = ?AccountingResponse, id = Id,
authenticator = ResponseAuthenticator, attributes = []},
radius:codec(Response).
-spec update_sub_values(Subscriber, Usage, AcctSessionID, Nas, Attributes) ->
Result
when
Subscriber :: string() | binary(),
Usage :: non_neg_integer(),
AcctSessionID :: integer(),
Nas :: string() | inet:ip_address(),
Attributes :: radius_attributes:attributes(),
Result :: {ok, NewBalance, DiscFlag} | {error, Reason},
Reason :: term(),
NewBalance :: integer(),
DiscFlag :: boolean().
%% @doc Decrements subscriber's current balance
update_sub_values(Subscriber, Usage, AcctSessionID, Nas, Attributes)
when is_list(Subscriber) ->
update_sub_values(list_to_binary(Subscriber), Usage, AcctSessionID, Nas,
Attributes);
update_sub_values(Subscriber, Usage, AcctSessionID, Nas, Attributes)
when is_binary(Subscriber), Usage >= 0 ->
F = fun() ->
case mnesia:read(subscriber, Subscriber, write) of
[#subscriber{buckets = Buckets, disconnect = Flag,
session_attributes = SessionList} = Entry] ->
UpdatedBuckets = update_buckets(Buckets, Usage),
NewEntry = case remove_session(SessionList, AcctSessionID, Nas,
Subscriber, Attributes) of
{ok, NewSessionList} ->
Entry#subscriber{buckets = UpdatedBuckets,
session_attributes = NewSessionList};
{error, SessionList} ->
Entry#subscriber{buckets = UpdatedBuckets,
session_attributes = SessionList}
end,
mnesia:write(subscriber, NewEntry, write),
NewBalance = get_balance(UpdatedBuckets),
{NewBalance, Flag};
[] ->
throw(not_found)
end
end,
case mnesia:transaction(F) of
{atomic, {NewBalance, Flag}} ->
{ok, NewBalance, Flag};
{aborted, {throw, Reason}} ->
{error, Reason};
{aborted, Reason} ->
error_logger:error_report(["Failed to decrement balance",
{error, Reason}, {subscriber, Subscriber}]),
{error, Reason}
end.
-spec start_disconnect(AcctSessionId, Id, Authenticator, Secret,
ListenPort,NasId, Address, Attributes, Subscriber, State) -> Result
when
AcctSessionId :: integer(),
Id :: byte(),
Authenticator :: [byte()],
Secret :: string(),
ListenPort :: non_neg_integer(),
NasId :: inet:ip_address() | string(),
Address :: inet:ip_address(),
Attributes :: [binary()],
Subscriber :: [byte()],
State :: #state{},
Result :: {reply, {ok, Response}, NewState},
Response :: binary(),
NewState :: #state{}.
%% @doc Start a disconnect_fsm worker.
start_disconnect(AcctSessionId, Id, Authenticator, Secret, ListenPort,
NasId, Address, Attributes, Subscriber, #state{handlers = Handlers,
disc_sup = DiscSup, disc_id = DiscId} = State) ->
case gb_trees:lookup({NasId, Subscriber, AcctSessionId}, Handlers) of
{value, _DiscPid} ->
{reply, {ok, response(Id, Authenticator, Secret)}, State};
none ->
DiscArgs = [Address, NasId, Subscriber,
AcctSessionId, Secret, ListenPort, Attributes, Id],
StartArgs = [DiscArgs, []],
case supervisor:start_child(DiscSup, StartArgs) of
{ok, DiscFsm} ->
link(DiscFsm),
NewHandlers = gb_trees:insert({NasId, Subscriber, AcctSessionId},
DiscFsm, Handlers),
NewDiscId = DiscId + 1,
NewState = State#state{handlers = NewHandlers,
disc_id = NewDiscId},
{reply, {ok, response(Id, Authenticator, Secret)}, NewState};
{error, Reason} ->
error_logger:error_report(["Failed to initiate session disconnect function",
{module, ?MODULE}, {subscriber, Subscriber}, {nas, NasId},
{address, Address}, {session, AcctSessionId}, {error, Reason}]),
{reply, {ok, response(Id, Authenticator, Secret)}, State}
end
end.
-spec client_disconnect_supported(Client) -> Result
when
Client :: inet:ip_address(),
Result :: boolean().
%% @doc Checks whether the client supports receiving RADIUS disconnect messages.
%% Value of 0 in port field in client table indicates that the client does not
%% support disconnect messages.
client_disconnect_supported(Client) ->
{ok, #client{port = Port}} = ocs:find_client(Client),
case Port of
0 ->
false;
_ ->
true
end.
-spec remove_session(SessionList, AcctSessionID, Nas, Subscriber, Attributes) ->
Result
when
SessionList :: [radius_attributes:attributes()],
AcctSessionID :: integer(),
Nas :: string() | inet:ip_address(),
Subscriber :: string() | binary(),
Attributes :: radius_attributes:attributes(),
Result :: {ok, NewSessionList} | {error, SessionList},
NewSessionList :: [] | [radius_attributes:attributes()].
%% @doc From `SessionList' remove a session if it includes certain RADIUS
%% session related attributes. These attributes can include Acct-Session-Id,
%% Nas-Identifier, Nas-IP-Address, User-Name, Calling-Station-Id.
%% @hidden
remove_session(SessionList, AcctSessionID, Nas, Subscriber, Attributes)
when is_binary(Subscriber) ->
remove_session(SessionList, AcctSessionID, Nas, binary_to_list(Subscriber),
Attributes);
remove_session(SessionList, AcctSessionID, Nas, Subscriber, Attributes) ->
Attributes1 = [{?AcctSessionId, AcctSessionID}],
Attributes2 = [{?NasIdentifier, Nas}, {?UserName, Subscriber}],
Attributes3 = [{?NasIpAddress, Nas}, {?UserName, Subscriber}],
CandidateAttributes = [Attributes1, Attributes2, Attributes3],
remove_session1(SessionList, CandidateAttributes, Nas, Subscriber, Attributes).
% @hidden
remove_session1(SessionList, [H | T], N, S, A) ->
case find_session(SessionList, H) of
not_found ->
remove_session1(SessionList, T, N, S, A);
Session ->
{ok, lists:delete(Session, SessionList)}
end;
remove_session1(SessionList, [], N, S ,A) ->
error_logger:error_report(["Failed to remove current session from subscriber",
{subscriber, S}, {nas, N}, {attributes, A}]),
{error, SessionList}.
-spec find_session(SessionList, Attributes) -> Result
when
SessionList :: [radius_attributes:attributes()],
Attributes :: radius_attributes:attributes(),
Result :: Session | not_found,
Session :: radius_attributes:attributes().
%% @doc Find all the attributes in `Attributes' in any member of
%% `SessionList'.
%% @hidden
find_session([H | T], Attributes) ->
F = fun(F, H1, [H2 | T2]) ->
case lists:member(H2, H1) of
true ->
F(F, H1, T2);
false ->
not_found
end;
(_, H1, []) ->
H1
end,
case F(F, H, Attributes) of
not_found ->
find_session(T, Attributes);
Session ->
Session
end;
find_session([], _) ->
not_found.
-spec get_balance(Buckets) ->
Balance when
Buckets :: [#bucket{}],
Balance :: integer().
%% get the availabel balance form buckets
get_balance([]) ->
0;
get_balance(Buckets) ->
get_balance1(Buckets, 0).
%% @hidden
get_balance1([], Balance) ->
Balance;
get_balance1([#bucket{remain_amount = #remain_amount{amount = RemAmnt}}
| Tail], Balance) ->
get_balance1(Tail, RemAmnt + Balance).
-spec update_buckets(Buckets, Usage) ->
UpdatedBuckets when
Buckets :: [#bucket{}],
Usage :: integer(),
UpdatedBuckets :: [#bucket{}].
%% @doc Decrement bucket balances and return new available buckets
update_buckets([], _Usage) ->
[];
update_buckets([#bucket{remain_amount = #remain_amount{amount = RemAmount} = RM} = Bucket |
Tail], Usage) ->
RemUsage = RemAmount - Usage,
case RemUsage of
RU when RU < 0 ->
update_buckets(Tail, RU);
_ ->
UpdatedBucket = Bucket#bucket{remain_amount = RM#remain_amount{amount = RemUsage}},
[UpdatedBucket | Tail]
end.