Skip to content

Commit

Permalink
chore: change option name
Browse files Browse the repository at this point in the history
  • Loading branch information
vgonkivs committed Jan 18, 2023
1 parent 9ca1a5d commit 426e296
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 29 deletions.
4 changes: 2 additions & 2 deletions header/p2p/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (ex *Exchange) GetRangeByHeight(ctx context.Context, from, amount uint64) (
if amount > ex.Params.MaxRequestSize {
return nil, header.ErrHeadersLimitExceeded
}
session := newSession(ex.ctx, ex.host, ex.peerTracker, ex.protocolID, ex.Params.RequestDuration)
session := newSession(ex.ctx, ex.host, ex.peerTracker, ex.protocolID, ex.Params.RequestTimeout)
defer session.close()
return session.getRangeByHeight(ctx, from, amount, ex.Params.MaxHeadersPerRequest)
}
Expand All @@ -179,7 +179,7 @@ func (ex *Exchange) GetVerifiedRange(
from *header.ExtendedHeader,
amount uint64,
) ([]*header.ExtendedHeader, error) {
session := newSession(ex.ctx, ex.host, ex.peerTracker, ex.protocolID, ex.Params.RequestDuration, withValidation(from))
session := newSession(ex.ctx, ex.host, ex.peerTracker, ex.protocolID, ex.Params.RequestTimeout, withValidation(from))
defer session.close()

return session.getRangeByHeight(ctx, uint64(from.Height)+1, amount, ex.Params.MaxHeadersPerRequest)
Expand Down
4 changes: 2 additions & 2 deletions header/p2p/exchange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,12 +321,12 @@ func TestExchange_RequestHeadersFromAnotherPeerWhenTimeout(t *testing.T) {

// create client + server(it does not have needed headers)
exchg, _ := createP2PExAndServer(t, host0, host1)
exchg.Params.RequestDuration = time.Millisecond * 100
exchg.Params.RequestTimeout = time.Millisecond * 100
// create one more server(with more headers in the store)
serverSideEx, err := NewExchangeServer(host2, headerMock.NewStore(t, 10), "private")
require.NoError(t, err)
// change store implementation
serverSideEx.getter = &timedOutStore{exchg.Params.RequestDuration}
serverSideEx.getter = &timedOutStore{exchg.Params.RequestTimeout}
require.NoError(t, serverSideEx.Start(context.Background()))
t.Cleanup(func() {
serverSideEx.Stop(context.Background()) //nolint:errcheck
Expand Down
36 changes: 18 additions & 18 deletions header/p2p/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ type ServerParameters struct {
ReadDeadline time.Duration
// MaxRequestSize defines the max amount of headers that can be handled at once.
MaxRequestSize uint64
// RequestDuration defines a timeout after which the session will try to re-request headers
// RequestTimeout defines a timeout after which the session will try to re-request headers
// from another peer.
RequestDuration time.Duration
RequestTimeout time.Duration
}

// DefaultServerParameters returns the default params to configure the store.
func DefaultServerParameters() ServerParameters {
return ServerParameters{
WriteDeadline: time.Second * 5,
ReadDeadline: time.Minute,
MaxRequestSize: 512,
RequestDuration: time.Second * 5,
WriteDeadline: time.Second * 5,
ReadDeadline: time.Minute,
MaxRequestSize: 512,
RequestTimeout: time.Second * 5,
}
}

Expand All @@ -48,9 +48,9 @@ func (p *ServerParameters) Validate() error {
if p.MaxRequestSize == 0 {
return fmt.Errorf("invalid max request size: %d", p.MaxRequestSize)
}
if p.RequestDuration == 0 {
if p.RequestTimeout == 0 {
return fmt.Errorf("invalid request duration for session: "+
"%s. %s: %v", greaterThenZero, providedSuffix, p.RequestDuration)
"%s. %s: %v", greaterThenZero, providedSuffix, p.RequestTimeout)
}
return nil
}
Expand Down Expand Up @@ -90,15 +90,15 @@ func WithMaxRequestSize[T parameters](size uint64) Option[T] {
}
}

// WithRequestDuration is a functional option that configures the
// `RequestDuration` parameter.
func WithRequestDuration[T parameters](duration time.Duration) Option[T] {
// WithRequestTimeout is a functional option that configures the
// `RequestTimeout` parameter.
func WithRequestTimeout[T parameters](duration time.Duration) Option[T] {
return func(p *T) {
switch t := any(p).(type) {
case *ClientParameters:
t.RequestDuration = duration
t.RequestTimeout = duration
case *ServerParameters:
t.RequestDuration = duration
t.RequestTimeout = duration
}
}
}
Expand All @@ -116,9 +116,9 @@ type ClientParameters struct {
MaxAwaitingTime time.Duration
// DefaultScore specifies the score for newly connected peers.
DefaultScore float32
// RequestDuration defines a timeout after which the session will try to re-request headers
// RequestTimeout defines a timeout after which the session will try to re-request headers
// from another peer.
RequestDuration time.Duration
RequestTimeout time.Duration
// MaxTrackerSize specifies the max amount of peers that can be added to the peerTracker.
MaxPeerTrackerSize int
}
Expand All @@ -131,7 +131,7 @@ func DefaultClientParameters() ClientParameters {
MaxHeadersPerRequest: 64,
MaxAwaitingTime: time.Hour,
DefaultScore: 1,
RequestDuration: time.Second * 3,
RequestTimeout: time.Second * 3,
MaxPeerTrackerSize: 100,
}
}
Expand Down Expand Up @@ -163,9 +163,9 @@ func (p *ClientParameters) Validate() error {
if p.DefaultScore <= 0 {
return fmt.Errorf("invalid DefaultScore: %s. %s: %f", greaterThenZero, providedSuffix, p.DefaultScore)
}
if p.RequestDuration == 0 {
if p.RequestTimeout == 0 {
return fmt.Errorf("invalid request duration for session: "+
"%s. %s: %v", greaterThenZero, providedSuffix, p.RequestDuration)
"%s. %s: %v", greaterThenZero, providedSuffix, p.RequestTimeout)
}
if p.MaxPeerTrackerSize <= 0 {
return fmt.Errorf("invalid MaxTrackerSize: %s. %s: %d", greaterThenZero, providedSuffix, p.MaxPeerTrackerSize)
Expand Down
6 changes: 3 additions & 3 deletions header/p2p/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (serv *ExchangeServer) requestHandler(stream network.Stream) {
// if it exists.
func (serv *ExchangeServer) handleRequestByHash(hash []byte) ([]*header.ExtendedHeader, error) {
log.Debugw("server: handling header request", "hash", tmbytes.HexBytes(hash).String())
ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestDuration)
ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestTimeout)
defer cancel()
ctx, span := tracer.Start(ctx, "request-by-hash", trace.WithAttributes(
attribute.String("hash", tmbytes.HexBytes(hash).String()),
Expand Down Expand Up @@ -188,7 +188,7 @@ func (serv *ExchangeServer) handleRequest(from, to uint64) ([]*header.ExtendedHe
return serv.handleHeadRequest()
}

ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestDuration)
ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestTimeout)
defer cancel()

ctx, span := tracer.Start(ctx, "request-range", trace.WithAttributes(
Expand Down Expand Up @@ -223,7 +223,7 @@ func (serv *ExchangeServer) handleRequest(from, to uint64) ([]*header.ExtendedHe
// handleHeadRequest returns the latest stored head.
func (serv *ExchangeServer) handleHeadRequest() ([]*header.ExtendedHeader, error) {
log.Debug("server: handling head request")
ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestDuration)
ctx, cancel := context.WithTimeout(serv.ctx, serv.Params.RequestTimeout)
defer cancel()
ctx, span := tracer.Start(ctx, "request-head")
defer span.End()
Expand Down
4 changes: 2 additions & 2 deletions nodebuilder/header/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func ConstructModule(tp node.Type, cfg *Config) fx.Option {
p2p.WithWriteDeadline(cfg.Server.WriteDeadline),
p2p.WithReadDeadline(cfg.Server.ReadDeadline),
p2p.WithMaxRequestSize[p2p.ServerParameters](cfg.Server.MaxRequestSize),
p2p.WithRequestDuration[p2p.ServerParameters](cfg.Server.RequestDuration),
p2p.WithRequestTimeout[p2p.ServerParameters](cfg.Server.RequestTimeout),
}
}),
fx.Provide(NewHeaderService),
Expand Down Expand Up @@ -111,7 +111,7 @@ func ConstructModule(tp node.Type, cfg *Config) fx.Option {
p2p.WithMaxHeadersPerRequest(cfg.Client.MaxHeadersPerRequest),
p2p.WithMaxAwaitingTime(cfg.Client.MaxAwaitingTime),
p2p.WithDefaultScore(cfg.Client.DefaultScore),
p2p.WithRequestDuration[p2p.ClientParameters](cfg.Client.RequestDuration),
p2p.WithRequestTimeout[p2p.ClientParameters](cfg.Client.RequestTimeout),
p2p.WithMaxTrackerSize(cfg.Client.MaxPeerTrackerSize),
}
},
Expand Down
4 changes: 2 additions & 2 deletions nodebuilder/header/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,10 @@ func TestConstructModule_ExchangeParams(t *testing.T) {
require.Equal(t, exchange.Params.MaxAwaitingTime, cfg.Client.MaxAwaitingTime)
require.Equal(t, exchange.Params.DefaultScore, cfg.Client.DefaultScore)
require.Equal(t, exchange.Params.MaxPeerTrackerSize, cfg.Client.MaxPeerTrackerSize)
require.Equal(t, exchange.Params.RequestDuration, cfg.Client.RequestDuration)
require.Equal(t, exchange.Params.RequestTimeout, cfg.Client.RequestTimeout)

require.Equal(t, exchangeServer.Params.WriteDeadline, cfg.Server.WriteDeadline)
require.Equal(t, exchangeServer.Params.ReadDeadline, cfg.Server.ReadDeadline)
require.Equal(t, exchangeServer.Params.MaxRequestSize, cfg.Server.MaxRequestSize)
require.Equal(t, exchangeServer.Params.RequestDuration, cfg.Server.RequestDuration)
require.Equal(t, exchangeServer.Params.RequestTimeout, cfg.Server.RequestTimeout)
}

0 comments on commit 426e296

Please sign in to comment.