Skip to content

Commit 288df5b

Browse files
committed
allow NewLimiter to pass limits
1 parent c0ff750 commit 288df5b

3 files changed

Lines changed: 20 additions & 13 deletions

File tree

limiter.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ type Limiter struct {
1515
Writes *Operation
1616
}
1717

18-
func NewLimiter(ctx context.Context) *Limiter {
18+
// NewLimiter returns a new limiter. If you provide limits, the first will set
19+
// both read and write limits, the second will set the write limit.
20+
func NewLimiter(ctx context.Context, limits ...int64) *Limiter {
1921
return &Limiter{
20-
Reads: NewOperation(ctx, true),
21-
Writes: NewOperation(ctx, false),
22+
Reads: NewOperation(ctx, limits, 0),
23+
Writes: NewOperation(ctx, limits, 1),
2224
}
2325
}
2426

operation.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ type Operation struct {
2121
reader bool
2222
}
2323

24-
func NewOperation(ctx context.Context, reader bool) (op *Operation) {
24+
func NewOperation(ctx context.Context, limits []int64, idx int) (op *Operation) {
2525
ch := make(chan struct{}, secparts)
26-
op = &Operation{ch: ch, reader: reader}
26+
op = &Operation{ch: ch, reader: idx == 0}
27+
var limit int64
28+
if len(limits) > 0 {
29+
limit = limits[0]
30+
if len(limits) > idx {
31+
limit = limits[idx]
32+
}
33+
}
34+
op.Limit.Store(limit)
2735
go op.run(ctx, ch)
2836
return
2937
}

operation_test.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,7 @@ func TestOperation_io_read_limit(t *testing.T) {
9292
func TestOperation_read_rate_low(t *testing.T) {
9393
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
9494
defer cancel()
95-
l := NewLimiter(ctx)
96-
97-
l.Reads.Limit.Store(1000) // 10 bytes @ 1000/sec
95+
l := NewLimiter(ctx, 1000)
9896

9997
now := time.Now()
10098
r := bytes.NewReader(make([]byte, 2000))
@@ -109,6 +107,7 @@ func TestOperation_read_rate_low(t *testing.T) {
109107
}
110108

111109
if elapsed := time.Since(now); elapsed < time.Millisecond*900 || elapsed > time.Millisecond*1100 {
110+
t.Log(l.Reads.Limit.Load())
112111
t.Error(elapsed)
113112
}
114113
if rate := int(l.Reads.Rate.Load()); rate < 990 || rate > 1000 {
@@ -119,10 +118,8 @@ func TestOperation_read_rate_low(t *testing.T) {
119118
func TestOperation_read_rate_high(t *testing.T) {
120119
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
121120
defer cancel()
122-
l := NewLimiter(ctx)
123-
124121
const numbytes = (2 * 1024 * 1024 * 1024) - 1
125-
l.Reads.Limit.Store(numbytes)
122+
l := NewLimiter(ctx, numbytes)
126123

127124
now := time.Now()
128125
r := &unlimitedReader{}
@@ -158,8 +155,8 @@ func TestOperation_read_rate_high(t *testing.T) {
158155
func TestOperation_write_rate(t *testing.T) {
159156
ctx, cancel := context.WithTimeout(context.Background(), time.Second*2)
160157
defer cancel()
161-
l := NewLimiter(ctx)
162-
l.Writes.Limit.Store(1000000)
158+
159+
l := NewLimiter(ctx, 1000000)
163160

164161
buf := make([]byte, 10000)
165162

0 commit comments

Comments
 (0)