Skip to content
This repository has been archived by the owner on Sep 9, 2022. It is now read-only.

Timeout Stream Read #124

Merged
merged 2 commits into from
Dec 18, 2020
Merged
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
6 changes: 6 additions & 0 deletions relay.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ var (

HopStreamBufferSize = 4096
HopStreamLimit = 1 << 19 // 512K hops for 1M goroutines

streamTimeout = 1 * time.Minute
)

// Relay is the relay transport and service.
Expand Down Expand Up @@ -240,6 +242,8 @@ func (r *Relay) CanHop(ctx context.Context, id peer.ID) (bool, error) {
}

func (r *Relay) handleNewStream(s network.Stream) {
s.SetReadDeadline(time.Now().Add(streamTimeout))

log.Infof("new relay stream from: %s", s.Conn().RemotePeer())

rd := newDelimitedReader(s, maxMessageSize)
Expand All @@ -252,6 +256,8 @@ func (r *Relay) handleNewStream(s network.Stream) {
r.handleError(s, pb.CircuitRelay_MALFORMED_MESSAGE)
return
}
// reset stream deadline as message has been read
s.SetReadDeadline(time.Time{})

switch msg.GetType() {
case pb.CircuitRelay_HOP:
Expand Down