forked from tiglabs/raft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport_heartbeat.go
129 lines (115 loc) · 2.75 KB
/
transport_heartbeat.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
// Copyright 2018 The tiglabs raft Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package raft
import (
"net"
"sync"
//"fmt"
//"github.com/tiglabs/raft/logger"
"github.com/tiglabs/raft/proto"
"github.com/tiglabs/raft/util"
)
type heartbeatTransport struct {
config *TransportConfig
raftServer *RaftServer
listener net.Listener
mu sync.RWMutex
senders map[uint64]*transportSender
stopc chan struct{}
}
func newHeartbeatTransport(raftServer *RaftServer, config *TransportConfig) (*heartbeatTransport, error) {
var (
listener net.Listener
err error
)
if listener, err = net.Listen("tcp", config.HeartbeatAddr); err != nil {
return nil, err
}
t := &heartbeatTransport{
config: config,
raftServer: raftServer,
listener: listener,
senders: make(map[uint64]*transportSender),
stopc: make(chan struct{}),
}
return t, nil
}
func (t *heartbeatTransport) stop() {
t.mu.Lock()
defer t.mu.Unlock()
select {
case <-t.stopc:
return
default:
close(t.stopc)
t.listener.Close()
for _, s := range t.senders {
s.stop()
}
}
}
func (t *heartbeatTransport) start() {
util.RunWorkerUtilStop(func() {
for {
select {
case <-t.stopc:
return
default:
conn, err := t.listener.Accept()
if err != nil {
continue
}
t.handleConn(util.NewConnTimeout(conn))
}
}
}, t.stopc)
}
func (t *heartbeatTransport) handleConn(conn *util.ConnTimeout) {
util.RunWorker(func() {
defer conn.Close()
bufRd := util.NewBufferReader(conn, 16*KB)
for {
select {
case <-t.stopc:
return
default:
if msg, err := reciveMessage(bufRd); err != nil {
return
} else {
//logger.Debug(fmt.Sprintf("Recive %v from (%v)", msg.ToString(), conn.RemoteAddr()))
t.raftServer.reciveMessage(msg)
}
}
}
})
}
func (t *heartbeatTransport) send(msg *proto.Message) {
s := t.getSender(msg.To)
s.send(msg)
}
func (t *heartbeatTransport) getSender(nodeId uint64) *transportSender {
t.mu.RLock()
sender, ok := t.senders[nodeId]
t.mu.RUnlock()
if ok {
return sender
}
t.mu.Lock()
defer t.mu.Unlock()
if sender, ok = t.senders[nodeId]; !ok {
sender = newTransportSender(nodeId, 1, 64, HeartBeat, t.config.Resolver)
t.senders[nodeId] = sender
}
return sender
}