Skip to content

Commit fb93ab8

Browse files
committed
idl: Add Rate_limit datamodel
Signed-off-by: Christian Pardillo Laursen <[email protected]>
1 parent 32092aa commit fb93ab8

18 files changed

+125
-42
lines changed

ocaml/idl/datamodel.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10535,6 +10535,7 @@ let all_system =
1053510535
; Datamodel_vm_group.t
1053610536
; Datamodel_host_driver.t
1053710537
; Datamodel_driver_variant.t
10538+
; Datamodel_rate_limit.t
1053810539
]
1053910540

1054010541
(* If the relation is one-to-many, the "many" nodes (one edge each) must come before the "one" node (many edges) *)
@@ -10786,6 +10787,7 @@ let expose_get_all_messages_for =
1078610787
; _observer
1078710788
; _host_driver
1078810789
; _driver_variant
10790+
; _rate_limit
1078910791
]
1079010792

1079110793
let no_task_id_for = [_task; (* _alert; *) _event]

ocaml/idl/datamodel_common.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,8 @@ let _host_driver = "Host_driver"
315315

316316
let _driver_variant = "Driver_variant"
317317

318+
let _rate_limit = "Rate_limit"
319+
318320
let update_guidances =
319321
Enum
320322
( "update_guidances"

ocaml/idl/datamodel_lifecycle.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
let prototyped_of_class = function
2+
| "Rate_limit" ->
3+
Some "25.38.0-next"
24
| "Driver_variant" ->
35
Some "25.2.0"
46
| "Host_driver" ->
@@ -13,6 +15,14 @@ let prototyped_of_class = function
1315
None
1416

1517
let prototyped_of_field = function
18+
| "Rate_limit", "fill_rate" ->
19+
Some "25.38.0-next"
20+
| "Rate_limit", "burst_size" ->
21+
Some "25.38.0-next"
22+
| "Rate_limit", "client_id" ->
23+
Some "25.38.0-next"
24+
| "Rate_limit", "uuid" ->
25+
Some "25.38.0-next"
1626
| "Driver_variant", "status" ->
1727
Some "25.2.0"
1828
| "Driver_variant", "priority" ->

ocaml/idl/datamodel_rate_limit.ml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
(*
2+
* Copyright (C) 2023 Cloud Software Group
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU Lesser General Public License as published
6+
* by the Free Software Foundation; version 2.1 only. with the special
7+
* exception on linking described in file LICENSE.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU Lesser General Public License for more details.
13+
*)
14+
15+
open Datamodel_types
16+
open Datamodel_common
17+
open Datamodel_roles
18+
19+
let lifecycle = []
20+
21+
let t =
22+
create_obj ~name:_rate_limit ~descr:"Rate limiting policy for a XAPI client"
23+
~doccomments:[] ~gen_constructor_destructor:true ~gen_events:true
24+
~in_db:true ~lifecycle:[] ~persist:PersistEverything ~in_oss_since:None
25+
~messages_default_allowed_roles:_R_POOL_ADMIN
26+
~contents:
27+
([uid _rate_limit ~lifecycle]
28+
@ [
29+
field ~qualifier:StaticRO ~ty:String ~lifecycle "client_id"
30+
"An identifier for the rate limited client" ~ignore_foreign_key:true
31+
~default_value:(Some (VString ""))
32+
; field ~qualifier:StaticRO ~ty:Float ~lifecycle "burst_size"
33+
"Amount of tokens that can be consumed in one burst"
34+
~ignore_foreign_key:true ~default_value:(Some (VFloat 0.))
35+
; field ~qualifier:StaticRO ~ty:Float ~lifecycle "fill_rate"
36+
"Tokens added to token bucket per second" ~ignore_foreign_key:true
37+
~default_value:(Some (VFloat 0.))
38+
]
39+
)
40+
~messages:[] ()

ocaml/idl/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
datamodel_values datamodel_schema datamodel_certificate
88
datamodel_diagnostics datamodel_repository datamodel_lifecycle
99
datamodel_vtpm datamodel_observer datamodel_vm_group api_version
10-
datamodel_host_driver datamodel_driver_variant)
10+
datamodel_host_driver datamodel_driver_variant datamodel_rate_limit)
1111
(libraries
1212
rpclib.core
1313
sexplib0

ocaml/idl/schematest.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ let hash x = Digest.string x |> Digest.to_hex
33
(* BEWARE: if this changes, check that schema has been bumped accordingly in
44
ocaml/idl/datamodel_common.ml, usually schema_minor_vsn *)
55

6-
let last_known_schema_hash = "3b20f4304cfaaa7b6213af91ae632e64"
6+
let last_known_schema_hash = "4708cb1f0cf7c1231c6958590ee1ed04"
77

88
let current_schema_hash : string =
99
let open Datamodel_types in

ocaml/libs/rate-limit/bucket_table.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ let with_lock = Xapi_stdext_threads.Threadext.Mutex.execute
3131

3232
let create () = Atomic.make StringMap.empty
3333

34+
let mem t ~user_agent =
35+
let map = Atomic.get t in
36+
StringMap.mem user_agent map
37+
3438
(* The worker thread is responsible for calling the callback when the token
3539
amount becomes available *)
3640
let rec worker_loop ~bucket ~process_queue ~process_queue_lock

ocaml/libs/rate-limit/bucket_table.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,11 @@ val add_bucket :
2222
t -> user_agent:string -> burst_size:float -> fill_rate:float -> bool
2323
(** [add_bucket table ~user_agent ~burst_size ~fill_rate] adds a token bucket
2424
for the given user agent. Returns [false] if a bucket already exists, or if
25-
the bucket configuration is invalid, e.g. negative fill rate. *)
25+
the bucket configuration is invalid, e.g. negative/zero fill rate. *)
26+
27+
val mem : t -> user_agent:string -> bool
28+
(** [mem table ~user_agent] returns whether [user_agent] has an associated
29+
token bucket in the bucket table *)
2630

2731
val peek : t -> user_agent:string -> float option
2832
(** [peek table ~user_agent] returns the current token count for the user agent,

ocaml/libs/rate-limit/test/test_token_bucket.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ let test_sleep () =
122122
let tb = Option.get (Token_bucket.create ~burst_size:20.0 ~fill_rate:5.0) in
123123
let _ = Token_bucket.consume tb 10.0 in
124124
Thread.delay 1.0 ;
125-
Alcotest.(check (float 0.2))
125+
Alcotest.(check (float 0.5))
126126
"Sleep 1 should refill token bucket by fill_rate" 15.0 (Token_bucket.peek tb)
127127

128128
let test_system_time_versions () =

ocaml/libs/uuid/uuidx.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type without_secret =
6464
| `sr_stat
6565
| `subject
6666
| `task
67+
| `Rate_limit
6768
| `tunnel
6869
| `USB_group
6970
| `user

0 commit comments

Comments
 (0)