-
-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
httploader: Implement retries #5077
Conversation
Not sure if the logging is correct? I think I can replace their logger like so: retryClient.Logger = ctx.Logger() but I kept getting this error:
|
Is the dependency really necessary? This seems like the sort of thing we should just do ourselves in code. We try to avoid dependencies as much as possible. |
Yeah, retries can be done with a |
So I have the // Reattempts the http call using exponential back off when maxRetries is greater than 0.
func doHttpCallWithRetries(ctx caddy.Context, client *http.Client, request *http.Request, maxRetries int) (*http.Response, error) {
var resp *http.Response
var err error
var errorMessage string
for retry := 0; retry <= maxRetries; retry++ {
resp, err = client.Do(request)
if err != nil {
errorMessage = fmt.Sprintf("problem calling endpoint: %v", err)
} else if resp.StatusCode < 200 || resp.StatusCode > 499 {
errorMessage = fmt.Sprintf("bad response status code from endpoint: %v", resp.StatusCode)
} else {
return resp, nil
}
ctx.Logger().Error(errorMessage)
exponentialBackOff := math.Pow(2, float64(retry))
ctx.Logger().Info(fmt.Sprintf("reattempting in %g seconds", exponentialBackOff))
time.Sleep(time.Duration(exponentialBackOff) * time.Second)
}
return resp, err
} Error: panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x100ac84ec]
goroutine 1 [running]:
github.com/caddyserver/caddy/v2.(*Logging).Logger(0x140000bd800, {0x0?, 0x0?})
/Users/corycooper/workspace/github/caddy/logging.go:202 +0x2c
github.com/caddyserver/caddy/v2.Context.Logger({{0x101dc8c70, 0x14000683780}, 0x140000bd7d0, 0x1400018fd10, {0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}}, ...)
/Users/corycooper/workspace/github/caddy/context.go:478 +0xc0
github.com/caddyserver/caddy/v2/caddyconfig.HTTPLoader.doHttpCallWithRetries({{0x14000688f58, 0x4}, {0x140001d4d50, 0x24}, 0x0, 0x0, 0x5, 0x0}, {{0x101dc8c70, 0x14000683780}, ...}, ...)
/Users/corycooper/workspace/github/caddy/caddyconfig/httploader.go:140 +0xa4
github.com/caddyserver/caddy/v2/caddyconfig.HTTPLoader.LoadConfig({{0x14000688f58, 0x4}, {0x140001d4d50, 0x24}, 0x0, 0x0, 0x5, 0x0}, {{0x101dc8c70, 0x14000683780}, ...})
/Users/corycooper/workspace/github/caddy/caddyconfig/httploader.go:101 +0x35c
github.com/caddyserver/caddy/v2.finishSettingUp({{0x101dc8c70, 0x14000683780}, 0x140000bd7d0, 0x1400018fd10, {0x0, 0x0, 0x0}, {0x0, 0x0, 0x0}}, ...)
/Users/corycooper/workspace/github/caddy/caddy.go:603 +0x4c0
github.com/caddyserver/caddy/v2.run(0x140000d6320?, 0x1)
/Users/corycooper/workspace/github/caddy/caddy.go:525 +0x584
github.com/caddyserver/caddy/v2.unsyncedDecodeAndRun({0x140000d6280, 0x93, 0xa0}, 0x1)
/Users/corycooper/workspace/github/caddy/caddy.go:337 +0xe4
github.com/caddyserver/caddy/v2.changeConfig({0x10166b466, 0x4}, {0x101678569, 0x7}, {0x14000474400, 0xe3, 0x200}, {0x0, 0x0}, 0x1)
/Users/corycooper/workspace/github/caddy/caddy.go:228 +0x644
github.com/caddyserver/caddy/v2.Load({0x14000474400?, 0xb?, 0x0?}, 0x0?)
/Users/corycooper/workspace/github/caddy/caddy.go:127 +0x8c
github.com/caddyserver/caddy/v2/cmd.cmdRun({0x0?})
/Users/corycooper/workspace/github/caddy/cmd/commandfuncs.go:212 +0x558
github.com/caddyserver/caddy/v2/cmd.caddyCmdToCoral.func1(0x140003fc000?, {0x10166c146?, 0x2?, 0x2?})
/Users/corycooper/workspace/github/caddy/cmd/cobra.go:114 +0x30
github.com/spf13/cobra.(*Command).execute(0x140003fc000, {0x140000bef80, 0x2, 0x2})
/Users/corycooper/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:852 +0x4c4
github.com/spf13/cobra.(*Command).ExecuteC(0x102a12a20)
/Users/corycooper/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:960 +0x34c
github.com/spf13/cobra.(*Command).Execute(...)
/Users/corycooper/go/pkg/mod/github.com/spf13/cobra@v1.1.3/command.go:897
github.com/caddyserver/caddy/v2/cmd.Main()
/Users/corycooper/workspace/github/caddy/cmd/main.go:64 +0x74
main.main()
/Users/corycooper/workspace/github/caddy/cmd/caddy/main.go:39 +0x1c
``` |
max_retries
option to admin httploader
max_retries
option to admin httploader
max_retries
option to admin http
config loader
The logger issue is related to this change e43b6d8 But I don't know why it's failing. It seems like it's not able to get a module from the context for some reason. I'm confused. It might be because config loader modules don't get |
I changed it to use the |
a4739f4
to
a176d4f
Compare
I'll take a look at this shortly! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this looks good overall! I had some thoughts that I wanted to bounce off you.
a176d4f
to
a2b3cad
Compare
max_retries
option to admin http
config loaderretry_delay
option to admin http
config loader
a2b3cad
to
9f257ea
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking really good!
Most of my comments are nits but I have a few technical suggestions as well. Hopefully these make sense.
Thanks for your patience with me.
retry_delay
option to admin http
config loaderThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!! Looking much better and more straightforward.
Just a few suggestions for code tidiness but otherwise this has my approval.
Co-authored-by: Matt Holt <mholt@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's perfect. Thanks Cory!
Resolves #5070