Compare commits
4 Commits
b5b0cbd599
...
89876f5bd6
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89876f5bd6 | ||
|
|
16bd9ddc3e | ||
|
|
ac42dd07dc | ||
|
|
88b1131104 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +1,6 @@
|
|||||||
[submodule "firebuf"]
|
[submodule "firebuf"]
|
||||||
path = firebuf
|
path = firebuf
|
||||||
url = ../firebuf.git
|
url = ../firebuf.git
|
||||||
|
[submodule "fireusage"]
|
||||||
|
path = fireusage
|
||||||
|
url = ../fireusage.git
|
||||||
|
|||||||
8
Makefile
8
Makefile
@@ -10,13 +10,16 @@ objects = server.o connection.o request.o parse.o
|
|||||||
firebuf/firebuf.o:
|
firebuf/firebuf.o:
|
||||||
$(MAKE) --directory=firebuf firebuf.o
|
$(MAKE) --directory=firebuf firebuf.o
|
||||||
|
|
||||||
|
fireusage/fireusage.o:
|
||||||
|
$(MAKE) --directory=fireusage fireusage.o
|
||||||
|
|
||||||
firecgi.a: $(objects)
|
firecgi.a: $(objects)
|
||||||
ar rcs $@ $^
|
ar rcs $@ $^
|
||||||
|
|
||||||
firecgi.o: $(objects) firebuf/firebuf.o
|
firecgi.o: $(objects) firebuf/firebuf.o fireusage/fireusage.o
|
||||||
gold -z relro -z now -r --output=$@ $+
|
gold -z relro -z now -r --output=$@ $+
|
||||||
|
|
||||||
firecgi.so: $(objects) firebuf/firebuf.o
|
firecgi.so: $(objects) firebuf/firebuf.o fireusage/fireusage.o
|
||||||
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDFLIBS)
|
$(FIRE_CXX) $(FIRE_CXXFLAGS) $(FIRE_LDFLAGS) -shared -o $@ $+ $(FIRE_LDFLIBS)
|
||||||
|
|
||||||
example_simple: example_simple.o firecgi.o
|
example_simple: example_simple.o firecgi.o
|
||||||
@@ -27,6 +30,7 @@ example_simple: example_simple.o firecgi.o
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
$(MAKE) --directory=firebuf clean
|
$(MAKE) --directory=firebuf clean
|
||||||
|
$(MAKE) --directory=fireusage clean
|
||||||
rm --force example_simple connection_afl *.so *.o *.a
|
rm --force example_simple connection_afl *.so *.o *.a
|
||||||
|
|
||||||
afl:
|
afl:
|
||||||
|
|||||||
@@ -9,10 +9,9 @@
|
|||||||
|
|
||||||
namespace firecgi {
|
namespace firecgi {
|
||||||
|
|
||||||
Connection::Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, const std::unordered_set<std::string_view>& headers, int max_request_len)
|
Connection::Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, int max_request_len)
|
||||||
: sock_(sock),
|
: sock_(sock),
|
||||||
callback_(callback),
|
callback_(callback),
|
||||||
headers_(headers),
|
|
||||||
buf_(sock, max_request_len),
|
buf_(sock, max_request_len),
|
||||||
request_(this) {
|
request_(this) {
|
||||||
char client_addr_str[INET6_ADDRSTRLEN];
|
char client_addr_str[INET6_ADDRSTRLEN];
|
||||||
@@ -104,9 +103,7 @@ int Connection::Read() {
|
|||||||
}
|
}
|
||||||
std::string_view value(value_buf, param_header->value_length);
|
std::string_view value(value_buf, param_header->value_length);
|
||||||
|
|
||||||
if (headers_.find(key) != headers_.end()) {
|
request_.AddParam(key, value);
|
||||||
request_.AddParam(key, value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -147,4 +144,8 @@ int Connection::Read() {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uint64_t Connection::Requests() const {
|
||||||
|
return requests_;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace firecgi
|
} // namespace firecgi
|
||||||
|
|||||||
@@ -13,16 +13,17 @@ namespace firecgi {
|
|||||||
|
|
||||||
class Connection {
|
class Connection {
|
||||||
public:
|
public:
|
||||||
Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, const std::unordered_set<std::string_view>& headers, int max_request_len);
|
Connection(int sock, const sockaddr_in6& client_addr, const std::function<void(Request*)>& callback, int max_request_len);
|
||||||
~Connection();
|
~Connection();
|
||||||
|
|
||||||
[[nodiscard]] int Read();
|
[[nodiscard]] int Read();
|
||||||
[[nodiscard]] bool Write(const std::vector<iovec>& vecs);
|
[[nodiscard]] bool Write(const std::vector<iovec>& vecs);
|
||||||
|
|
||||||
|
[[nodiscard]] uint64_t Requests() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const int sock_;
|
const int sock_;
|
||||||
const std::function<void(Request*)>& callback_;
|
const std::function<void(Request*)>& callback_;
|
||||||
const std::unordered_set<std::string_view>& headers_;
|
|
||||||
firebuf::StreamBuffer buf_;
|
firebuf::StreamBuffer buf_;
|
||||||
Request request_;
|
Request request_;
|
||||||
|
|
||||||
|
|||||||
@@ -15,5 +15,9 @@ int main(int argc, char *argv[]) {
|
|||||||
request->WriteBody("Hello world");
|
request->WriteBody("Hello world");
|
||||||
request->End();
|
request->End();
|
||||||
}, FLAGS_threads);
|
}, FLAGS_threads);
|
||||||
|
server.RegisterSignalHandlers();
|
||||||
server.Serve();
|
server.Serve();
|
||||||
|
|
||||||
|
gflags::ShutDownCommandLineFlags();
|
||||||
|
google::ShutdownGoogleLogging();
|
||||||
}
|
}
|
||||||
|
|||||||
1
fireusage
Submodule
1
fireusage
Submodule
Submodule fireusage added at 7972d253ab
@@ -28,7 +28,7 @@ void Request::NewRequest(uint16_t request_id) {
|
|||||||
body_written_ = false;
|
body_written_ = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t Request::RequestId() {
|
uint16_t Request::RequestId() const {
|
||||||
return request_id_;
|
return request_id_;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,7 +40,7 @@ void Request::SetBody(const std::string_view& body) {
|
|||||||
body_ = body;
|
body_ = body;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string_view& Request::GetParam(const std::string_view& key) {
|
const std::string_view& Request::GetParam(const std::string_view& key) const {
|
||||||
auto iter = params_.find(key);
|
auto iter = params_.find(key);
|
||||||
if (iter == params_.end()) {
|
if (iter == params_.end()) {
|
||||||
static const std::string_view none;
|
static const std::string_view none;
|
||||||
@@ -49,7 +49,7 @@ const std::string_view& Request::GetParam(const std::string_view& key) {
|
|||||||
return iter->second;
|
return iter->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::string_view& Request::GetBody() {
|
const std::string_view& Request::GetBody() const {
|
||||||
return body_;
|
return body_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,13 +18,13 @@ class Request {
|
|||||||
|
|
||||||
void NewRequest(uint16_t request_id);
|
void NewRequest(uint16_t request_id);
|
||||||
|
|
||||||
uint16_t RequestId();
|
uint16_t RequestId() const;
|
||||||
|
|
||||||
void AddParam(const std::string_view& key, const std::string_view& value);
|
void AddParam(const std::string_view& key, const std::string_view& value);
|
||||||
void SetBody(const std::string_view& in);
|
void SetBody(const std::string_view& in);
|
||||||
|
|
||||||
const std::string_view& GetParam(const std::string_view& key);
|
const std::string_view& GetParam(const std::string_view& key) const;
|
||||||
const std::string_view& GetBody();
|
const std::string_view& GetBody() const;
|
||||||
|
|
||||||
void WriteHeader(const std::string_view& name, const std::string_view& value);
|
void WriteHeader(const std::string_view& name, const std::string_view& value);
|
||||||
void WriteBody(const std::string_view& body);
|
void WriteBody(const std::string_view& body);
|
||||||
|
|||||||
78
server.cc
78
server.cc
@@ -1,24 +1,30 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
|
#include <iomanip>
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <netinet/tcp.h>
|
#include <netinet/tcp.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <sys/epoll.h>
|
#include <sys/epoll.h>
|
||||||
|
#include <sys/eventfd.h>
|
||||||
|
#include <sys/resource.h>
|
||||||
#include <sys/socket.h>
|
#include <sys/socket.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
|
#include "fireusage/usage.h"
|
||||||
|
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
#include "connection.h"
|
#include "connection.h"
|
||||||
|
|
||||||
namespace firecgi {
|
namespace firecgi {
|
||||||
|
|
||||||
Server::Server(int port, const std::function<void(Request*)>& callback, int threads, const std::unordered_set<std::string_view>& headers, int max_request_len)
|
Server::Server(int port, const std::function<void(Request*)>& callback, int threads, int max_request_len)
|
||||||
: port_(port),
|
: port_(port),
|
||||||
callback_(callback),
|
callback_(callback),
|
||||||
threads_(threads),
|
threads_(threads),
|
||||||
headers_(headers),
|
max_request_len_(max_request_len),
|
||||||
max_request_len_(max_request_len) {
|
close_fd_(eventfd(0, 0)) {
|
||||||
LOG(INFO) << "listening on [::1]:" << port_;
|
CHECK_GE(close_fd_, 0);
|
||||||
|
|
||||||
|
LOG(INFO) << "listening on [::1]:" << port_;
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +34,29 @@ void Server::Serve() {
|
|||||||
threads.emplace_back([this]() { ServeInt(); });
|
threads.emplace_back([this]() { ServeInt(); });
|
||||||
}
|
}
|
||||||
ServeInt();
|
ServeInt();
|
||||||
|
for (auto& thread : threads) {
|
||||||
|
thread.join();
|
||||||
|
}
|
||||||
|
LOG(INFO) << "all threads shut down";
|
||||||
|
}
|
||||||
|
|
||||||
|
void Server::Shutdown() {
|
||||||
|
uint64_t shutdown = 1;
|
||||||
|
PCHECK(write(close_fd_, &shutdown, sizeof(shutdown)) == sizeof(shutdown));
|
||||||
|
}
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
Server* shutdown_server = nullptr;
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void Server::RegisterSignalHandlers() {
|
||||||
|
shutdown_server = this;
|
||||||
|
for (auto sig : {SIGINT, SIGTERM}) {
|
||||||
|
signal(sig, [](int signum) {
|
||||||
|
LOG(INFO) << "received " << strsignal(signum);
|
||||||
|
shutdown_server->Shutdown();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::ServeInt() {
|
void Server::ServeInt() {
|
||||||
@@ -36,16 +65,33 @@ void Server::ServeInt() {
|
|||||||
|
|
||||||
auto listen_sock = NewListenSock();
|
auto listen_sock = NewListenSock();
|
||||||
|
|
||||||
|
char new_conn;
|
||||||
{
|
{
|
||||||
struct epoll_event ev{
|
struct epoll_event ev{
|
||||||
.events = EPOLLIN,
|
.events = EPOLLIN,
|
||||||
.data = {
|
.data = {
|
||||||
.ptr = nullptr,
|
.ptr = &new_conn,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_sock, &ev) == 0);
|
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, listen_sock, &ev) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char shutdown;
|
||||||
|
{
|
||||||
|
struct epoll_event ev{
|
||||||
|
.events = EPOLLIN,
|
||||||
|
.data = {
|
||||||
|
.ptr = &shutdown,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, close_fd_, &ev) == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unordered_set<Connection*> connections;
|
||||||
|
|
||||||
|
fireusage::UsageTracker usage_tracker;
|
||||||
|
usage_tracker.Start();
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
constexpr auto max_events = 256;
|
constexpr auto max_events = 256;
|
||||||
struct epoll_event events[max_events];
|
struct epoll_event events[max_events];
|
||||||
@@ -56,13 +102,25 @@ void Server::ServeInt() {
|
|||||||
PCHECK(num_fd > 0) << "epoll_wait()";
|
PCHECK(num_fd > 0) << "epoll_wait()";
|
||||||
|
|
||||||
for (auto i = 0; i < num_fd; ++i) {
|
for (auto i = 0; i < num_fd; ++i) {
|
||||||
if (events[i].data.ptr == nullptr) {
|
if (events[i].data.ptr == &new_conn) {
|
||||||
NewConn(listen_sock, epoll_fd);
|
connections.insert(CHECK_NOTNULL(NewConn(listen_sock, epoll_fd)));
|
||||||
|
} else if (events[i].data.ptr == &shutdown) {
|
||||||
|
for (auto& conn : connections) {
|
||||||
|
usage_tracker.AddEvents(conn->Requests());
|
||||||
|
delete conn;
|
||||||
|
}
|
||||||
|
usage_tracker.Stop();
|
||||||
|
PCHECK(close(listen_sock) == 0);
|
||||||
|
PCHECK(close(epoll_fd) == 0);
|
||||||
|
usage_tracker.Log();
|
||||||
|
return;
|
||||||
} else {
|
} else {
|
||||||
auto conn = static_cast<Connection*>(events[i].data.ptr);
|
auto conn = static_cast<Connection*>(events[i].data.ptr);
|
||||||
auto fd = conn->Read();
|
auto fd = conn->Read();
|
||||||
if (fd != -1) {
|
if (fd != -1) {
|
||||||
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr) == 0);
|
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_DEL, fd, nullptr) == 0);
|
||||||
|
usage_tracker.AddEvents(conn->Requests());
|
||||||
|
connections.erase(conn);
|
||||||
delete conn;
|
delete conn;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,7 +128,7 @@ void Server::ServeInt() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Server::NewConn(int listen_sock, int epoll_fd) {
|
Connection* Server::NewConn(int listen_sock, int epoll_fd) {
|
||||||
sockaddr_in6 client_addr;
|
sockaddr_in6 client_addr;
|
||||||
socklen_t client_addr_len = sizeof(client_addr);
|
socklen_t client_addr_len = sizeof(client_addr);
|
||||||
|
|
||||||
@@ -81,8 +139,8 @@ void Server::NewConn(int listen_sock, int epoll_fd) {
|
|||||||
int flags = 1;
|
int flags = 1;
|
||||||
PCHECK(setsockopt(client_sock, SOL_TCP, TCP_NODELAY, &flags, sizeof(flags)) == 0);
|
PCHECK(setsockopt(client_sock, SOL_TCP, TCP_NODELAY, &flags, sizeof(flags)) == 0);
|
||||||
|
|
||||||
|
auto *conn = new Connection(client_sock, client_addr, callback_, max_request_len_);
|
||||||
{
|
{
|
||||||
auto *conn = new Connection(client_sock, client_addr, callback_, headers_, max_request_len_);
|
|
||||||
struct epoll_event ev{
|
struct epoll_event ev{
|
||||||
.events = EPOLLIN,
|
.events = EPOLLIN,
|
||||||
.data = {
|
.data = {
|
||||||
@@ -91,6 +149,8 @@ void Server::NewConn(int listen_sock, int epoll_fd) {
|
|||||||
};
|
};
|
||||||
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_sock, &ev) == 0);
|
PCHECK(epoll_ctl(epoll_fd, EPOLL_CTL_ADD, client_sock, &ev) == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return conn;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Server::NewListenSock() {
|
int Server::NewListenSock() {
|
||||||
|
|||||||
9
server.h
9
server.h
@@ -10,19 +10,22 @@ namespace firecgi {
|
|||||||
|
|
||||||
class Server {
|
class Server {
|
||||||
public:
|
public:
|
||||||
Server(int port, const std::function<void(Request*)>& callback, int threads=1, const std::unordered_set<std::string_view>& headers={}, int max_request_len=(16*1024));
|
Server(int port, const std::function<void(Request*)>& callback, int threads=1, int max_request_len=(16*1024));
|
||||||
void Serve();
|
void Serve();
|
||||||
|
void Shutdown();
|
||||||
|
void RegisterSignalHandlers();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void NewConn(int listen_sock, int epoll_fd);
|
Connection *NewConn(int listen_sock, int epoll_fd);
|
||||||
int NewListenSock();
|
int NewListenSock();
|
||||||
void ServeInt();
|
void ServeInt();
|
||||||
|
|
||||||
const int port_;
|
const int port_;
|
||||||
const std::function<void(Request*)> callback_;
|
const std::function<void(Request*)> callback_;
|
||||||
const int threads_;
|
const int threads_;
|
||||||
const std::unordered_set<std::string_view> headers_;
|
|
||||||
const int max_request_len_;
|
const int max_request_len_;
|
||||||
|
|
||||||
|
int close_fd_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // firecgi
|
} // firecgi
|
||||||
|
|||||||
Reference in New Issue
Block a user