Skip to content

Commit 9f00ae1

Browse files
LucasRoesleralexellis
authored andcommitted
feat: allow functional nil limiter.Met implementation
Ensure that Met() is functional when the limiter is nil. This makes it a bit easier to work with because you can assume the `cl.Met` is always callable. Reducing the number of required nil checks. Signed-off-by: Lucas Roesler <[email protected]>
1 parent dabce8d commit 9f00ae1

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

concurrency-limiter/concurrency_limiter.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ type ConcurrencyLimiter struct {
4444
}
4545

4646
func (cl *ConcurrencyLimiter) Met() bool {
47+
if cl == nil {
48+
return false
49+
}
50+
4751
// We should not have any ConcurrencyLimiter created with a limit of 0
4852
// but return early if that's the case.
4953
if cl.maxInflightRequests == 0 {

concurrency-limiter/concurrency_limiter_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ import (
1111
"time"
1212
)
1313

14+
func Test_Met_FalseWhenNil(t *testing.T) {
15+
var cl *ConcurrencyLimiter
16+
if cl.Met() != false {
17+
t.Fatalf("Want Met() to be false when nil, got: %t", cl.Met())
18+
}
19+
}
20+
1421
func Test_Met_FalseWhenNoLimitSet(t *testing.T) {
1522
t.Parallel()
1623

0 commit comments

Comments
 (0)