github.com/koko1123/flow-go-1@v0.29.6/network/relay/network.go (about)

     1  package relay
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/ipfs/go-datastore"
     7  	"github.com/libp2p/go-libp2p/core/protocol"
     8  	"github.com/rs/zerolog"
     9  
    10  	"github.com/koko1123/flow-go-1/module/irrecoverable"
    11  	"github.com/koko1123/flow-go-1/module/util"
    12  	"github.com/koko1123/flow-go-1/network"
    13  	"github.com/koko1123/flow-go-1/network/channels"
    14  )
    15  
    16  type RelayNetwork struct {
    17  	originNet      network.Network
    18  	destinationNet network.Network
    19  	logger         zerolog.Logger
    20  	channels       map[channels.Channel]channels.Channel
    21  }
    22  
    23  var _ network.Network = (*RelayNetwork)(nil)
    24  
    25  func NewRelayNetwork(
    26  	originNetwork network.Network,
    27  	destinationNetwork network.Network,
    28  	logger zerolog.Logger,
    29  	channels map[channels.Channel]channels.Channel,
    30  ) *RelayNetwork {
    31  	return &RelayNetwork{
    32  		originNet:      originNetwork,
    33  		destinationNet: destinationNetwork,
    34  		logger:         logger.With().Str("component", "relay_network").Logger(),
    35  		channels:       channels,
    36  	}
    37  }
    38  
    39  func (r *RelayNetwork) Register(channel channels.Channel, messageProcessor network.MessageProcessor) (network.Conduit, error) {
    40  	// Only relay configured channels
    41  	dstChannel, ok := r.channels[channel]
    42  	if !ok {
    43  		return r.originNet.Register(channel, messageProcessor)
    44  	}
    45  
    46  	relayer, err := NewRelayer(r.destinationNet, dstChannel, messageProcessor)
    47  
    48  	if err != nil {
    49  		return nil, fmt.Errorf("failed to register relayer on origin network: %w", err)
    50  	}
    51  
    52  	conduit, err := r.originNet.Register(channel, relayer)
    53  
    54  	if err != nil {
    55  		if closeErr := relayer.Close(); closeErr != nil {
    56  			r.logger.Err(closeErr).Msg("failed to close relayer")
    57  		}
    58  
    59  		return nil, fmt.Errorf("failed to register relayer on destination network: %w", err)
    60  	}
    61  
    62  	return conduit, nil
    63  }
    64  
    65  func (r *RelayNetwork) Start(ctx irrecoverable.SignalerContext) {}
    66  
    67  func (r *RelayNetwork) Ready() <-chan struct{} {
    68  	return util.AllReady(r.originNet, r.destinationNet)
    69  }
    70  
    71  func (r *RelayNetwork) Done() <-chan struct{} {
    72  	return util.AllDone(r.originNet, r.destinationNet)
    73  }
    74  
    75  func (r *RelayNetwork) RegisterBlobService(channel channels.Channel, store datastore.Batching, opts ...network.BlobServiceOption) (network.BlobService, error) {
    76  	return r.originNet.RegisterBlobService(channel, store, opts...)
    77  }
    78  
    79  func (r *RelayNetwork) RegisterPingService(pid protocol.ID, provider network.PingInfoProvider) (network.PingService, error) {
    80  	return r.originNet.RegisterPingService(pid, provider)
    81  }