Skip to content

Instantly share code, notes, and snippets.

@fernandezpablo85
Created March 1, 2023 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fernandezpablo85/18d2a52797e6e3fd2be0167699f7b7ce to your computer and use it in GitHub Desktop.
Save fernandezpablo85/18d2a52797e6e3fd2be0167699f7b7ce to your computer and use it in GitHub Desktop.
-------------------------------- MODULE throttler --------------------------------
EXTENDS TLC, Integers, Sequences
MaxEpoch == 20
RequestLimit == 5
TimeSpan == 1
Min(seq) ==
LET
ix == 1..Len(seq)
i == CHOOSE x \in ix: \A y \in ix: seq[x] <= seq[y]
IN seq[i]
TooManyRequests(seq, epoch) ==
/\ Len(seq) >= RequestLimit
/\ Min(seq) >= epoch - TimeSpan
CircularBufferAppend(seq, newElement) ==
IF Len(seq) < RequestLimit
THEN Append(seq, newElement)
ELSE Tail(seq) \o <<newElement>>
(* --algorithm throttler
variables
epoch = 0;
mem = <<>>;
ready = TRUE;
revived = FALSE;
define
TypeOk ==
/\ epoch \in 0..MaxEpoch
/\ \A i \in 1..Len(mem): mem[i] <= epoch
RequestsEventuallyUnblock == ~ready ~> ~[]ready
end define;
fair+ process time = 0
begin
Tick:
while epoch < MaxEpoch do
epoch := epoch + 1;
end while;
end process;
fair+ process requests = 1
begin
Request:
while epoch < MaxEpoch do
if TooManyRequests(mem, epoch) then
Wait:
ready := FALSE;
else
Act:
ready := TRUE;
mem := CircularBufferAppend(mem, epoch);
end if;
end while;
end process;
end algorithm; *)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment