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