Skip to content

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

Open
@sebastian-popa

Description

@sebastian-popa

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"
}'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions