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

hystrix POST with retry returns "http: ContentLength=... with Body length 0" #127

Open
sebastian-popa opened this issue Jan 19, 2022 · 0 comments · May be fixed by #132
Open

hystrix POST with retry returns "http: ContentLength=... with Body length 0" #127

sebastian-popa opened this issue Jan 19, 2022 · 0 comments · May be fixed by #132

Comments

@sebastian-popa
Copy link

It looks like the retries with POST used in a hystrix client run into some error related to the request being already used. Error returned from http call is "{"status":"Post "http://localhost:8080/sampleLookup\": http: ContentLength=14 with Body length 0"}", which seems incorrect - we shouldn't run into an issue of request being reused several times.

Did anyone encounter this?

Thanks!

Source code following, curl command under the code.

package main

import (
	"bytes"
	"encoding/json"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	hystrix "github.com/gojek/heimdall/v7/hystrix"
)

type SampleRequest struct {
	String string `json:"string"`
}

type SampleResponse struct {
	Status string `json:"status"`
}

func initialEndpoint(w http.ResponseWriter, r *http.Request) {
	var sampleRequest SampleRequest

	// hystrix client
	client := hystrix.NewClient(
		hystrix.WithHystrixTimeout(1000*time.Millisecond),
		hystrix.WithRetryCount(3),
	)

	// request buffer
	var buf bytes.Buffer
	_ = json.NewEncoder(&buf).Encode(sampleRequest)

	// send request
	headers := http.Header{}
	headers.Set("Content-Type", "application/json")
	res, err := client.Post("http://localhost:8080/sampleLookup", &buf, headers)

	// interpret response
	if err != nil {
		sampleResponse := SampleResponse{
			Status: err.Error(),
		}
		json.NewEncoder(w).Encode(sampleResponse)
		return
	}
	body, _ := ioutil.ReadAll(res.Body)
	json.NewEncoder(w).Encode(body)
	res.Body.Close()
}

// just wait for 3 sec
func sampleLookup(w http.ResponseWriter, r *http.Request) {
	time.Sleep(3 * time.Second)
	json.NewEncoder(w).Encode(SampleResponse{
		Status: "ok",
	})
}

// start server with 2 endpoints
func main() {
	http.HandleFunc("/initialEndpoint", initialEndpoint)
	http.HandleFunc("/sampleLookup", sampleLookup)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Curl command:

curl --location --request GET 'http://localhost:8080/initialEndpoint' \
--header 'Content-Type: text/plain' \
--data-raw '{
    "String": "some value"
}'
@vaguecoder vaguecoder linked a pull request Aug 14, 2022 that will close this issue
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 a pull request may close this issue.

1 participant