github.com/aergoio/aergo@v1.3.1/consensus/impl/raftv2/transport.go (about)

     1  /*
     2   * @file
     3   * @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package raftv2
     7  
     8  import (
     9  	"github.com/aergoio/aergo/types"
    10  	rtypes "github.com/aergoio/etcd/pkg/types"
    11  	"github.com/aergoio/etcd/raft/raftpb"
    12  	"github.com/aergoio/etcd/rafthttp"
    13  	"github.com/aergoio/etcd/snap"
    14  	"net/http"
    15  	"time"
    16  )
    17  
    18  type Transporter interface {
    19  	// Start starts the given Transporter.
    20  	// Start MUST be called before calling other functions in the interface.
    21  	Start() error
    22  	// Handler returns the HTTP handler of the transporter.
    23  	// A transporter HTTP handler handles the HTTP requests
    24  	// from remote peers.
    25  	// The handler MUST be used to handle RaftPrefix(/raft)
    26  	// endpoint.
    27  	Handler() http.Handler
    28  	// Send sends out the given messages to the remote peers.
    29  	// Each message has a To field, which is an id that maps
    30  	// to an existing peer in the transport.
    31  	// If the id cannot be found in the transport, the message
    32  	// will be ignored.
    33  	Send(m []raftpb.Message)
    34  	// SendSnapshot sends out the given snapshot message to a remote peer.
    35  	// The behavior of SendSnapshot is similar to Send.
    36  	SendSnapshot(m snap.Message)
    37  
    38  	// AddPeer adds a peer with given peer urls into the transport.
    39  	// It is the caller's responsibility to ensure the urls are all valid,
    40  	// or it panics.
    41  	// Peer urls are used to connect to the remote peer.
    42  	AddPeer(id rtypes.ID, peerID types.PeerID, urls []string)
    43  
    44  	// RemovePeer removes the peer with given id.
    45  	RemovePeer(id rtypes.ID)
    46  	// RemoveAllPeers removes all the existing peers in the transport.
    47  	RemoveAllPeers()
    48  	// UpdatePeer updates the peer urls of the peer with the given id.
    49  	// It is the caller's responsibility to ensure the urls are all valid,
    50  	// or it panics.
    51  	UpdatePeer(id rtypes.ID, urls []string)
    52  	// ActiveSince returns the time that the connection with the peer
    53  	// of the given id becomes active.
    54  	// If the connection is active since peer was added, it returns the adding time.
    55  	// If the connection is currently inactive, it returns zero time.
    56  	ActiveSince(id rtypes.ID) time.Time
    57  	// ActivePeers returns the number of active peers.
    58  	ActivePeers() int
    59  	// Stop closes the connections and stops the transporter.
    60  	Stop()
    61  }
    62  
    63  type HttpTransportWrapper struct {
    64  	rafthttp.Transport
    65  }
    66  
    67  func (t *HttpTransportWrapper)AddPeer(id rtypes.ID, peerID types.PeerID, urls []string) {
    68  	t.Transport.AddPeer(id, urls)
    69  }