Skip to content

Commit

Permalink
Fix: disable KeepAlives in server
Browse files Browse the repository at this point in the history
See discussion in ipfs/kubo#5168

We cannot stream responses with keep-alives enabled.

I prefer this to not be a client feature, as otherwise users might end up
shooting themselves in the foot.

Note, the price is a corrupted request body which gets added
normally and gives wrong hashes!

License: MIT
Signed-off-by: Hector Sanjuan <code@hector.link>
  • Loading branch information
hsanjuan committed Aug 18, 2018
1 parent 0709652 commit be651da
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion adder/adderutils/adderutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ func AddMultipartHTTPHandler(
}

enc := json.NewEncoder(w)
w.Header().Add("Content-Type", "application/octet-stream")
// This must be application/json otherwise go-ipfs client
// will break.
w.Header().Add("Content-Type", "application/json")
// Browsers should not cache when streaming content.
w.Header().Add("Cache-Control", "no-cache")
w.WriteHeader(http.StatusOK)

var wg sync.WaitGroup
Expand Down
4 changes: 3 additions & 1 deletion api/rest/restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ func NewAPIWithHost(cfg *Config, h host.Host) (*API, error) {
IdleTimeout: cfg.IdleTimeout,
Handler: router,
}
s.SetKeepAlivesEnabled(true) // A reminder that this can be changed

// See: https://github.com/ipfs/go-ipfs/issues/5168
s.SetKeepAlivesEnabled(false)

ctx, cancel := context.WithCancel(context.Background())

Expand Down
4 changes: 3 additions & 1 deletion ipfsconn/ipfshttp/ipfshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ func NewConnector(cfg *Config) (*Connector, error) {
IdleTimeout: cfg.ProxyIdleTimeout,
Handler: smux,
}
s.SetKeepAlivesEnabled(true) // A reminder that this can be changed

// See: https://github.com/ipfs/go-ipfs/issues/5168
s.SetKeepAlivesEnabled(false) // A reminder that this can be changed

c := &http.Client{} // timeouts are handled by context timeouts

Expand Down
4 changes: 2 additions & 2 deletions ipfsconn/ipfshttp/ipfshttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ func testIPFSConnector(t *testing.T) (*Connector, *test.IpfsMock) {
if err != nil {
t.Fatal("creating an IPFSConnector should work: ", err)
}

ipfs.server.SetKeepAlivesEnabled(false)
ipfs.SetClient(test.NewMockRPCClient(t))
return ipfs, mock
}
Expand Down Expand Up @@ -528,8 +530,6 @@ func TestProxyAdd(t *testing.T) {
url := fmt.Sprintf("%s/add?"+tc.query, proxyURL(ipfs))
req, _ := http.NewRequest("POST", url, mr)
req.Header.Set("Content-Type", cType)
req.ContentLength = -1
req.Close = true
reqs[i] = req
}

Expand Down

0 comments on commit be651da

Please sign in to comment.