-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
transport: mute errors when too verbose (#249)
- Loading branch information
Showing
3 changed files
with
137 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package utils | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type BatchMute struct { | ||
batchTime time.Time | ||
resetInterval time.Duration | ||
ctr int | ||
max int | ||
} | ||
|
||
func (b *BatchMute) increment(val int, t time.Time) (muted bool, skipped int) { | ||
|
||
if b.max == 0 || b.resetInterval == 0 { | ||
return muted, skipped | ||
} | ||
|
||
if b.ctr >= b.max { | ||
skipped = b.ctr - b.max | ||
} | ||
|
||
if t.Sub(b.batchTime) > b.resetInterval { | ||
b.ctr = 0 | ||
b.batchTime = t | ||
} | ||
b.ctr += val | ||
|
||
return b.max > 0 && b.ctr > b.max, skipped | ||
} | ||
|
||
func (b *BatchMute) Increment() (muting bool, skipped int) { | ||
return b.increment(1, time.Now().UTC()) | ||
} | ||
|
||
func NewBatchMute(resetInterval time.Duration, max int) *BatchMute { | ||
return &BatchMute{ | ||
batchTime: time.Now().UTC(), | ||
resetInterval: resetInterval, | ||
max: max, | ||
} | ||
} |
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,51 @@ | ||
package utils | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestBatchMute(t *testing.T) { | ||
tm := time.Date(2023, time.November, 10, 23, 0, 0, 0, time.UTC) | ||
bm := BatchMute{ | ||
batchTime: tm, | ||
resetInterval: time.Second * 10, | ||
max: 5, | ||
} | ||
|
||
for i := 0; i < 20; i++ { | ||
tm = tm.Add(time.Second) | ||
t.Log(bm.increment(1, tm)) | ||
} | ||
|
||
} | ||
|
||
func TestBatchMuteZero(t *testing.T) { | ||
tm := time.Date(2023, time.November, 10, 23, 0, 0, 0, time.UTC) | ||
bm := BatchMute{ | ||
batchTime: tm, | ||
resetInterval: time.Second * 10, | ||
max: 0, | ||
} | ||
|
||
for i := 0; i < 20; i++ { | ||
tm = tm.Add(time.Second) | ||
t.Log(bm.increment(1, tm)) | ||
} | ||
|
||
} | ||
|
||
func TestBatchMuteInterval(t *testing.T) { | ||
tm := time.Date(2023, time.November, 10, 23, 0, 0, 0, time.UTC) | ||
bm := BatchMute{ | ||
batchTime: tm, | ||
resetInterval: 0, | ||
max: 5, | ||
} | ||
|
||
for i := 0; i < 20; i++ { | ||
tm = tm.Add(time.Second) | ||
t.Log(bm.increment(1, tm)) | ||
} | ||
|
||
} |