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

fix: forwarder copy body eof #38

Merged
merged 2 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions agent/forwarder.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package agent

import (
"bytes"
"context"
"errors"
"fmt"
"io"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -80,6 +83,18 @@ func (f *forwarder) Forward(req *http.Request) (*http.Response, error) {
return nil, errUpstreamUnreachable
}

// We must copy the response body before replying. The request context is
// cancelled when Forward returns so attempting top copy the body again
// may fail.
//
// TODO(andydunstall): Understand this better as should be avoidable.
var buf bytes.Buffer
if _, err := io.Copy(&buf, resp.Body); err != nil {
return nil, fmt.Errorf("copy body: %w", err)
}
resp.Body.Close()
resp.Body = io.NopCloser(&buf)

f.metrics.ForwardRequestsTotal.With(prometheus.Labels{
"method": req.Method,
"status": strconv.Itoa(resp.StatusCode),
Expand Down
5 changes: 4 additions & 1 deletion cli/workload/requests.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package workload

import (
"bytes"
"context"
"fmt"
"math/rand"
Expand Down Expand Up @@ -129,12 +130,14 @@ func runClient(ctx context.Context, conf *config.RequestsConfig, logger log.Logg
ticker := time.NewTicker(time.Duration(int(time.Second) / conf.Rate))
defer ticker.Stop()

body := make([]byte, conf.RequestSize)

client := &http.Client{}
for {
select {
case <-ticker.C:
endpointID := rand.Int() % conf.Endpoints
req, _ := http.NewRequest("GET", conf.Server.URL, nil)
req, _ := http.NewRequest("GET", conf.Server.URL, bytes.NewReader(body))
req.Header.Set("x-piko-endpoint", strconv.Itoa(endpointID))
resp, err := client.Do(req)
if err != nil {
Expand Down
12 changes: 12 additions & 0 deletions workload/config/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type RequestsConfig struct {
// Endpoints is the number of available endpoint IDs.
Endpoints int `json:"endpoints" yaml:"endpoints"`

// RequestSize is the size of each request.
RequestSize int `json:"request_size" yaml:"request_size"`

Server ServerConfig `json:"server" yaml:"server"`

Log log.Config `json:"log" yaml:"log"`
Expand Down Expand Up @@ -69,6 +72,15 @@ On each request, the client selects a random endpoint ID from 0 to
'endpoints'.`,
)

fs.IntVar(
&c.RequestSize,
"request-size",
1024,
`
The size of each request. As the upstream echos the response body, the response
will have the same size.`,
)

fs.StringVar(
&c.Server.URL,
"server.url",
Expand Down
Loading