Loading...
Searching...
No Matches
acceptor.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
21
22#include <glog/logging.h>
23#include <signal.h>
24
25#include <thread>
26
28
29namespace resdb {
30
32 LockFreeQueue<QueueItem>* input_queue)
33 : socket_(std::make_unique<TcpSocket>()),
34 config_(config),
35 input_queue_(input_queue) {
36 socket_->SetRecvTimeout(1000000); // set 1s timeout.
37
38 LOG(ERROR) << "listen ip:" << config.GetSelfInfo().ip()
39 << " port:" << config.GetSelfInfo().port();
40 assert(socket_->Listen(config.GetSelfInfo().ip(),
41 config.GetSelfInfo().port()) == 0);
42 is_stop_ = false;
43 global_stats_ = Stats::GetGlobalStats();
44}
45
47
49 LOG(ERROR) << "server:" << config_.GetSelfInfo().id() << " start running";
50
51 LockFreeQueue<Socket> socket_queue("server");
52 std::vector<std::thread> threads;
53 int woker_num = config_.GetInputWorkerNum();
54 for (int i = 0; i < woker_num; ++i) {
55 threads.push_back(std::thread([&]() {
56 while (IsRunning()) {
57 auto client_socket = socket_queue.Pop();
58 if (client_socket == nullptr) {
59 continue;
60 }
61 std::unique_ptr<DataInfo> request_info = std::make_unique<DataInfo>();
62 int ret =
63 client_socket->Recv(&request_info->buff, &request_info->data_len);
64 if (ret <= 0) {
65 continue;
66 }
67 std::unique_ptr<QueueItem> item = std::make_unique<QueueItem>();
68 item->socket = std::move(client_socket);
69 item->data = std::move(request_info);
70 global_stats_->ServerCall();
71 input_queue_->Push(std::move(item));
72 }
73 }));
74 }
75
76 while (IsRunning()) {
77 auto client_socket = socket_->Accept();
78 if (client_socket == nullptr) {
79 continue;
80 }
81 socket_queue.Push(std::move(client_socket));
82 }
83
84 for (auto& th : threads) {
85 if (th.joinable()) {
86 th.join();
87 }
88 }
89}
90
92 is_stop_ = true;
93 socket_->Close();
94}
95
96bool Acceptor::IsRunning() { return !is_stop_; }
97
98} // namespace resdb
virtual ~Acceptor()
Definition acceptor.cpp:46
Acceptor(const ResDBConfig &config, LockFreeQueue< QueueItem > *input_queue)
Definition acceptor.cpp:31
void Push(std::unique_ptr< T > data)
std::unique_ptr< T > Pop(int timeout_ms=100)
uint32_t GetInputWorkerNum() const
const ReplicaInfo & GetSelfInfo() const
static Stats * GetGlobalStats(int sleep_seconds=5)
Definition stats.cpp:29
void ServerCall()
Definition stats.cpp:285
std::unique_ptr< ResDBConfig > config_