Skip to content

Commit

Permalink
Merge pull request #49 from deeglaze/no_burst
Browse files Browse the repository at this point in the history
Make ratelimiting flag-configurable, set burst to 1
  • Loading branch information
deeglaze authored May 17, 2023
2 parents 8ac7f5f + 4547e2c commit cf84543
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions client/client_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package client

import (
"flag"
"fmt"
"time"

Expand All @@ -28,8 +29,13 @@ import (
const (
// defaultSevGuestDevicePath is the platform's usual device path to the SEV guest.
defaultSevGuestDevicePath = "/dev/sev-guest"
throttleDuration = 2 * time.Second
burstMax = 2
)

// These flags should not be needed for long term health of the project as the Linux kernel
// catches up with throttling-awareness.
var (
throttleDuration = flag.Duration("self_throttle_duration", 2*time.Second, "Rate-limit library-initiated device commands to this duration")
burstMax = flag.Int("self_throttle_burst", 1, "Rate-limit library-initiated device commands to this many commands per duration")
)

// LinuxDevice implements the Device interface with Linux ioctls.
Expand Down Expand Up @@ -83,16 +89,16 @@ func (d *LinuxDevice) Ioctl(command uintptr, req any) (uintptr, error) {
if d.burst == 0 {
sinceLast := time.Since(d.lastCmd)
// Self-throttle for tests without guest OS throttle detection
if sinceLast < throttleDuration {
time.Sleep(throttleDuration - sinceLast)
if sinceLast < *throttleDuration {
time.Sleep(*throttleDuration - sinceLast)
}
}
switch sreq := req.(type) {
case *labi.SnpUserGuestRequest:
abi := sreq.ABI()
result, _, errno := unix.Syscall(unix.SYS_IOCTL, uintptr(d.fd), command, uintptr(abi.Pointer()))
abi.Finish(sreq)
d.burst = (d.burst + 1) % burstMax
d.burst = (d.burst + 1) % *burstMax
if d.burst == 0 {
d.lastCmd = time.Now()
}
Expand Down

0 comments on commit cf84543

Please sign in to comment.