Skip to content

Commit

Permalink
Add ability to capture logs
Browse files Browse the repository at this point in the history
  • Loading branch information
meson10 committed Apr 4, 2017
1 parent 926c200 commit 55d56e8
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 3 deletions.
20 changes: 17 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Client struct {
MaxRetries int
Backoff BackoffStrategy
KeepLog bool
LogHook LogHook

SuccessReqNum int
SuccessRetryNum int
Expand Down Expand Up @@ -95,6 +96,10 @@ func NewExtendedClient(hc *http.Client) *Client {
return c
}

// PrintErrStrategy is used to log attempts as they happen.
// You know, more visible
type LogHook func(e ErrEntry)

// BackoffStrategy is used to determine how long a retry request should wait until attempted
type BackoffStrategy func(retry int) time.Duration

Expand Down Expand Up @@ -336,12 +341,17 @@ func (c *Client) LogString() string {
defer c.Unlock()
var res string
for _, e := range c.ErrLog {
res += fmt.Sprintf("%d %s [%s] %s request-%d retry-%d error: %s\n",
e.Time.Unix(), e.Method, e.Verb, e.URL, e.Request, e.Retry, e.Err)
res += c.FormatError(e)
}
return res
}

// Format the Error to human readable string
func (c *Client) FormatError(e ErrEntry) string {
return fmt.Sprintf("%d %s [%s] %s request-%d retry-%d error: %s\n",
e.Time.Unix(), e.Method, e.Verb, e.URL, e.Request, e.Retry, e.Err)
}

// LogErrCount is a helper method used primarily for test validation
func (c *Client) LogErrCount() int {
c.Lock()
Expand All @@ -358,8 +368,12 @@ func (c *Client) EmbedHTTPClient(hc *http.Client) {
func (c *Client) log(e ErrEntry) {
if c.KeepLog {
c.Lock()
defer c.Unlock()
c.ErrLog = append(c.ErrLog, e)
c.Unlock()
} else if c.LogHook != nil {
// NOTE: There is a possibility that Log Printing hook slows it down.
// but the consumer can always do the Job in a go-routine.
c.LogHook(e)
}
}

Expand Down
57 changes: 57 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,63 @@ func TestDefaultBackoff(t *testing.T) {

}

func TestCustomLogHook(t *testing.T) {
t.Parallel()

expectedRetries := 5
errorLines := []ErrEntry{}

c := New()
//c.KeepLog = true
c.MaxRetries = expectedRetries
c.Backoff = func(_ int) time.Duration {
return 10 * time.Microsecond
}

c.LogHook = func(e ErrEntry) {
errorLines = append(errorLines, e)
}

nonExistantURL := "http://localhost:9000/foo"

_, err := c.Get(nonExistantURL)
if err == nil {
t.Fatal("expected to get an error")
}
c.Wait()

// in the event of an error, let's see what the logs were
if expectedRetries != len(errorLines) {
t.Errorf("Expected %d lines to be emitted. Got %d", expectedRetries, errorLines)
}
}

func TestDefaultLogHook(t *testing.T) {
t.Parallel()

errorLines := 0

c := New()
//c.KeepLog = true
c.MaxRetries = 5
c.Backoff = func(_ int) time.Duration {
return 10 * time.Microsecond
}

nonExistantURL := "http://localhost:9000/foo"

_, err := c.Get(nonExistantURL)
if err == nil {
t.Fatal("expected to get an error")
}
c.Wait()

// in the event of an error, let's see what the logs were
if errorLines != 0 {
t.Errorf("Expected 0 lines to be emitted. Got %d", errorLines)
}
}

func TestLinearJitterBackoff(t *testing.T) {
t.Parallel()
c := New()
Expand Down

0 comments on commit 55d56e8

Please sign in to comment.