2019-08-16 20:03:11 +00:00
|
|
|
package sync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2020-04-16 22:21:44 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/params"
|
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
|
|
|
|
2019-08-16 20:03:11 +00:00
|
|
|
func setRPCStreamDeadlines(stream network.Stream) {
|
2019-09-24 14:56:50 +00:00
|
|
|
setStreamReadDeadline(stream, defaultReadDuration)
|
|
|
|
setStreamWriteDeadline(stream, defaultWriteDuration)
|
|
|
|
}
|
|
|
|
|
|
|
|
func setStreamReadDeadline(stream network.Stream, duration time.Duration) {
|
2019-09-25 17:00:04 +00:00
|
|
|
// libp2p uses the system clock time for determining the deadline so we use
|
|
|
|
// time.Now() instead of the synchronized roughtime.Now().
|
2020-04-13 04:11:09 +00:00
|
|
|
if err := stream.SetReadDeadline(time.Now().Add(duration)); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to set stream deadline")
|
|
|
|
}
|
2019-09-24 14:56:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setStreamWriteDeadline(stream network.Stream, duration time.Duration) {
|
2019-09-25 17:00:04 +00:00
|
|
|
// libp2p uses the system clock time for determining the deadline so we use
|
|
|
|
// time.Now() instead of the synchronized roughtime.Now().
|
2020-04-13 04:11:09 +00:00
|
|
|
if err := stream.SetWriteDeadline(time.Now().Add(duration)); err != nil {
|
|
|
|
log.WithError(err).Error("Failed to set stream deadline")
|
|
|
|
}
|
2019-08-16 20:03:11 +00:00
|
|
|
}
|