rpcenc: Add timeout for readers

This commit is contained in:
Łukasz Magiera 2020-08-14 23:12:37 +02:00
parent 0377d6952b
commit aa611e49fd

View File

@ -11,6 +11,7 @@ import (
"reflect" "reflect"
"strconv" "strconv"
"sync" "sync"
"time"
"github.com/google/uuid" "github.com/google/uuid"
logging "github.com/ipfs/go-log/v2" logging "github.com/ipfs/go-log/v2"
@ -21,7 +22,9 @@ import (
sealing "github.com/filecoin-project/storage-fsm" sealing "github.com/filecoin-project/storage-fsm"
) )
var log = logging.Logger("rpc") var log = logging.Logger("rpcenc")
var Timeout = 30 * time.Second
type StreamType string type StreamType string
const ( const (
@ -111,10 +114,14 @@ func ReaderParamDecoder() (http.HandlerFunc, jsonrpc.ServerOption) {
wait: make(chan struct{}), wait: make(chan struct{}),
} }
tctx, cancel := context.WithTimeout(req.Context(), Timeout)
defer cancel()
select { select {
case ch <- wr: case ch <- wr:
case <-req.Context().Done(): case <-tctx.Done():
log.Error("context error in reader stream handler (1): %v", req.Context().Err()) close(ch)
log.Error("context error in reader stream handler (1): %v", tctx.Err())
resp.WriteHeader(500) resp.WriteHeader(500)
return return
} }
@ -158,8 +165,15 @@ func ReaderParamDecoder() (http.HandlerFunc, jsonrpc.ServerOption) {
} }
readersLk.Unlock() readersLk.Unlock()
ctx, cancel := context.WithTimeout(ctx, Timeout)
defer cancel()
select { select {
case wr := <-ch: case wr, ok := <-ch:
if !ok {
return reflect.Value{}, xerrors.Errorf("handler timed out")
}
return reflect.ValueOf(wr), nil return reflect.ValueOf(wr), nil
case <-ctx.Done(): case <-ctx.Done():
return reflect.Value{}, ctx.Err() return reflect.Value{}, ctx.Err()