Remove the nested read loops in FastCGIConn, hide complexity inside StreamBuffer

This commit is contained in:
Ian Gulliver
2019-04-28 17:50:44 +00:00
parent 323cfc15a5
commit 45e5570959
10 changed files with 73 additions and 51 deletions

1
.gitignore vendored
View File

@@ -1,2 +1,3 @@
mirall mirall
example_simple
*.o *.o

View File

@@ -1,5 +1,5 @@
example_simple: example_simple.o fastcgi.o fastcgi_conn.o fastcgi_request.o buffer.o example_simple: example_simple.o fastcgi.o fastcgi_conn.o fastcgi_request.o stream_buffer.o buffer.o
clang++ -std=gnu++2a -o example_simple example_simple.o fastcgi.o fastcgi_conn.o fastcgi_request.o buffer.o -lgflags -lglog -lpthread clang++ -std=gnu++2a -o example_simple example_simple.o fastcgi.o fastcgi_conn.o fastcgi_request.o stream_buffer.o buffer.o -lgflags -lglog -lpthread
clean: clean:
rm --force exmaple_simple *.o rm --force exmaple_simple *.o

View File

@@ -23,14 +23,6 @@ bool ConstBuffer::Discard(size_t len) {
return true; return true;
} }
void ConstBuffer::ResetRead() {
start_ = commit_;
}
void ConstBuffer::Commit() {
commit_ = start_;
}
Buffer::Buffer(char *buf, size_t size, size_t len) Buffer::Buffer(char *buf, size_t size, size_t len)
: ConstBuffer(buf, size), : ConstBuffer(buf, size),
@@ -57,12 +49,11 @@ void Buffer::Wrote(size_t len) {
len_ += len; len_ += len;
} }
void Buffer::Consume() { void Buffer::Commit() {
if (commit_ == 0) { if (start_ == 0) {
return; return;
} }
memmove(buf_, &buf_[commit_], len_ - commit_); memmove(buf_, &buf_[start_], len_ - start_);
len_ -= commit_; len_ -= start_;
start_ -= commit_; start_ = 0;
commit_ = 0;
} }

View File

@@ -8,23 +8,19 @@ class ConstBuffer {
ConstBuffer(const char *buf, size_t len); ConstBuffer(const char *buf, size_t len);
[[nodiscard]] size_t ReadMaxLen() const; [[nodiscard]] size_t ReadMaxLen() const;
[[nodiscard]] const char *Read(size_t len); [[nodiscard]] virtual const char *Read(size_t len);
template<class T> [[nodiscard]] const T *ReadObj(); template<class T> [[nodiscard]] const T *ReadObj();
bool Discard(size_t len); // like Read() but don't use the result bool Discard(size_t len); // like Read() but don't use the result
void ResetRead(); // next read from last commit
void Commit(); // commit read position
protected: protected:
const char *const_buf_; const char *const_buf_;
size_t len_; size_t len_;
size_t start_ = 0; size_t start_ = 0;
size_t commit_ = 0;
}; };
class Buffer : public ConstBuffer { class Buffer : public ConstBuffer {
public: public:
Buffer(const char *buf, size_t len) = delete;
Buffer(char *buf, size_t size, size_t len); Buffer(char *buf, size_t size, size_t len);
Buffer(size_t size); Buffer(size_t size);
@@ -32,9 +28,9 @@ class Buffer : public ConstBuffer {
[[nodiscard]] size_t WriteMaxLen() const; [[nodiscard]] size_t WriteMaxLen() const;
void Wrote(size_t len); void Wrote(size_t len);
void Consume(); // discard up to last commit void Commit(); // commit read position
private: protected:
std::unique_ptr<char> own_buf_; std::unique_ptr<char> own_buf_;
char *buf_; char *buf_;
const size_t size_; const size_t size_;

Binary file not shown.

View File

@@ -10,7 +10,6 @@ int main(int argc, char *argv[]) {
gflags::ParseCommandLineFlags(&argc, &argv, true); gflags::ParseCommandLineFlags(&argc, &argv, true);
FastCGIServer server(FLAGS_port, [](std::unique_ptr<FastCGIRequest> request) { FastCGIServer server(FLAGS_port, [](std::unique_ptr<FastCGIRequest> request) {
LOG(INFO) << "request from " << request->GetParam("REMOTE_ADDR");
request->Write({{"Content-Type", "text/plain"}}, {"Hello world"}); request->Write({{"Content-Type", "text/plain"}}, {"Hello world"});
request->WriteEnd(); request->WriteEnd();
}); });

View File

@@ -55,7 +55,7 @@ constexpr auto fcgi_max_record_len = sizeof(fcgi_header) + fcgi_max_content_len
FastCGIConn::FastCGIConn(int sock, const sockaddr_in6& client_addr, const std::function<void(std::unique_ptr<FastCGIRequest>)>& callback) FastCGIConn::FastCGIConn(int sock, const sockaddr_in6& client_addr, const std::function<void(std::unique_ptr<FastCGIRequest>)>& callback)
: sock_(sock), : sock_(sock),
callback_(callback), callback_(callback),
buf_(fcgi_max_record_len) { buf_(sock, fcgi_max_record_len) {
char client_addr_str[INET6_ADDRSTRLEN]; char client_addr_str[INET6_ADDRSTRLEN];
PCHECK(inet_ntop(AF_INET6, &client_addr.sin6_addr, client_addr_str, sizeof(client_addr_str))); PCHECK(inet_ntop(AF_INET6, &client_addr.sin6_addr, client_addr_str, sizeof(client_addr_str)));
@@ -67,22 +67,6 @@ FastCGIConn::~FastCGIConn() {
LOG(INFO) << "connection closed"; LOG(INFO) << "connection closed";
} }
void FastCGIConn::Serve() {
while (true) {
auto read_len = read(sock_, buf_.WritePtr(), buf_.WriteMaxLen());
PCHECK(read_len >= 0);
if (read_len == 0) {
LOG(INFO) << "peer closed connection";
delete this;
return;
}
buf_.Wrote(read_len);
ParseBuf();
buf_.Consume(); // free buffer tail space for next read
}
}
void FastCGIConn::WriteBlock(uint8_t type, uint16_t request_id, const std::vector<iovec>& vecs) { void FastCGIConn::WriteBlock(uint8_t type, uint16_t request_id, const std::vector<iovec>& vecs) {
std::vector<iovec> out_vecs; std::vector<iovec> out_vecs;
out_vecs.reserve(vecs.size() + 1); out_vecs.reserve(vecs.size() + 1);
@@ -123,17 +107,16 @@ void FastCGIConn::WriteEnd(uint16_t request_id) {
WriteBlock(3, request_id, vecs); WriteBlock(3, request_id, vecs);
} }
void FastCGIConn::ParseBuf() { void FastCGIConn::Serve() {
buf_.ResetRead();
while (true) { while (true) {
const auto *header = buf_.ReadObj<fcgi_header>(); const auto *header = buf_.ReadObj<fcgi_header>();
if (!header) { if (!header) {
return; LOG(INFO) << "readobj failed";
break;
} }
CHECK_EQ(header->version, 1); CHECK_EQ(header->version, 1);
if (buf_.ReadMaxLen() < header->ContentLength() + header->padding_length) { if (!buf_.BufferAtLeast(header->ContentLength())) {
return; return;
} }
@@ -170,10 +153,19 @@ void FastCGIConn::ParseBuf() {
request_->AddIn(in); request_->AddIn(in);
} }
} }
break;
default:
CHECK(false) << "unknown record type: " << header->type;
break;
} }
CHECK(buf_.Discard(header->padding_length)); if (!buf_.Discard(header->padding_length)) {
break;
}
buf_.Commit(); // we've acted on the bytes read so far buf_.Commit(); // we've acted on the bytes read so far
} }
delete this;
} }

View File

@@ -3,7 +3,7 @@
#include <functional> #include <functional>
#include <unordered_map> #include <unordered_map>
#include "buffer.h" #include "stream_buffer.h"
struct sockaddr_in6; struct sockaddr_in6;
class FastCGIRequest; class FastCGIRequest;
@@ -20,12 +20,10 @@ class FastCGIConn {
void WriteEnd(uint16_t request_id); void WriteEnd(uint16_t request_id);
private: private:
void ParseBuf();
const int sock_; const int sock_;
std::function<void(std::unique_ptr<FastCGIRequest>)> callback_; std::function<void(std::unique_ptr<FastCGIRequest>)> callback_;
Buffer buf_; StreamBuffer buf_;
std::unique_ptr<FastCGIRequest> request_; std::unique_ptr<FastCGIRequest> request_;
}; };

26
stream_buffer.cpp Normal file
View File

@@ -0,0 +1,26 @@
#include "stream_buffer.h"
StreamBuffer::StreamBuffer(int sock, size_t size)
: Buffer(size),
sock_(sock) {}
bool StreamBuffer::BufferAtLeast(size_t len) {
CHECK_LE(start_ + len, size_);
while (ReadMaxLen() < len) {
auto read_len = read(sock_, WritePtr(), WriteMaxLen());
PCHECK(read_len >= 0);
if (read_len == 0) {
return false;
}
Wrote(read_len);
}
return true;
}
const char *StreamBuffer::Read(size_t len) {
if (!BufferAtLeast(len)) {
return nullptr;
}
return Buffer::Read(len);
}

19
stream_buffer.h Normal file
View File

@@ -0,0 +1,19 @@
#pragma once
#include "buffer.h"
class StreamBuffer : public Buffer {
public:
StreamBuffer(int sock, size_t size);
[[nodiscard]] bool BufferAtLeast(size_t len);
[[nodiscard]] const char *Read(size_t len) override;
template<class T> [[nodiscard]] const T *ReadObj();
private:
int sock_;
};
template<class T> const T *StreamBuffer::ReadObj() {
return reinterpret_cast<const T*>(Read(sizeof(T)));
}