Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ We welcome contributions from the community. Here are some guidelines.
includes both const and non-const references.
* Function names using camel case starting with a lower case letter (e.g., "doFoo()").
* Struct/Class member variables have a '\_' postfix (e.g., "int foo\_;").
* 100 columns is the line limit.
* Global static non-pod variables are allowed because Envoy correctly joins all threads on exit.
However, care is needed during init and static accessor functions may be required.
* OOM events (both memory and FDs) are considered fatal crashing errors.
* There are probably a few other things missing from this list. We will add them as they
are brought to our attention.
* 100 columns is the line limit.

# Submitting a PR

Expand Down
2 changes: 2 additions & 0 deletions include/envoy/http/codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ namespace Http {
*/
enum class Code {
// clang-format off
Continue = 100,

OK = 200,
Created = 201,
Accepted = 202,
Expand Down
3 changes: 3 additions & 0 deletions source/common/http/codes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ std::string CodeUtility::groupStringForResponseCode(Code response_code) {
const char* CodeUtility::toString(Code code) {
// clang-format off
switch (code) {
// 1xx
case Code::Continue: return "Continue";

// 2xx
case Code::OK: return "OK";
case Code::Created: return "Created";
Expand Down
1 change: 1 addition & 0 deletions source/common/http/codes.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class CodeUtility {
*/
static const char* toString(Code code);

static bool is1xx(uint64_t code) { return code >= 100 && code < 200; }
static bool is2xx(uint64_t code) { return code >= 200 && code < 300; }
static bool is3xx(uint64_t code) { return code >= 300 && code < 400; }
static bool is4xx(uint64_t code) { return code >= 400 && code < 500; }
Expand Down
5 changes: 3 additions & 2 deletions source/common/http/filter/ratelimit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
namespace Http {
namespace RateLimit {

const Http::HeaderMapPtr Filter::TOO_MANY_REQUESTS_HEADER{new Http::HeaderMapImpl{
{Http::Headers::get().Status, std::to_string(enumToInt(Code::TooManyRequests))}}};
const std::unique_ptr<const Http::HeaderMap> Filter::TOO_MANY_REQUESTS_HEADER{
new Http::HeaderMapImpl{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Google C++ style guide doesn't like globals like this, since unjoined threads might observe destruction when main() is done. https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables.

Depending on whether this is something we want in the Envoy coding style (maybe we can assume we always have sane threading behavior and everything joins before the end?), this could be addressed by making this a function that returns the results of a static new* allocation of the object.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do correctly join all threads before exiting main. I'm not a huge fan of the raw pointer approach because AFAIK this will cause the heap leak detector to fail on standard settings. I don't feel super strongly about this, but we do this in other places also. Thoughts?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's also the concern about non-deterministic ordering of initialization. Not really a concern for simple local objects like this.

I think that when done sanely (e.g. don't initialize static data that depends on other static data), and given we have proper thread joining on exit, it's probably fine.

It would make sense to document this in https://github.com/lyft/envoy/blob/master/CONTRIBUTING.md though.

@mattklein123 mattklein123 Mar 14, 2017 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for init, we need to more careful, and we do handle more complex cases in init by having functions that return static variables. I will do a doc update.

{Http::Headers::get().Status, std::to_string(enumToInt(Code::TooManyRequests))}}};

void Filter::initiateCall(const HeaderMap& headers) {
Router::RoutePtr route = callbacks_->route();
Expand Down
2 changes: 1 addition & 1 deletion source/common/http/filter/ratelimit.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class Filter : public StreamDecoderFilter, public ::RateLimit::RequestCallbacks

enum class State { NotStarted, Calling, Complete, Responded };

static const Http::HeaderMapPtr TOO_MANY_REQUESTS_HEADER;
static const std::unique_ptr<const Http::HeaderMap> TOO_MANY_REQUESTS_HEADER;

FilterConfigPtr config_;
::RateLimit::ClientPtr client_;
Expand Down
5 changes: 2 additions & 3 deletions source/common/http/http1/codec_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,8 @@ int ServerConnectionImpl::onHeadersComplete(HeaderMapImplPtr&& headers) {
const char* method_string = http_method_str(static_cast<http_method>(parser_.method));
headers->insertMethod().value(method_string, strlen(method_string));

// Deal with expect: 100-continue here since a) only HTTP/1.1 has this, b) higher layers are
// never going to do anything other than say to continue since we can response before request
// complete if necessary.
// Deal with expect: 100-continue here since higher layers are never going to do anything other
// than say to continue so that we can respond before request complete if necessary.
if (headers->Expect() &&
0 == StringUtil::caseInsensitiveCompare(headers->Expect()->value().c_str(),
Headers::get().ExpectValues._100Continue.c_str())) {
Expand Down
70 changes: 62 additions & 8 deletions source/common/http/http2/codec_impl.cc
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#include "codec_impl.h"

#include "envoy/event/dispatcher.h"
#include "envoy/http/codes.h"
#include "envoy/http/header_map.h"
#include "envoy/network/connection.h"

#include "common/common/assert.h"
#include "common/common/enum_to_int.h"
#include "common/common/utility.h"
#include "common/http/codes.h"
#include "common/http/exception.h"
#include "common/http/headers.h"
#include "common/http/utility.h"

namespace Http {
namespace Http2 {
Expand All @@ -28,6 +32,9 @@ bool Utility::reconstituteCrumbledCookies(const HeaderString& key, const HeaderS

ConnectionImpl::Http2Callbacks ConnectionImpl::http2_callbacks_;
ConnectionImpl::Http2Options ConnectionImpl::http2_options_;
const std::unique_ptr<const Http::HeaderMap> ConnectionImpl::CONTINUE_HEADER{

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above (regarding static non-POD).

new Http::HeaderMapImpl{
{Http::Headers::get().Status, std::to_string(enumToInt(Code::Continue))}}};

/**
* Helper to remove const during a cast. nghttp2 takes non-const pointers for headers even though
Expand All @@ -39,7 +46,8 @@ template <typename T> static T* remove_const(const void* object) {

ConnectionImpl::StreamImpl::StreamImpl(ConnectionImpl& parent)
: parent_(parent), headers_(new HeaderMapImpl()), local_end_stream_(false),
local_end_stream_sent_(false), remote_end_stream_(false), data_deferred_(false) {}
local_end_stream_sent_(false), remote_end_stream_(false), data_deferred_(false),
waiting_for_non_informational_headers_(false) {}

ConnectionImpl::StreamImpl::~StreamImpl() {}

Expand Down Expand Up @@ -283,17 +291,63 @@ int ConnectionImpl::onFrameReceived(const nghttp2_frame* frame) {
stream->headers_->addViaMove(std::move(key), std::move(stream->cookies_));
}

if (frame->headers.cat == NGHTTP2_HCAT_REQUEST || frame->headers.cat == NGHTTP2_HCAT_RESPONSE) {
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST && stream->headers_->Expect() &&
0 == StringUtil::caseInsensitiveCompare(stream->headers_->Expect()->value().c_str(),
Headers::get().ExpectValues._100Continue.c_str())) {
// Deal with expect: 100-continue here since higher layers are never going to do anything
// other than say to continue so that we can respond before request complete if necessary.
std::vector<nghttp2_nv> final_headers;
StreamImpl::buildHeaders(final_headers, *CONTINUE_HEADER);
int rc = nghttp2_submit_headers(session_, 0, stream->stream_id_, nullptr, &final_headers[0],
final_headers.size(), nullptr);
ASSERT(rc == 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this definitely a failure that can be ignored if not-debug?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on logic flow, this function should never fail, so I made it a debug only assert. We do this in a few other places in this file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about NGHTTP2_ERR_NOMEM?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of the Envoy code is safe for memory allocation failure, so we don't handle similar errors anywhere and assume we will shortly crash if OOM. (This is a design philosophy, we should discuss this outside of the context of this change if there are concerns).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, we can discuss this elsewhere (I do worry about libraries using ENOMEM in non-OOM scenarios, as opposed to tcmalloc). In any case, could you also include this design philosophy in https://github.com/lyft/envoy/blob/master/CONTRIBUTING.md when updating? Thanks.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is a valid concern and might necessitate some paranoia checking in certain cases which would crash us if we receive ENOMEM, etc. I will open a GH issue to track as well as updating contributing guide.

UNREFERENCED_PARAMETER(rc);

stream->headers_->removeExpect();
}

switch (frame->headers.cat) {
case NGHTTP2_HCAT_RESPONSE: {
if (CodeUtility::is1xx(Http::Utility::getResponseStatus(*stream->headers_))) {
stream->waiting_for_non_informational_headers_ = true;
}

// Fall through.
}

case NGHTTP2_HCAT_REQUEST: {
stream->decoder_->decodeHeaders(std::move(stream->headers_), stream->remote_end_stream_);
} else {
ASSERT(frame->headers.cat == NGHTTP2_HCAT_HEADERS);
ASSERT(stream->remote_end_stream_);
break;
}

// It's possible that we are waiting to send a deferred reset, so only raise trailers if local
// is not complete.
case NGHTTP2_HCAT_HEADERS: {
// It's possible that we are waiting to send a deferred reset, so only raise headers/trailers
// if local is not complete.
if (!stream->deferred_reset_.valid()) {
stream->decoder_->decodeTrailers(std::move(stream->headers_));
if (!stream->waiting_for_non_informational_headers_) {
ASSERT(stream->remote_end_stream_);
stream->decoder_->decodeTrailers(std::move(stream->headers_));
} else {
ASSERT(!nghttp2_session_check_server_session(session_));
stream->waiting_for_non_informational_headers_ = false;

// This can only happen in the client case in a response, when we received a 1xx to
// start out with. In this case, raise as headers. nghttp2 message checking guarantees
// proper flow here.
// TODO(mattklein123): Higher layers don't currently deal with a double decodeHeaders()
// call and will probably crash. We do this in the client path for testing when the server
// responds with 1xx. In the future, if needed, we can properly handle 1xx in higher layer
// code, or just eat it.
stream->decoder_->decodeHeaders(std::move(stream->headers_), stream->remote_end_stream_);
}
}

break;
}

default:
// We do not currently support push.
NOT_IMPLEMENTED;
}

stream->headers_.reset();
Expand Down
5 changes: 4 additions & 1 deletion source/common/http/http2/codec_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ class ConnectionImpl : public virtual Connection, Logger::Loggable<Logger::Id::h
ssize_t onDataSourceRead(uint64_t length, uint32_t* data_flags);
int onDataSourceSend(const uint8_t* framehd, size_t length);
void resetStreamWorker(StreamResetReason reason);
void buildHeaders(std::vector<nghttp2_nv>& final_headers, const HeaderMap& headers);
static void buildHeaders(std::vector<nghttp2_nv>& final_headers, const HeaderMap& headers);
void saveHeader(HeaderString&& name, HeaderString&& value);
virtual void submitHeaders(const std::vector<nghttp2_nv>& final_headers,
nghttp2_data_provider* provider) PURE;
Expand Down Expand Up @@ -157,6 +157,7 @@ class ConnectionImpl : public virtual Connection, Logger::Loggable<Logger::Id::h
bool local_end_stream_sent_ : 1;
bool remote_end_stream_ : 1;
bool data_deferred_ : 1;
bool waiting_for_non_informational_headers_ : 1;
};

typedef std::unique_ptr<StreamImpl> StreamImplPtr;
Expand Down Expand Up @@ -211,6 +212,8 @@ class ConnectionImpl : public virtual Connection, Logger::Loggable<Logger::Id::h
// needed.
static const uint64_t DEFAULT_WINDOW_SIZE = 256 * 1024 * 1024;

static const std::unique_ptr<const Http::HeaderMap> CONTINUE_HEADER;

Network::Connection& connection_;
bool dispatching_ : 1;
bool raised_goaway_ : 1;
Expand Down
2 changes: 1 addition & 1 deletion source/exe/hot_restart.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ SharedMemory& SharedMemory::initialize(Options& options) {
}

// Here we catch the case where a new Envoy starts up when the current Envoy has not yet fully
// initialized. The startup logic is quite compicated, and it's not worth trying to handle this
// initialized. The startup logic is quite complicated, and it's not worth trying to handle this
// in a finer way. This will cause the startup to fail with an error code early, without
// affecting any currently running processes. The process runner should try again later with some
// back off and with the same hot restart epoch number.
Expand Down
7 changes: 4 additions & 3 deletions test/common/http/codes_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ TEST_F(CodeUtilityTest, Canary) {

TEST_F(CodeUtilityTest, All) {
std::vector<std::pair<Code, std::string>> testSet = {
std::make_pair(Code::OK, "OK"), std::make_pair(Code::Created, "Created"),
std::make_pair(Code::Accepted, "Accepted"),
std::make_pair(Code::Continue, "Continue"), std::make_pair(Code::OK, "OK"),
std::make_pair(Code::Created, "Created"), std::make_pair(Code::Accepted, "Accepted"),
std::make_pair(Code::NonAuthoritativeInformation, "Non-Authoritative Information"),
std::make_pair(Code::NoContent, "No Content"),
std::make_pair(Code::ResetContent, "Reset Content"),
Expand Down Expand Up @@ -133,7 +133,8 @@ TEST_F(CodeUtilityTest, All) {
std::make_pair(Code::InsufficientStorage, "Insufficient Storage"),
std::make_pair(Code::LoopDetected, "Loop Detected"),
std::make_pair(Code::NotExtended, "Not Extended"),
std::make_pair(Code::NetworkAuthenticationRequired, "Network Authentication Required")};
std::make_pair(Code::NetworkAuthenticationRequired, "Network Authentication Required"),
std::make_pair(static_cast<Code>(600), "Unknown")};

for (const auto& testCase : testSet) {
EXPECT_EQ(testCase.second, CodeUtility::toString(testCase.first));
Expand Down
Loading