-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for rate limiting for inbound and outbound data streams of firecracker. This also introduces a TokenBuilder which can be passed into the NewRateLimiter factory function to return a new rate limiter.
- Loading branch information
Showing
4 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package firecracker | ||
|
||
import ( | ||
"time" | ||
|
||
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models" | ||
) | ||
|
||
// RateLimiterOpt represents a functional option for rate limiting construction | ||
type RateLimiterOpt func(*models.RateLimiter) | ||
|
||
// NewRateLimiter will construct a new RateLimiter with given parameters. | ||
func NewRateLimiter(bandwidth, ops models.TokenBucket, opts ...RateLimiterOpt) *models.RateLimiter { | ||
limiter := &models.RateLimiter{ | ||
Bandwidth: &bandwidth, | ||
Ops: &ops, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(limiter) | ||
} | ||
|
||
return limiter | ||
} | ||
|
||
// TokenBucketBuilder is a builder that allows of building components of the | ||
// models.RateLimiter structure. | ||
type TokenBucketBuilder struct { | ||
bucket models.TokenBucket | ||
} | ||
|
||
// WithBucketSize will set the bucket size for the given token bucket | ||
func (b TokenBucketBuilder) WithBucketSize(size int64) TokenBucketBuilder { | ||
b.bucket.Size = &size | ||
|
||
return b | ||
} | ||
|
||
// WithRefillDuration will set the given refill duration of the bucket fill rate. | ||
func (b TokenBucketBuilder) WithRefillDuration(dur time.Duration) TokenBucketBuilder { | ||
ms := dur.Nanoseconds() | ||
ms /= int64(time.Millisecond) | ||
b.bucket.RefillTime = &ms | ||
|
||
return b | ||
} | ||
|
||
// WithInitialSize will set the initial token bucket size | ||
func (b TokenBucketBuilder) WithInitialSize(size int64) TokenBucketBuilder { | ||
b.bucket.OneTimeBurst = &size | ||
|
||
return b | ||
} | ||
|
||
// Build will return a new token bucket | ||
func (b TokenBucketBuilder) Build() models.TokenBucket { | ||
return b.bucket | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package firecracker_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/firecracker-microvm/firecracker-go-sdk" | ||
models "github.com/firecracker-microvm/firecracker-go-sdk/client/models" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestRateLimiter(t *testing.T) { | ||
bucket := firecracker.TokenBucketBuilder{}. | ||
WithRefillDuration(1 * time.Hour). | ||
WithBucketSize(100). | ||
WithInitialSize(100). | ||
Build() | ||
|
||
expectedBucket := models.TokenBucket{ | ||
OneTimeBurst: firecracker.Int64(100), | ||
RefillTime: firecracker.Int64(3600000), | ||
Size: firecracker.Int64(100), | ||
} | ||
|
||
assert.Equal(t, expectedBucket, bucket) | ||
} | ||
|
||
func TestRateLimiter_RefillTime(t *testing.T) { | ||
cases := []struct { | ||
Name string | ||
Dur time.Duration | ||
ExpectedMilliseconds int64 | ||
}{ | ||
{ | ||
Name: "one hour", | ||
Dur: 1 * time.Hour, | ||
ExpectedMilliseconds: 3600000, | ||
}, | ||
{ | ||
Name: "zero", | ||
ExpectedMilliseconds: 0, | ||
}, | ||
} | ||
|
||
for _, c := range cases { | ||
t.Run(c.Name, func(t *testing.T) { | ||
bucket := firecracker.TokenBucketBuilder{}. | ||
WithRefillDuration(c.Dur). | ||
Build() | ||
|
||
assert.Equal(t, &c.ExpectedMilliseconds, bucket.RefillTime) | ||
}) | ||
} | ||
} |