-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
70 lines (57 loc) · 1.46 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
// Package transport provides a Transport for github.com/hashicorp/raft over Capnproto.
package foobar
import (
"sync"
"time"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/raft"
"github.com/pkg/errors"
)
var (
errCloseErr = errors.New("error closing connections")
)
type Manager struct {
localAddress raft.ServerAddress
//dialOptions []grpc.DialOption
rpcChan chan raft.RPC
heartbeatFunc func(raft.RPC)
heartbeatFuncMtx sync.Mutex
heartbeatTimeout time.Duration
connectionsMtx sync.Mutex
connections map[raft.ServerID]*conn
}
// New creates both components of raft-grpc-transport: a gRPC service and a Raft Transport.
func New(localAddress raft.ServerAddress, options ...Option) *Manager {
m := &Manager{
localAddress: localAddress,
//dialOptions: dialOptions,
rpcChan: make(chan raft.RPC),
connections: map[raft.ServerID]*conn{},
}
for _, opt := range options {
opt(m)
}
return m
}
// Transport returns a raft.Transport that communicates over gRPC.
func (m *Manager) Transport() raft.Transport {
return raftAPI{m}
}
func (m *Manager) Close() error {
m.connectionsMtx.Lock()
defer m.connectionsMtx.Unlock()
err := errCloseErr
for _, conn := range m.connections {
// Lock conn.mtx to ensure Dial() is complete
conn.mtx.Lock()
conn.mtx.Unlock()
closeErr := conn.clientConn.Close()
if closeErr != nil {
err = multierror.Append(err, closeErr)
}
}
if err != errCloseErr {
return err
}
return nil
}