From 3c128b725c8fd8daffdc3c129292e6f2c35ccd26 Mon Sep 17 00:00:00 2001 From: Dave Collins Date: Fri, 1 May 2020 06:08:00 -0500 Subject: [PATCH] peer: Set a default idle timeout if not specified. This modifies the peer initialization code to set a default idle timeout of two minutes if the caller did not specify one. This is desirable since the default is the zero value otherwise, which results in an immediate disconnection for callers that don't specify a timeout. It is worth noting that the server code already has a default setting that can be configured via the command-line or a configuration file which is used to configure the peers, so this has no effect on dcrd, but it is a nice addition for other consumers of the peer package. --- peer/peer.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/peer/peer.go b/peer/peer.go index e96d2df9..55f607ca 100644 --- a/peer/peer.go +++ b/peer/peer.go @@ -60,6 +60,11 @@ const ( // trickleTimeout is the duration of the ticker which trickles down the // inventory to a peer. trickleTimeout = 500 * time.Millisecond + + // defaultIdleTimeout is the default duration of inactivity before a peer is + // timed out when a peer is created with the idle timeout configuration + // option set to 0. + defaultIdleTimeout = 120 * time.Second ) var ( @@ -2072,6 +2077,11 @@ func newPeerBase(cfgOrig *Config, inbound bool) *Peer { cfg.Net = wire.TestNet3 } + // Set a default idle timeout if the caller did not specify one. + if cfg.IdleTimeout == 0 { + cfg.IdleTimeout = defaultIdleTimeout + } + p := Peer{ inbound: inbound, knownInventory: lru.NewCache(maxKnownInventory),