@@ -20,6 +20,7 @@ class TcpServerTmpl {
20
20
tls = false ;
21
21
unpack_setting.mode = UNPACK_MODE_NONE;
22
22
max_connections = 0xFFFFFFFF ;
23
+ load_balance = LB_RoundRobin;
23
24
}
24
25
25
26
virtual ~TcpServerTmpl () {
@@ -46,6 +47,10 @@ class TcpServerTmpl {
46
47
max_connections = num;
47
48
}
48
49
50
+ void setLoadBalance (load_balance_e lb) {
51
+ load_balance = lb;
52
+ }
53
+
49
54
// NOTE: totalThreadNum = 1 acceptor_thread + N worker_threads (N can be 0)
50
55
void setThreadNum (int num) {
51
56
worker_threads.setThreadNum (num);
@@ -168,6 +173,10 @@ class TcpServerTmpl {
168
173
}
169
174
};
170
175
channel->onclose = [server, &channel]() {
176
+ EventLoop* worker_loop = currentThreadEventLoop;
177
+ assert (worker_loop != NULL );
178
+ --worker_loop->connectionNum ;
179
+
171
180
channel->status = SocketChannel::CLOSED;
172
181
if (server->onConnection ) {
173
182
server->onConnection (channel);
@@ -190,11 +199,11 @@ class TcpServerTmpl {
190
199
TcpServerTmpl* server = (TcpServerTmpl*)hevent_userdata (connio);
191
200
// NOTE: detach from acceptor loop
192
201
hio_detach (connio);
193
- // Load Banlance: Round-Robin
194
- EventLoopPtr worker_loop = server->worker_threads .nextLoop ();
202
+ EventLoopPtr worker_loop = server->worker_threads .nextLoop (server->load_balance );
195
203
if (worker_loop == NULL ) {
196
204
worker_loop = server->acceptor_thread .loop ();
197
205
}
206
+ ++worker_loop->connectionNum ;
198
207
worker_loop->runInLoop (std::bind (&TcpServerTmpl::newConnEvent, connio));
199
208
}
200
209
@@ -209,6 +218,7 @@ class TcpServerTmpl {
209
218
std::function<void (const TSocketChannelPtr&, Buffer*)> onWriteComplete;
210
219
211
220
uint32_t max_connections;
221
+ load_balance_e load_balance;
212
222
213
223
private:
214
224
// id => TSocketChannelPtr
0 commit comments