-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Yuri Nikolic <[email protected]>
- Loading branch information
1 parent
e772133
commit a47c7a4
Showing
6 changed files
with
271 additions
and
24 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,79 @@ | ||
package log | ||
|
||
import "golang.org/x/time/rate" | ||
|
||
type rateLimitedLogger struct { | ||
next Interface | ||
limiter *rate.Limiter | ||
} | ||
|
||
// NewRateLimitedLogger returns a logger.Interface that is limited to the given number of logs per second, | ||
// with the given burst size. | ||
func NewRateLimitedLogger(logger Interface, logsPerSecond rate.Limit, burstSize int) Interface { | ||
return &rateLimitedLogger{ | ||
next: logger, | ||
limiter: rate.NewLimiter(logsPerSecond, burstSize), | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Debugf(format string, args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Debugf(format, args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Debugln(args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Debugln(args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Infof(format string, args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Infof(format, args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Infoln(args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Infoln(args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Errorf(format string, args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Errorf(format, args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Errorln(args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Errorln(args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Warnf(format string, args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Warnf(format, args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) Warnln(args ...interface{}) { | ||
if l.limiter.Allow() { | ||
l.next.Warnln(args...) | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) WithField(key string, value interface{}) Interface { | ||
return &rateLimitedLogger{ | ||
next: l.next.WithField(key, value), | ||
limiter: l.limiter, | ||
} | ||
} | ||
|
||
func (l *rateLimitedLogger) WithFields(f Fields) Interface { | ||
return &rateLimitedLogger{ | ||
next: l.next.WithFields(f), | ||
limiter: l.limiter, | ||
} | ||
} |
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,59 @@ | ||
package log | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type counterLogger struct { | ||
count int | ||
} | ||
|
||
func (c *counterLogger) Debugf(_ string, _ ...interface{}) { c.count++ } | ||
func (c *counterLogger) Debugln(_ ...interface{}) { c.count++ } | ||
func (c *counterLogger) Infof(_ string, _ ...interface{}) { c.count++ } | ||
func (c *counterLogger) Infoln(_ ...interface{}) { c.count++ } | ||
func (c *counterLogger) Warnf(_ string, _ ...interface{}) { c.count++ } | ||
func (c *counterLogger) Warnln(_ ...interface{}) { c.count++ } | ||
func (c *counterLogger) Errorf(_ string, _ ...interface{}) { c.count++ } | ||
func (c *counterLogger) Errorln(_ ...interface{}) { c.count++ } | ||
func (c *counterLogger) WithField(_ string, _ interface{}) Interface { | ||
return c | ||
} | ||
func (c *counterLogger) WithFields(Fields) Interface { | ||
return c | ||
} | ||
|
||
func TestRateLimitedLoggerLogs(t *testing.T) { | ||
c := &counterLogger{} | ||
r := NewRateLimitedLogger(c, 1, 1) | ||
|
||
r.Errorln("asdf") | ||
assert.Equal(t, 1, c.count) | ||
} | ||
|
||
func TestRateLimitedLoggerLimits(t *testing.T) { | ||
c := &counterLogger{} | ||
r := NewRateLimitedLogger(c, 2, 2) | ||
|
||
r.Errorln("asdf") | ||
r.Infoln("asdf") | ||
r.Debugln("asdf") | ||
assert.Equal(t, 2, c.count) | ||
time.Sleep(time.Second) | ||
r.Infoln("asdf") | ||
assert.Equal(t, 3, c.count) | ||
} | ||
|
||
func TestRateLimitedLoggerWithFields(t *testing.T) { | ||
c := &counterLogger{} | ||
r := NewRateLimitedLogger(c, 1, 1) | ||
r2 := r.WithField("key", "value") | ||
|
||
r.Errorf("asdf") | ||
r2.Errorln("asdf") | ||
r2.Warnln("asdf") | ||
assert.Equal(t, 1, c.count) | ||
} |
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
Oops, something went wrong.