2021-11-16 16:39:38 +08:00
|
|
|
// Copyright 2021, Beeri 15. All rights reserved.
|
|
|
|
// Author: Roman Gershman (romange@gmail.com)
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "server/dragonfly_connection.h"
|
|
|
|
|
|
|
|
#include <boost/fiber/operations.hpp>
|
|
|
|
|
|
|
|
#include "base/io_buf.h"
|
|
|
|
#include "base/logging.h"
|
|
|
|
#include "server/main_service.h"
|
2021-11-16 21:04:32 +08:00
|
|
|
#include "server/redis_parser.h"
|
2021-11-17 22:38:32 +08:00
|
|
|
#include "server/conn_context.h"
|
2021-11-16 16:39:38 +08:00
|
|
|
#include "util/fiber_sched_algo.h"
|
|
|
|
|
|
|
|
using namespace util;
|
|
|
|
using namespace std;
|
|
|
|
namespace this_fiber = boost::this_fiber;
|
|
|
|
namespace fibers = boost::fibers;
|
|
|
|
|
|
|
|
namespace dfly {
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constexpr size_t kMinReadSize = 256;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
struct Connection::Shutdown {
|
|
|
|
absl::flat_hash_map<ShutdownHandle, ShutdownCb> map;
|
|
|
|
ShutdownHandle next_handle = 1;
|
|
|
|
|
|
|
|
ShutdownHandle Add(ShutdownCb cb) {
|
|
|
|
map[next_handle] = move(cb);
|
|
|
|
return next_handle++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Remove(ShutdownHandle sh) {
|
|
|
|
map.erase(sh);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Connection::Connection(Service* service)
|
|
|
|
: service_(service) {
|
2021-11-16 21:04:32 +08:00
|
|
|
redis_parser_.reset(new RedisParser);
|
2021-11-16 16:39:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Connection::~Connection() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::OnShutdown() {
|
|
|
|
VLOG(1) << "Connection::OnShutdown";
|
|
|
|
if (shutdown_) {
|
|
|
|
for (const auto& k_v : shutdown_->map) {
|
|
|
|
k_v.second();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto Connection::RegisterShutdownHook(ShutdownCb cb) -> ShutdownHandle {
|
|
|
|
if (!shutdown_) {
|
|
|
|
shutdown_ = make_unique<Shutdown>();
|
|
|
|
}
|
|
|
|
return shutdown_->Add(std::move(cb));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::UnregisterShutdownHook(ShutdownHandle id) {
|
|
|
|
if (shutdown_) {
|
|
|
|
shutdown_->Remove(id);
|
|
|
|
if (shutdown_->map.empty())
|
|
|
|
shutdown_.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::HandleRequests() {
|
|
|
|
this_fiber::properties<FiberProps>().set_name("DflyConnection");
|
|
|
|
|
|
|
|
int val = 1;
|
|
|
|
CHECK_EQ(0, setsockopt(socket_->native_handle(), SOL_TCP, TCP_NODELAY, &val, sizeof(val)));
|
|
|
|
|
|
|
|
FiberSocketBase* peer = socket_.get();
|
2021-11-17 22:38:32 +08:00
|
|
|
cc_.reset(new ConnectionContext(peer, this));
|
|
|
|
cc_->shard_set = &service_->shard_set();
|
|
|
|
|
2021-11-16 16:39:38 +08:00
|
|
|
InputLoop(peer);
|
|
|
|
|
|
|
|
VLOG(1) << "Closed connection for peer " << socket_->RemoteEndpoint();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Connection::InputLoop(FiberSocketBase* peer) {
|
|
|
|
base::IoBuf io_buf{kMinReadSize};
|
2021-11-16 21:04:32 +08:00
|
|
|
ParserStatus status = OK;
|
2021-11-16 16:39:38 +08:00
|
|
|
std::error_code ec;
|
|
|
|
|
|
|
|
do {
|
|
|
|
auto buf = io_buf.AppendBuffer();
|
|
|
|
::io::Result<size_t> recv_sz = peer->Recv(buf);
|
|
|
|
|
|
|
|
if (!recv_sz) {
|
|
|
|
ec = recv_sz.error();
|
2021-11-16 21:04:32 +08:00
|
|
|
status = OK;
|
2021-11-16 16:39:38 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
io_buf.CommitWrite(*recv_sz);
|
2021-11-17 22:38:32 +08:00
|
|
|
status = ParseRedis(&io_buf);
|
2021-11-16 21:04:32 +08:00
|
|
|
if (status == NEED_MORE) {
|
|
|
|
status = OK;
|
|
|
|
} else if (status != OK) {
|
2021-11-16 16:39:38 +08:00
|
|
|
break;
|
2021-11-16 21:04:32 +08:00
|
|
|
}
|
2021-11-16 16:39:38 +08:00
|
|
|
} while (peer->IsOpen());
|
|
|
|
|
|
|
|
if (ec && !FiberSocketBase::IsConnClosed(ec)) {
|
|
|
|
LOG(WARNING) << "Socket error " << ec;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 22:38:32 +08:00
|
|
|
auto Connection::ParseRedis(base::IoBuf* io_buf) -> ParserStatus {
|
2021-11-16 21:04:32 +08:00
|
|
|
RespVec args;
|
|
|
|
uint32_t consumed = 0;
|
|
|
|
|
|
|
|
RedisParser::Result result = RedisParser::OK;
|
2021-11-17 22:38:32 +08:00
|
|
|
|
2021-11-16 21:04:32 +08:00
|
|
|
do {
|
|
|
|
result = redis_parser_->Parse(io_buf->InputBuffer(), &consumed, &args);
|
|
|
|
|
|
|
|
if (result == RedisParser::OK && !args.empty()) {
|
|
|
|
RespExpr& first = args.front();
|
|
|
|
if (first.type == RespExpr::STRING) {
|
|
|
|
DVLOG(2) << "Got Args with first token " << ToSV(first.GetBuf());
|
|
|
|
}
|
|
|
|
|
|
|
|
CHECK_EQ(RespExpr::STRING, first.type); // TODO
|
|
|
|
string_view sv = ToSV(first.GetBuf());
|
|
|
|
if (sv == "PING") {
|
2021-11-17 22:38:32 +08:00
|
|
|
cc_->SendSimpleString("PONG");
|
2021-11-17 18:51:47 +08:00
|
|
|
} else if (sv == "SET") {
|
|
|
|
CHECK_EQ(3u, args.size());
|
|
|
|
service_->Set(ToSV(args[1].GetBuf()), ToSV(args[2].GetBuf()));
|
2021-11-17 22:38:32 +08:00
|
|
|
cc_->SendOk();
|
2021-11-16 21:04:32 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
io_buf->ConsumeInput(consumed);
|
2021-11-17 22:38:32 +08:00
|
|
|
} while (RedisParser::OK == result && !cc_->ec());
|
2021-11-16 21:04:32 +08:00
|
|
|
|
|
|
|
parser_error_ = result;
|
|
|
|
if (result == RedisParser::OK)
|
|
|
|
return OK;
|
|
|
|
|
|
|
|
if (result == RedisParser::INPUT_PENDING)
|
|
|
|
return NEED_MORE;
|
|
|
|
|
|
|
|
return ERROR;
|
|
|
|
}
|
2021-11-16 16:39:38 +08:00
|
|
|
} // namespace dfly
|