-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Debugging and extension timeout (#417)
This commit: * Adds an extensions_timeout to the osquery invocation * Adds stacktraces to the top level fatal error reporting * Adds retry logic in several places Why? * We occasionally see issues with osquery failing to start due to an extension timeout. This has been rare, and hard to debug. This adds retry logic and improves debugging
- Loading branch information
1 parent
8de4cba
commit fa45344
Showing
11 changed files
with
169 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
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
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,63 @@ | ||
package backoff | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type Opt func(*Backoff) | ||
|
||
func MaxAttempts(i int) Opt { | ||
return func(b *Backoff) { | ||
b.maxAttempt = i | ||
} | ||
} | ||
|
||
// Backoff is a quick retry function | ||
type Backoff struct { | ||
count int | ||
maxAttempt int | ||
delay float32 | ||
runFunc func() error | ||
} | ||
|
||
// New returns a Backoff timer | ||
func New(opts ...Opt) *Backoff { | ||
b := &Backoff{ | ||
count: 0, | ||
delay: 1, | ||
maxAttempt: 20, | ||
} | ||
|
||
for _, opt := range opts { | ||
opt(b) | ||
} | ||
|
||
return b | ||
} | ||
|
||
// Run trys to run function several times until it succeeds or times out. | ||
func (b *Backoff) Run(runFunc func() error) error { | ||
b.runFunc = runFunc | ||
return b.try() | ||
} | ||
|
||
func (b *Backoff) try() error { | ||
b.count += 1 | ||
err := b.runFunc() | ||
if err != nil { | ||
if b.count >= b.maxAttempt { | ||
return errors.Wrap(err, "done trying") | ||
} | ||
|
||
// Wait for amount of time | ||
timer := time.NewTimer(time.Second * time.Duration(b.delay)) | ||
<-timer.C | ||
|
||
return b.try() | ||
} | ||
|
||
// err == nil, SUCCESS! | ||
return nil | ||
} |
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,77 @@ | ||
package backoff | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestImmediate(t *testing.T) { | ||
t.Parallel() | ||
|
||
bkoff := New() | ||
bkoff.delay = 0.0 | ||
|
||
err := bkoff.Run(willSucceed) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, bkoff.count) | ||
} | ||
|
||
func TestEventual(t *testing.T) { | ||
t.Parallel() | ||
bkoff := New() | ||
bkoff.delay = 0.0 | ||
|
||
tTime := &takesTime{ | ||
attempts: 0, | ||
} | ||
|
||
err := bkoff.Run(tTime.eventual) | ||
require.NoError(t, err) | ||
require.Equal(t, 5, bkoff.count) | ||
require.Equal(t, 5, tTime.attempts) | ||
|
||
} | ||
|
||
func TestSlowFail(t *testing.T) { | ||
t.Parallel() | ||
bkoff := New() | ||
bkoff.delay = 0.0 | ||
|
||
err := bkoff.Run(willFail) | ||
require.Error(t, err) | ||
require.Equal(t, 20, bkoff.count) | ||
} | ||
|
||
func TestMaxAttempts(t *testing.T) { | ||
t.Parallel() | ||
bkoff := New(MaxAttempts(5)) | ||
bkoff.delay = 0.0 | ||
|
||
err := bkoff.Run(willFail) | ||
require.Error(t, err) | ||
require.Equal(t, 5, bkoff.count) | ||
} | ||
|
||
func willSucceed() error { | ||
return nil | ||
} | ||
|
||
func willFail() error { | ||
return errors.New("nope") | ||
} | ||
|
||
type takesTime struct { | ||
attempts int | ||
} | ||
|
||
func (t *takesTime) eventual() error { | ||
t.attempts += 1 | ||
|
||
if t.attempts >= 5 { | ||
return nil | ||
} | ||
|
||
return errors.New("not yet") | ||
} |
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