mock: export ratelimiter as RateLimiter

Technically a breaking change because I changed the constructor casing but
nobody's using this outside of libp2p (yet), mostly because the type hasn't been
exported.
This commit is contained in:
Steven Allen 2018-12-18 21:15:32 -08:00
parent 894979f46f
commit 758b516603
3 changed files with 11 additions and 11 deletions

View File

@ -16,7 +16,7 @@ type link struct {
mock *mocknet mock *mocknet
nets []*peernet nets []*peernet
opts LinkOptions opts LinkOptions
ratelimiter *ratelimiter ratelimiter *RateLimiter
// this could have addresses on both sides. // this could have addresses on both sides.
sync.RWMutex sync.RWMutex
@ -25,7 +25,7 @@ type link struct {
func newLink(mn *mocknet, opts LinkOptions) *link { func newLink(mn *mocknet, opts LinkOptions) *link {
l := &link{mock: mn, l := &link{mock: mn,
opts: opts, opts: opts,
ratelimiter: NewRatelimiter(opts.Bandwidth)} ratelimiter: NewRateLimiter(opts.Bandwidth)}
return l return l
} }

View File

@ -487,7 +487,7 @@ func TestAdding(t *testing.T) {
} }
func TestRateLimiting(t *testing.T) { func TestRateLimiting(t *testing.T) {
rl := NewRatelimiter(10) rl := NewRateLimiter(10)
if !within(rl.Limit(10), time.Duration(float32(time.Second)), time.Millisecond/10) { if !within(rl.Limit(10), time.Duration(float32(time.Second)), time.Millisecond/10) {
t.Fail() t.Fail()

View File

@ -5,9 +5,9 @@ import (
"time" "time"
) )
// A ratelimiter is used by a link to determine how long to wait before sending // A RateLimiter is used by a link to determine how long to wait before sending
// data given a bandwidth cap. // data given a bandwidth cap.
type ratelimiter struct { type RateLimiter struct {
lock sync.Mutex lock sync.Mutex
bandwidth float64 // bytes per nanosecond bandwidth float64 // bytes per nanosecond
allowance float64 // in bytes allowance float64 // in bytes
@ -17,11 +17,11 @@ type ratelimiter struct {
duration time.Duration // total delay introduced due to rate limiting duration time.Duration // total delay introduced due to rate limiting
} }
// Creates a new ratelimiter with bandwidth (in bytes/sec) // Creates a new RateLimiter with bandwidth (in bytes/sec)
func NewRatelimiter(bandwidth float64) *ratelimiter { func NewRateLimiter(bandwidth float64) *RateLimiter {
// convert bandwidth to bytes per nanosecond // convert bandwidth to bytes per nanosecond
b := bandwidth / float64(time.Second) b := bandwidth / float64(time.Second)
return &ratelimiter{ return &RateLimiter{
bandwidth: b, bandwidth: b,
allowance: 0, allowance: 0,
maxAllowance: bandwidth, maxAllowance: bandwidth,
@ -29,8 +29,8 @@ func NewRatelimiter(bandwidth float64) *ratelimiter {
} }
} }
// Changes bandwidth of a ratelimiter and resets its allowance // Changes bandwidth of a RateLimiter and resets its allowance
func (r *ratelimiter) UpdateBandwidth(bandwidth float64) { func (r *RateLimiter) UpdateBandwidth(bandwidth float64) {
r.lock.Lock() r.lock.Lock()
defer r.lock.Unlock() defer r.lock.Unlock()
// Convert bandwidth from bytes/second to bytes/nanosecond // Convert bandwidth from bytes/second to bytes/nanosecond
@ -43,7 +43,7 @@ func (r *ratelimiter) UpdateBandwidth(bandwidth float64) {
} }
// Returns how long to wait before sending data with length 'dataSize' bytes // Returns how long to wait before sending data with length 'dataSize' bytes
func (r *ratelimiter) Limit(dataSize int) time.Duration { func (r *RateLimiter) Limit(dataSize int) time.Duration {
r.lock.Lock() r.lock.Lock()
defer r.lock.Unlock() defer r.lock.Unlock()
// update time // update time