-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
Prototypes a GET HTTP request with retries and an independent timeout #1
Conversation
main.go
Outdated
return b.Bytes(), nil | ||
} | ||
|
||
func getWithTimeout(u string, t time.Duration) ([]byte, error) { |
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.
Sugestão:
getWithContext ... e a usuária envia um context com timeout. O que acha?
main.go
Outdated
}() | ||
select { | ||
case <-ctx.Done(): | ||
return []byte{}, fmt.Errorf("request to %s ended due to timeout", u) |
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.
A requisição http disparada pelo get ficará pendente até o final da solicitação (mesmo em caso de timeout). Apesar de não ser muito problemático, me soa como um leak de abstração, indicando que temos que usar context e timeout em toda a pilha.
Aqui você encontra um bom material para guiar essa mudança
main.go
Outdated
} | ||
|
||
func get(u string) ([]byte, error) { | ||
r, err := http.Get(u) |
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.
Provavelmente iremos customizar o cliente padrão, daí talvez vale a pena já nascer como struct guardando o objeto http client.
main.go
Outdated
} | ||
} | ||
|
||
func getWithRetriesAndTimeout(u string, t time.Duration) ([]byte, error) { |
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.
Essa parte é bem tricky e eu recomendo usar uma lib (tiny) para te ajudar. Duas opções:
- https://github.com/avast/retry-go (MIT)
- https://github.com/sethvargo/go-retry (Apache 2)
main_test.go
Outdated
s := httptest.NewServer( | ||
http.HandlerFunc( | ||
func(w http.ResponseWriter, r *http.Request) { | ||
switch r.URL.Path { |
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.
Sugestão para tornar mais claro:
- criar um map[string]func(w http.ResponseWriter)
- a implementação do servidor faz apenas mr.URL.Path
8ea3d2a
to
d110280
Compare
9e5a1cb
to
50f9d4c
Compare
Prototypes a GET HTTP request with retries and an independent timeout.
The main question here is whether the suggested
getWithTimeout
is a safe and good practice — i.e. an early return on a function when there's a child goroutine that might be working yet.