2019-08-16 20:03:11 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
2020-07-13 01:20:53 +00:00
|
|
|
"strings"
|
2019-08-16 20:03:11 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2020-04-16 22:21:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
2020-06-23 09:00:09 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-08-16 20:03:11 +00:00
|
|
|
)
|
|
|
|
|
2020-04-16 22:21:44 +00:00
|
|
|
var defaultReadDuration = ttfbTimeout
|
|
|
|
var defaultWriteDuration = params.BeaconNetworkConfig().RespTimeout // RESP_TIMEOUT
|
2019-09-24 14:56:50 +00:00
|
|
|
|
2020-06-25 22:36:18 +00:00
|
|
|
// SetRPCStreamDeadlines sets read and write deadlines for libp2p-based connection streams.
|
|
|
|
func SetRPCStreamDeadlines(stream network.Stream) {
|
|
|
|
SetStreamReadDeadline(stream, defaultReadDuration)
|
|
|
|
SetStreamWriteDeadline(stream, defaultWriteDuration)
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:36:18 +00:00
|
|
|
// SetStreamReadDeadline for reading from libp2p connection streams, deciding when to close
|
|
|
|
// a connection based on a particular duration.
|
|
|
|
//
|
|
|
|
// NOTE: libp2p uses the system clock time for determining the deadline so we use
|
|
|
|
// time.Now() instead of the synchronized roughtime.Now(). If the system
|
|
|
|
// time is corrupted (i.e. time does not advance), the node will experience
|
|
|
|
// issues being able to properly close streams, leading to unexpected failures and possible
|
|
|
|
// memory leaks.
|
|
|
|
func SetStreamReadDeadline(stream network.Stream, duration time.Duration) {
|
2020-07-13 01:20:53 +00:00
|
|
|
if err := stream.SetReadDeadline(time.Now().Add(duration)); err != nil &&
|
|
|
|
!strings.Contains(err.Error(), "stream closed") {
|
2020-06-23 09:00:09 +00:00
|
|
|
log.WithError(err).WithFields(logrus.Fields{
|
|
|
|
"peer": stream.Conn().RemotePeer(),
|
|
|
|
"protocol": stream.Protocol(),
|
|
|
|
"direction": stream.Stat().Direction,
|
|
|
|
}).Debug("Failed to set stream deadline")
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 22:36:18 +00:00
|
|
|
// SetStreamWriteDeadline for writing to libp2p connection streams, deciding when to close
|
|
|
|
// a connection based on a particular duration.
|
|
|
|
//
|
|
|
|
// NOTE: libp2p uses the system clock time for determining the deadline so we use
|
|
|
|
// time.Now() instead of the synchronized roughtime.Now(). If the system
|
|
|
|
// time is corrupted (i.e. time does not advance), the node will experience
|
|
|
|
// issues being able to properly close streams, leading to unexpected failures and possible
|
|
|
|
// memory leaks.
|
|
|
|
func SetStreamWriteDeadline(stream network.Stream, duration time.Duration) {
|
2020-04-13 04:11:09 +00:00
|
|
|
if err := stream.SetWriteDeadline(time.Now().Add(duration)); err != nil {
|
2020-06-23 09:00:09 +00:00
|
|
|
log.WithError(err).WithFields(logrus.Fields{
|
|
|
|
"peer": stream.Conn().RemotePeer(),
|
|
|
|
"protocol": stream.Protocol(),
|
|
|
|
"direction": stream.Stat().Direction,
|
|
|
|
}).Debug("Failed to set stream deadline")
|
2020-04-13 04:11:09 +00:00
|
|
|
}
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|