prysm-pulse/beacon-chain/sync/deadlines.go
Raul Jordan d077483577
Add V3 Suffix to All Prysm Packages (#11083)
* v3 import renamings

* tidy

* fmt

* rev

* Update beacon-chain/core/epoch/precompute/reward_penalty_test.go

* Update beacon-chain/core/helpers/validators_test.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/alias.go

* Update beacon-chain/db/iface/BUILD.bazel

* Update beacon-chain/db/kv/kv.go

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/rpc/prysm/v1alpha1/validator/attester_test.go

* Update beacon-chain/sync/initial-sync/service.go

* fix deps

* fix bad replacements

* fix bad replacements

* change back

* gohashtree version

* fix deps

Co-authored-by: Nishant Das <nishdas93@gmail.com>
Co-authored-by: Potuz <potuz@prysmaticlabs.com>
2022-08-16 12:20:13 +00:00

57 lines
2.2 KiB
Go

package sync
import (
"strings"
"time"
"github.com/libp2p/go-libp2p-core/network"
"github.com/prysmaticlabs/prysm/v3/config/params"
"github.com/sirupsen/logrus"
)
var defaultReadDuration = ttfbTimeout
var defaultWriteDuration = params.BeaconNetworkConfig().RespTimeout // RESP_TIMEOUT
// SetRPCStreamDeadlines sets read and write deadlines for libp2p-based connection streams.
func SetRPCStreamDeadlines(stream network.Stream) {
SetStreamReadDeadline(stream, defaultReadDuration)
SetStreamWriteDeadline(stream, defaultWriteDuration)
}
// 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) {
if err := stream.SetReadDeadline(time.Now().Add(duration)); err != nil &&
!strings.Contains(err.Error(), "stream closed") {
log.WithError(err).WithFields(logrus.Fields{
"peer": stream.Conn().RemotePeer(),
"protocol": stream.Protocol(),
"direction": stream.Stat().Direction,
}).Debug("Could not set stream deadline")
}
}
// 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) {
if err := stream.SetWriteDeadline(time.Now().Add(duration)); err != nil {
log.WithError(err).WithFields(logrus.Fields{
"peer": stream.Conn().RemotePeer(),
"protocol": stream.Protocol(),
"direction": stream.Stat().Direction,
}).Debug("Could not set stream deadline")
}
}