-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding Exp and Const backoff for good measure. (#442)
* Adding Exp and Const backoff for good measure. Signed-off-by: Scott Nichols <snichols@vmware.com> * vet wants the cancel called... Signed-off-by: Scott Nichols <snichols@vmware.com> * vet fix. Signed-off-by: Scott Nichols <snichols@vmware.com> * no need for the inner function Signed-off-by: Scott Nichols <snichols@vmware.com>
- Loading branch information
Showing
13 changed files
with
540 additions
and
261 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,51 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"time" | ||
|
||
cloudevents "github.com/cloudevents/sdk-go/v2" | ||
) | ||
|
||
func main() { | ||
ctx := cloudevents.ContextWithTarget(context.Background(), "http://localhost:8080/") | ||
|
||
p, err := cloudevents.NewHTTP() | ||
if err != nil { | ||
log.Fatalf("failed to create protocol: %s", err.Error()) | ||
} | ||
|
||
c, err := cloudevents.NewClient(p, cloudevents.WithTimeNow(), cloudevents.WithUUIDs()) | ||
if err != nil { | ||
log.Fatalf("failed to create client, %v", err) | ||
} | ||
|
||
// must send each event within 5 seconds for sleepy demo. | ||
|
||
log.Println("--- Constant ---") | ||
send10(cloudevents.ContextWithRetriesConstantBackoff(ctx, 10*time.Millisecond, 10), c) | ||
log.Println("--- Linear ---") | ||
send10(cloudevents.ContextWithRetriesLinearBackoff(ctx, 10*time.Millisecond, 10), c) | ||
log.Println("--- Exponential ---") | ||
send10(cloudevents.ContextWithRetriesExponentialBackoff(ctx, 10*time.Millisecond, 10), c) | ||
} | ||
|
||
func send10(ctx context.Context, c cloudevents.Client) { | ||
for i := 0; i < 100; i++ { | ||
e := cloudevents.NewEvent() | ||
e.SetType("com.cloudevents.sample.sent") | ||
e.SetSource("https://github.com/cloudevents/sdk-go/v2/cmd/samples/httpb/sender") | ||
_ = e.SetData(cloudevents.ApplicationJSON, map[string]interface{}{ | ||
"id": i, | ||
"message": "Hello, World!", | ||
}) | ||
|
||
if result := c.Send(ctx, e); !cloudevents.IsACK(result) { | ||
log.Printf("failed to send: %s", result.Error()) | ||
} else { | ||
log.Printf("sent: %d", i) | ||
} | ||
time.Sleep(50 * time.Millisecond) | ||
} | ||
} |
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,114 @@ | ||
package context | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestRetryParams_Backoff(t *testing.T) { | ||
tests := map[string]struct { | ||
rp *RetryParams | ||
ctx context.Context | ||
tries int | ||
wantErr bool | ||
}{ | ||
"none 1": { | ||
ctx: context.Background(), | ||
rp: &RetryParams{Strategy: BackoffStrategyNone}, | ||
tries: 1, | ||
wantErr: true, | ||
}, | ||
"const 1": { | ||
ctx: context.Background(), | ||
rp: &RetryParams{Strategy: BackoffStrategyConstant, MaxTries: 10, Period: 1 * time.Nanosecond}, | ||
tries: 5, | ||
}, | ||
"linear 1": { | ||
ctx: context.Background(), | ||
rp: &RetryParams{Strategy: BackoffStrategyLinear, MaxTries: 10, Period: 1 * time.Nanosecond}, | ||
tries: 1, | ||
}, | ||
"exponential 1": { | ||
ctx: context.Background(), | ||
rp: &RetryParams{Strategy: BackoffStrategyExponential, MaxTries: 10, Period: 1 * time.Nanosecond}, | ||
tries: 1, | ||
}, | ||
"const timeout": { | ||
ctx: func() context.Context { | ||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond) | ||
go func() { | ||
time.Sleep(10 * time.Millisecond) | ||
cancel() | ||
}() | ||
return ctx | ||
}(), | ||
rp: &RetryParams{Strategy: BackoffStrategyConstant, MaxTries: 10, Period: 1 * time.Second}, | ||
tries: 5, | ||
wantErr: true, | ||
}, | ||
} | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
if err := tc.rp.Backoff(tc.ctx, tc.tries); (err != nil) != tc.wantErr { | ||
t.Errorf("Backoff() error = %v, wantErr %v", err, tc.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestRetryParams_BackoffFor(t *testing.T) { | ||
tests := map[string]struct { | ||
rp *RetryParams | ||
tries int | ||
want time.Duration | ||
}{ | ||
"none 1": { | ||
rp: &RetryParams{Strategy: BackoffStrategyNone}, | ||
tries: 1, | ||
want: time.Duration(0), | ||
}, | ||
"const 1": { | ||
rp: &RetryParams{Strategy: BackoffStrategyConstant, MaxTries: 10, Period: 1 * time.Second}, | ||
tries: 1, | ||
want: 1 * time.Second, | ||
}, | ||
"linear 1": { | ||
rp: &RetryParams{Strategy: BackoffStrategyLinear, MaxTries: 10, Period: 1 * time.Second}, | ||
tries: 1, | ||
want: 1 * time.Second, | ||
}, | ||
"exponential 1": { | ||
rp: &RetryParams{Strategy: BackoffStrategyExponential, MaxTries: 10, Period: 1 * time.Second}, | ||
tries: 1, | ||
want: 2 * time.Second, // 1 == 2^1 | ||
}, | ||
"none 5": { | ||
rp: &RetryParams{Strategy: BackoffStrategyNone}, | ||
tries: 5, | ||
want: time.Duration(0), | ||
}, | ||
"const 5": { | ||
rp: &RetryParams{Strategy: BackoffStrategyConstant, MaxTries: 10, Period: 1 * time.Second}, | ||
tries: 5, | ||
want: 1 * time.Second, | ||
}, | ||
"linear 5": { | ||
rp: &RetryParams{Strategy: BackoffStrategyLinear, MaxTries: 10, Period: 1 * time.Second}, | ||
tries: 5, | ||
want: 5 * time.Second, | ||
}, | ||
"exponential 5": { | ||
rp: &RetryParams{Strategy: BackoffStrategyExponential, MaxTries: 10, Period: 1 * time.Second}, | ||
tries: 5, | ||
want: 32 * time.Second, // 32 == 2^5 | ||
}, | ||
} | ||
for name, tc := range tests { | ||
t.Run(name, func(t *testing.T) { | ||
if got := tc.rp.BackoffFor(tc.tries); got != tc.want { | ||
t.Errorf("BackoffFor() = %v, want %v", got, tc.want) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.