-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Osquerybeat: Improve osquery client connect code
- Loading branch information
Showing
4 changed files
with
219 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package osqdcli | ||
|
||
import ( | ||
"context" | ||
"time" | ||
|
||
"github.com/elastic/beats/v7/libbeat/logp" | ||
) | ||
|
||
type retry struct { | ||
maxRetry int | ||
retryWait time.Duration | ||
log *logp.Logger | ||
} | ||
|
||
type tryFunc func(ctx context.Context) error | ||
|
||
func (r *retry) Run(ctx context.Context, fn tryFunc) (err error) { | ||
maxAttempts := r.maxRetry + 1 | ||
for i := 0; i < maxAttempts; i++ { | ||
attempt := i + 1 | ||
r.log.Debugf("attempt %v out of %v", attempt, maxAttempts) | ||
|
||
err = fn(ctx) | ||
|
||
if err != nil { | ||
r.log.Debugf("attempt %v out of %v failed, err: %v", attempt, maxAttempts, err) | ||
if i != maxAttempts { | ||
if r.retryWait > 0 { | ||
r.log.Debugf("wait for %v before next retry", r.retryWait) | ||
err = waitWithContext(ctx, retryWait) | ||
if err != nil { | ||
r.log.Debugf("wait returned err: %v", err) | ||
return err | ||
} | ||
} | ||
} else { | ||
r.log.Debugf("no more attempts, return err: %v", err) | ||
return err | ||
} | ||
} else { | ||
r.log.Debugf("attempt %v out of %v succeeded", attempt, maxAttempts) | ||
return nil | ||
} | ||
} | ||
return err | ||
} | ||
|
||
func waitWithContext(ctx context.Context, to time.Duration) error { | ||
t := time.NewTimer(to) | ||
defer t.Stop() | ||
select { | ||
case <-ctx.Done(): | ||
return ctx.Err() | ||
case <-t.C: | ||
} | ||
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,139 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License; | ||
// you may not use this file except in compliance with the Elastic License. | ||
|
||
package osqdcli | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/elastic/beats/v7/libbeat/logp" | ||
"github.com/google/go-cmp/cmp" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
) | ||
|
||
func TestRetryRun(t *testing.T) { | ||
logp.Configure(logp.Config{ | ||
Level: logp.DebugLevel, | ||
ToStderr: true, | ||
Selectors: []string{"*"}, | ||
}) | ||
|
||
log := logp.NewLogger("retry_test").With("context", "osquery client connect") | ||
ctx := context.Background() | ||
|
||
type fields struct { | ||
maxRetry int | ||
retryWait time.Duration | ||
log *logp.Logger | ||
} | ||
|
||
type args struct { | ||
ctx context.Context | ||
fn tryFunc | ||
} | ||
|
||
argsWithFunc := func(fn tryFunc) args { | ||
return args{ | ||
ctx: ctx, | ||
fn: fn, | ||
} | ||
} | ||
|
||
funcSucceedsOnNAttempt := func(attempt int) func(context.Context) error { | ||
curAttempt := 1 | ||
return func(ctx context.Context) error { | ||
if curAttempt == attempt { | ||
return nil | ||
} | ||
curAttempt++ | ||
return ErrAlreadyConnected | ||
} | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
fields fields | ||
args args | ||
wantErr error | ||
}{ | ||
{ | ||
name: "no retries, no wait, success", | ||
fields: fields{ | ||
log: log, | ||
}, | ||
args: argsWithFunc(func(ctx context.Context) error { | ||
return nil | ||
}), | ||
}, | ||
{ | ||
name: "no retries, no wait, error", | ||
fields: fields{ | ||
log: log, | ||
}, | ||
args: argsWithFunc(func(ctx context.Context) error { | ||
return ErrAlreadyConnected | ||
}), | ||
wantErr: ErrAlreadyConnected, | ||
}, | ||
{ | ||
name: "retries, no wait, no more retries fails", | ||
fields: fields{ | ||
maxRetry: 3, | ||
log: log, | ||
}, | ||
args: argsWithFunc(funcSucceedsOnNAttempt(8)), | ||
wantErr: ErrAlreadyConnected, | ||
}, | ||
{ | ||
name: "retries, no wait, success", | ||
fields: fields{ | ||
maxRetry: 3, | ||
log: log, | ||
}, | ||
args: argsWithFunc(funcSucceedsOnNAttempt(4)), | ||
}, | ||
{ | ||
name: "retries, with wait, success", | ||
fields: fields{ | ||
maxRetry: 3, | ||
retryWait: 1 * time.Millisecond, | ||
log: log, | ||
}, | ||
args: argsWithFunc(funcSucceedsOnNAttempt(4)), | ||
}, | ||
{ | ||
name: "retries, with wait, success sooner", | ||
fields: fields{ | ||
maxRetry: 3, | ||
retryWait: 1 * time.Millisecond, | ||
log: log, | ||
}, | ||
args: argsWithFunc(funcSucceedsOnNAttempt(2)), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
r := &retry{ | ||
maxRetry: tt.fields.maxRetry, | ||
retryWait: tt.fields.retryWait, | ||
log: tt.fields.log, | ||
} | ||
err := r.Run(tt.args.ctx, tt.args.fn) | ||
if err != nil { | ||
if tt.wantErr != nil { | ||
diff := cmp.Diff(tt.wantErr, err, cmpopts.EquateErrors()) | ||
if diff != "" { | ||
t.Error(diff) | ||
} | ||
} else { | ||
t.Errorf("got err: %v, wantErr: nil", err) | ||
} | ||
} else if tt.wantErr != nil { | ||
t.Errorf("got err: nil, wantErr: %v", tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |