Skip to content

Commit

Permalink
fix(api): streaming chunk upload for long running connections
Browse files Browse the repository at this point in the history
  • Loading branch information
alok committed Jun 28, 2022
1 parent 9d77690 commit 23f356d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
14 changes: 11 additions & 3 deletions pkg/api/chunk_stream.go
Expand Up @@ -14,17 +14,20 @@ import (
"github.com/ethersphere/bee/pkg/cac"
"github.com/ethersphere/bee/pkg/jsonhttp"
"github.com/ethersphere/bee/pkg/postage"
"github.com/ethersphere/bee/pkg/sctx"
"github.com/ethersphere/bee/pkg/storage"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/tags"
"github.com/gorilla/websocket"
)

const streamReadTimeout = 15 * time.Minute

var successWsMsg = []byte{}

func (s *Service) chunkUploadStreamHandler(w http.ResponseWriter, r *http.Request) {

ctx, tag, putter, wait, err := s.processUploadRequest(r)
_, tag, putter, wait, err := s.processUploadRequest(r)
if err != nil {
jsonhttp.BadRequest(w, err.Error())
return
Expand All @@ -44,9 +47,14 @@ func (s *Service) chunkUploadStreamHandler(w http.ResponseWriter, r *http.Reques
return
}

cctx := context.Background()
if tag != nil {
cctx = sctx.SetTag(cctx, tag)
}

s.wsWg.Add(1)
go s.handleUploadStream(
ctx,
cctx,
c,
tag,
putter,
Expand Down Expand Up @@ -120,7 +128,7 @@ func (s *Service) handleUploadStream(
// if there is no indication to stop, go ahead and read the next message
}

err = conn.SetReadDeadline(time.Now().Add(readDeadline))
err = conn.SetReadDeadline(time.Now().Add(streamReadTimeout))
if err != nil {
s.logger.Debugf("chunk stream handler: set read deadline: %v", err)
s.logger.Error("chunk stream handler: set read deadline")
Expand Down
1 change: 0 additions & 1 deletion pkg/api/pss.go
Expand Up @@ -26,7 +26,6 @@ import (

const (
writeDeadline = 4 * time.Second // write deadline. should be smaller than the shutdown timeout on api close
readDeadline = 4 * time.Second // read deadline. should be smaller than the shutdown timeout on api close
targetMaxLength = 3 // max target length in bytes, in order to prevent grieving by excess computation
)

Expand Down

0 comments on commit 23f356d

Please sign in to comment.