Skip to content

Commit 8f01b72

Browse files
committed
rate-limit: Adjust token costs of RPC calls
Rather than have the same token costs for all calls, we want to rate limit clients based on how expensive the functions they are calling are. I measured the average runtime of most xapi calls, which are stored in a hashtable and are used as the token cost for each call. This does not account for any overheads, and will need to be adjusted - it is merely a starting point. We also use the median as a default. Signed-off-by: Christian Pardillo Laursen <[email protected]>
1 parent 18c5764 commit 8f01b72

File tree

4 files changed

+659
-6
lines changed

4 files changed

+659
-6
lines changed

ocaml/xapi/server_helpers.ml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ let do_dispatch ?session_id ?forward_op ?self:_ supports_async called_fn_name
188188
Rate_limit.Bucket_table.peek Xapi_rate_limit.bucket_table ~user_agent
189189
)
190190
in
191+
let token_cost = Xapi_rate_limit.get_token_cost called_fn_name in
191192
let handle_request () =
192193
match sync_ty with
193194
| `Sync ->
@@ -205,19 +206,19 @@ let do_dispatch ?session_id ?forward_op ?self:_ supports_async called_fn_name
205206
match peek_result with
206207
| Some tokens -> (
207208
D.debug
208-
"Bucket table: Expecting to consume 1 token from user_agent %s \
209-
with available tokens %f"
210-
user_agent tokens ;
209+
"Bucket table: Expecting to consume %f tokens from user_agent %s \
210+
with available tokens %f in function %s"
211+
token_cost user_agent tokens __FUNCTION__ ;
211212
match sync_ty with
212213
| `Sync ->
213214
Rate_limit.Bucket_table.submit_sync Xapi_rate_limit.bucket_table
214-
~user_agent ~callback:sync 1.
215+
~user_agent ~callback:sync token_cost
215216
| `Async ->
216217
let need_complete = not (Context.forwarded_task __context) in
217218
Rate_limit.Bucket_table.submit Xapi_rate_limit.bucket_table
218219
~user_agent
219220
~callback:(fun () -> async ~need_complete)
220-
1. ;
221+
token_cost ;
221222
Rpc.success (API.rpc_of_ref_task (Context.get_task_id __context))
222223
| `InternalAsync ->
223224
async ~need_complete:true ;

ocaml/xapi/xapi_http.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ let add_handler (name, handler) =
361361
| Some user_agent ->
362362
debug "Rate limiting handler %s with user_agent %s" name user_agent ;
363363
Rate_limit.Bucket_table.submit Xapi_rate_limit.bucket_table
364-
~user_agent ~callback:handler 1.0
364+
~user_agent ~callback:handler Xapi_rate_limit.median_token_cost
365365
in
366366
let h req ic () =
367367
let client =

0 commit comments

Comments
 (0)