Skip to content
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

Merged
merged 1 commit into from
Oct 30, 2022

Conversation

cuducos
Copy link
Owner

@cuducos cuducos commented Oct 27, 2022

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.

main.go Outdated Show resolved Hide resolved
main.go Outdated Show resolved Hide resolved
main.go Outdated Show resolved Hide resolved
main.go Outdated
return b.Bytes(), nil
}

func getWithTimeout(u string, t time.Duration) ([]byte, error) {
Copy link
Collaborator

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 Show resolved Hide resolved
main.go Outdated
}()
select {
case <-ctx.Done():
return []byte{}, fmt.Errorf("request to %s ended due to timeout", u)
Copy link
Collaborator

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)
Copy link
Collaborator

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) {
Copy link
Collaborator

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:

main_test.go Outdated
s := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
Copy link
Collaborator

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

@cuducos cuducos force-pushed the get-with-retries-and-timeout branch from 8ea3d2a to d110280 Compare October 27, 2022 19:38
@cuducos cuducos force-pushed the get-with-retries-and-timeout branch from 9e5a1cb to 50f9d4c Compare October 30, 2022 13:08
@cuducos cuducos merged commit 6862f83 into main Oct 30, 2022
@cuducos cuducos deleted the get-with-retries-and-timeout branch October 30, 2022 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants