-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from EspressoSystems/jh/remove-log
Add the log helper
- Loading branch information
Showing
6 changed files
with
213 additions
and
13 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
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,45 @@ | ||
package log_helper | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
type Channel string | ||
|
||
// This is used for cleaning up log outputs. | ||
// The scenario is that rollup will query hotshot blocks that are not yet ready, | ||
// which generates a lot of logs. However, these logs don't contain much useful | ||
// information and actually make debugging more difficult. | ||
type LogHelper struct { | ||
strategies map[Channel]Strategy | ||
} | ||
|
||
func NewLogger() LogHelper { | ||
return LogHelper{strategies: map[Channel]Strategy{}} | ||
} | ||
|
||
func (l *LogHelper) AddStrategy(channel Channel, strategy Strategy) { | ||
l.strategies[channel] = strategy | ||
} | ||
|
||
func (l *LogHelper) AddLogAfterRetryStrategy(channel Channel, id string, retryTimes uint32) { | ||
s := NewLogAfterRetryStrategy(id, retryTimes) | ||
l.AddStrategy(channel, s) | ||
} | ||
|
||
func (l *LogHelper) AddLogAfterDurationStrategy(channel Channel, id string, duration time.Duration) { | ||
s := NewLogAfterDurationStrategy(id, duration) | ||
l.AddStrategy(channel, s) | ||
} | ||
|
||
func (l *LogHelper) Attempt(channel Channel, id string, logFunc func()) { | ||
strategy, ok := l.strategies[channel] | ||
if !ok { | ||
// Can not find the strategy, log it | ||
logFunc() | ||
return | ||
} | ||
|
||
result := strategy.attempt(id, logFunc) | ||
l.strategies[channel] = result | ||
} |
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,91 @@ | ||
package log_helper | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/benbjohnson/clock" | ||
) | ||
|
||
func TestLogAfterRetry(t *testing.T) { | ||
helper := NewLogger() | ||
|
||
channel := Channel("test1") | ||
id := "id1" | ||
retryTimes := 5 | ||
helper.AddLogAfterRetryStrategy(channel, id, uint32(retryTimes)) | ||
|
||
cnt := 0 | ||
for i := 0; i < retryTimes; i += 1 { | ||
helper.Attempt(channel, id, func() { cnt += 1 }) | ||
if cnt > 0 { | ||
t.Error("should be zero") | ||
} | ||
} | ||
|
||
expect := 10 | ||
helper.Attempt(channel, id, func() { cnt += expect }) | ||
if cnt != expect { | ||
t.Errorf("expect %d, but got %d", expect, cnt) | ||
} | ||
|
||
// A new id should set the haveRetried to 1. | ||
cnt = 0 | ||
id2 := "id2" | ||
for i := 0; i < retryTimes; i += 1 { | ||
helper.Attempt(channel, id2, func() { cnt += 1 }) | ||
if cnt > 0 { | ||
t.Error("should be zero") | ||
} | ||
} | ||
|
||
helper.Attempt(channel, id2, func() { cnt += expect }) | ||
if cnt != expect { | ||
t.Errorf("expect %d, but got %d", expect, cnt) | ||
} | ||
|
||
} | ||
|
||
func TestLogAfterDuration(t *testing.T) { | ||
mockClock := clock.NewMock() | ||
clk = mockClock | ||
helper := NewLogger() | ||
|
||
channel := Channel("test1") | ||
id := "id1" | ||
|
||
cnt := 0 | ||
|
||
retryDuration := time.Hour | ||
helper.AddLogAfterDurationStrategy(channel, id, retryDuration) | ||
for i := 0; i < 5; i += 1 { | ||
helper.Attempt(channel, id, func() { cnt += 1 }) | ||
if cnt > 0 { | ||
t.Error("should be zero") | ||
} | ||
} | ||
|
||
mockClock.Add(retryDuration + time.Minute) | ||
expect := 10 | ||
helper.Attempt(channel, id, func() { cnt += expect }) | ||
if cnt != expect { | ||
t.Errorf("expect %d, but got %d", expect, cnt) | ||
} | ||
|
||
// A new id should set the startTime to now. | ||
cnt = 0 | ||
|
||
id2 := "id2" | ||
for i := 0; i < 5; i += 1 { | ||
helper.Attempt(channel, id2, func() { cnt += 1 }) | ||
if cnt > 0 { | ||
t.Error("should be zero") | ||
} | ||
} | ||
|
||
mockClock.Add(retryDuration + time.Minute) | ||
helper.Attempt(channel, id2, func() { cnt += expect }) | ||
if cnt != expect { | ||
t.Errorf("expect %d, but got %d", expect, cnt) | ||
} | ||
} |
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,72 @@ | ||
package log_helper | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/benbjohnson/clock" | ||
) | ||
|
||
type Strategy interface { | ||
attempt(id string, logFunc func()) Strategy | ||
} | ||
|
||
type LogAfterRetryStrategy struct { | ||
Id string | ||
Limit uint32 | ||
haveRetried uint32 | ||
} | ||
|
||
func NewLogAfterRetryStrategy(id string, retryTimes uint32) LogAfterRetryStrategy { | ||
return LogAfterRetryStrategy{ | ||
Id: id, | ||
Limit: retryTimes, | ||
haveRetried: 0, | ||
} | ||
} | ||
|
||
func (s LogAfterRetryStrategy) attempt(id string, logFunc func()) Strategy { | ||
if s.Id != id { | ||
s.Id = id | ||
s.haveRetried = 1 | ||
return s | ||
} | ||
|
||
s.haveRetried += 1 | ||
|
||
if s.haveRetried <= s.Limit { | ||
return s | ||
} | ||
|
||
logFunc() | ||
return s | ||
} | ||
|
||
var clk clock.Clock = clock.New() | ||
|
||
type LogAfterDurationStrategy struct { | ||
Id string | ||
RetryDuration time.Duration | ||
StartTime time.Time | ||
} | ||
|
||
func NewLogAfterDurationStrategy(id string, retryDuration time.Duration) LogAfterDurationStrategy { | ||
return LogAfterDurationStrategy{ | ||
Id: id, | ||
RetryDuration: retryDuration, | ||
StartTime: clk.Now(), | ||
} | ||
} | ||
|
||
func (s LogAfterDurationStrategy) attempt(id string, logFunc func()) Strategy { | ||
if s.Id != id { | ||
s.Id = id | ||
s.StartTime = clk.Now() | ||
return s | ||
} | ||
|
||
if clk.Since(s.StartTime) > s.RetryDuration { | ||
logFunc() | ||
return s | ||
} | ||
return s | ||
} |