github.com/ethersphere/bee/v2@v2.2.0/pkg/p2p/specwrapper.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package p2p
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"time"
    11  )
    12  
    13  var ErrUnexpected = errors.New("unexpected request while in light mode")
    14  
    15  // WithDisconnectStreams will mutate the given spec and replace the handler with a always erroring one.
    16  func WithDisconnectStreams(spec ProtocolSpec) {
    17  	for i := range spec.StreamSpecs {
    18  		spec.StreamSpecs[i].Handler = func(c context.Context, p Peer, s Stream) error {
    19  			return NewDisconnectError(ErrUnexpected)
    20  		}
    21  	}
    22  }
    23  
    24  // WithBlocklistStreams will mutate the given spec and replace the handler with a always erroring one.
    25  func WithBlocklistStreams(dur time.Duration, spec ProtocolSpec) {
    26  	for i := range spec.StreamSpecs {
    27  		spec.StreamSpecs[i].Handler = func(c context.Context, p Peer, s Stream) error {
    28  			return NewBlockPeerError(dur, ErrUnexpected)
    29  		}
    30  	}
    31  }