-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtransport.go
232 lines (189 loc) · 5.31 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package libp2praft
import (
"context"
"errors"
"fmt"
"io"
"log"
"net"
"time"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/raft"
logging "github.com/ipfs/go-log/v2"
gostream "github.com/libp2p/go-libp2p-gostream"
"github.com/libp2p/go-libp2p/core/host"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
)
const RaftProtocol protocol.ID = "/raft/1.0.0/rpc"
var raftLogger = logging.Logger("raftlib")
// HcLogToLogger implements github.com/hashicorp/go-hclog
type HcLogToLogger struct {
extraArgs []interface{}
name string
}
func (log *HcLogToLogger) formatArgs(args []interface{}) string {
result := ""
args = append(args, log.extraArgs)
for i := 0; i < len(args); i = i + 2 {
key, ok := args[i].(string)
if !ok {
continue
}
val := args[i+1]
result += fmt.Sprintf(" %s=%s.", key, val)
}
return result
}
func (log *HcLogToLogger) format(msg string, args []interface{}) string {
argstr := log.formatArgs(args)
if len(argstr) > 0 {
argstr = ". Args: " + argstr
}
name := log.name
if len(name) > 0 {
name += ": "
}
return name + msg + argstr
}
func (log *HcLogToLogger) Log(level hclog.Level, msg string, args ...interface{}) {
switch level {
case hclog.Trace, hclog.Debug:
log.Debug(msg, args)
case hclog.NoLevel, hclog.Info:
log.Info(msg, args)
case hclog.Warn:
log.Warn(msg, args)
case hclog.Error:
log.Error(msg, args)
default:
log.Warn(msg, args)
}
}
func (log *HcLogToLogger) Trace(msg string, args ...interface{}) {
raftLogger.Debug(log.format(msg, args))
}
func (log *HcLogToLogger) Debug(msg string, args ...interface{}) {
raftLogger.Debug(log.format(msg, args))
}
func (log *HcLogToLogger) Info(msg string, args ...interface{}) {
raftLogger.Info(log.format(msg, args))
}
func (log *HcLogToLogger) Warn(msg string, args ...interface{}) {
raftLogger.Warn(log.format(msg, args))
}
func (log *HcLogToLogger) Error(msg string, args ...interface{}) {
raftLogger.Error(log.format(msg, args))
}
func (log *HcLogToLogger) IsTrace() bool {
return true
}
func (log *HcLogToLogger) IsDebug() bool {
return true
}
func (log *HcLogToLogger) IsInfo() bool {
return true
}
func (log *HcLogToLogger) IsWarn() bool {
return true
}
func (log *HcLogToLogger) IsError() bool {
return true
}
func (log *HcLogToLogger) Name() string {
return log.name
}
func (log *HcLogToLogger) With(args ...interface{}) hclog.Logger {
return &HcLogToLogger{extraArgs: args}
}
func (log *HcLogToLogger) Named(name string) hclog.Logger {
return &HcLogToLogger{name: log.name + ": " + name}
}
func (log *HcLogToLogger) ResetNamed(name string) hclog.Logger {
return &HcLogToLogger{name: name}
}
func (log *HcLogToLogger) SetLevel(level hclog.Level) {}
func (l *HcLogToLogger) GetLevel() hclog.Level {
return hclog.LevelFromString("DEBUG")
}
func (log *HcLogToLogger) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
return nil
}
func (log *HcLogToLogger) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
return nil
}
func (log *HcLogToLogger) ImpliedArgs() []interface{} {
return nil
}
// streamLayer an implementation of raft.StreamLayer for use
// with raft.NetworkTransportConfig.
type streamLayer struct {
host host.Host
l net.Listener
}
func newStreamLayer(h host.Host) (*streamLayer, error) {
listener, err := gostream.Listen(h, RaftProtocol)
if err != nil {
return nil, err
}
return &streamLayer{
host: h,
l: listener,
}, nil
}
func (sl *streamLayer) Dial(address raft.ServerAddress, timeout time.Duration) (net.Conn, error) {
if sl.host == nil {
return nil, errors.New("streamLayer not initialized")
}
pid, err := peer.Decode(string(address))
if err != nil {
return nil, err
}
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
return gostream.Dial(ctx, sl.host, pid, RaftProtocol)
}
func (sl *streamLayer) Accept() (net.Conn, error) {
return sl.l.Accept()
}
func (sl *streamLayer) Addr() net.Addr {
return sl.l.Addr()
}
func (sl *streamLayer) Close() error {
return sl.l.Close()
}
type addrProvider struct {
h host.Host
}
// ServerAddr takes a raft.ServerID and returns it as a ServerAddress. libp2p
// will either know how to contact that peer ID or try to find it using the
// configured routing mechanism.
func (ap *addrProvider) ServerAddr(id raft.ServerID) (raft.ServerAddress, error) {
return raft.ServerAddress(id), nil
}
// type Libp2pTransport struct {
// raftTrans *raft.NetworkTransport
// }
func NewLibp2pTransport(h host.Host, timeout time.Duration) (*raft.NetworkTransport, error) {
provider := &addrProvider{h}
stream, err := newStreamLayer(h)
if err != nil {
return nil, err
}
// This is a configuration for raft.NetworkTransport
// initialized with our own StreamLayer and Logger.
// We set MaxPool to 0 so the NetworkTransport does not
// pool connections. This allows re-using already stablished
// TCP connections, for example, which are expensive to create.
// We are, however, multiplexing streams over an already created
// Libp2p connection, which is cheap. We don't need to re-use
// streams.
cfg := &raft.NetworkTransportConfig{
ServerAddressProvider: provider,
Logger: &HcLogToLogger{},
Stream: stream,
MaxPool: 0,
Timeout: timeout,
}
return raft.NewNetworkTransportWithConfig(cfg), nil
}