Skip to content

Commit

Permalink
introduce onBody callback function in CurlClient
Browse files Browse the repository at this point in the history
Summary:
introduce a onBody callback function in CurlClient so the response value can be checked.

Also re-type httpPaths and requestGaps in HQToolClientParams from std::vector<folly::StringPiece> to std::vector<std::string> because
1. there was a bug with a vector of StringPiece.
    HQToolParamsBuilderFromCmdline takes a list of gflags from the caller to override gflags. It works only in the scope of gflags::FlagSaver. https://fburl.com/code/5bg24j7v. After the constructor function is finished, gflag value will be changed back and so does requestGaps/httpPaths because StringPieces are just pointers to some other allocation not owned by themselves
 2. To create a client wrapper to send massive requests to multiple vips and check the return values,  we need to create separate and different params for each HQClient instance so we need requestGaps and httpPaths to be a vector of strings.

Reviewed By: kvtsoy

Differential Revision: D59966312

fbshipit-source-id: 00c0a50691d566d3ffa59a0a7253979e86e63cec
  • Loading branch information
Fei Chen authored and facebook-github-bot committed Jul 22, 2024
1 parent 12f541d commit cd7a030
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 2 deletions.
3 changes: 3 additions & 0 deletions proxygen/httpclient/samples/curl/CurlClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ void CurlClient::onHeadersComplete(unique_ptr<HTTPMessage> msg) noexcept {
}

void CurlClient::onBody(std::unique_ptr<folly::IOBuf> chain) noexcept {
if (onBodyFunc_ && chain) {
onBodyFunc_.value()(request_, chain.get());
}
if (!loggingEnabled_) {
return;
}
Expand Down
10 changes: 10 additions & 0 deletions proxygen/httpclient/samples/curl/CurlClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ class CurlClient
eomFunc_ = eomFunc;
}

void setOnBodyFunc(
const std::function<void(const proxygen::HTTPMessage& request,
const folly::IOBuf* chainBuf)>& onBodyFunc) {
onBodyFunc_ = onBodyFunc;
}

protected:
void sendBodyFromFile();

Expand Down Expand Up @@ -169,6 +175,10 @@ class CurlClient

folly::Optional<std::function<void()>> eomFunc_;

folly::Optional<std::function<void(const proxygen::HTTPMessage& request,
const folly::IOBuf* chainBuf)>>
onBodyFunc_;

friend class CurlPushHandler;
};

Expand Down
4 changes: 4 additions & 0 deletions proxygen/httpserver/samples/hq/HQClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,10 @@ HQClient::sendRequest(const proxygen::URL& requestUrl) {
}
}
client->sendRequest(txn);

if (onBodyFunc_) {
client->setOnBodyFunc(onBodyFunc_.value());
}
curls_.emplace_back(std::move(client));
return txn;
}
Expand Down
12 changes: 12 additions & 0 deletions proxygen/httpserver/samples/hq/HQClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

#pragma once

#include <folly/io/IOBuf.h>
#include <list>
#include <memory>
#include <proxygen/httpclient/samples/curl/CurlClient.h>
#include <proxygen/httpserver/samples/hq/H1QUpstreamSession.h>
#include <proxygen/httpserver/samples/hq/HQCommandLine.h>
#include <proxygen/lib/http/HTTPMessage.h>
#include <proxygen/lib/http/session/HQUpstreamSession.h>
#include <quic/common/events/HighResQuicTimer.h>

Expand All @@ -31,6 +33,12 @@ class HQClient : private quic::QuicSocket::ConnectionSetupCallback {

int start();

void setOnBodyFunc(
const std::function<void(const proxygen::HTTPMessage& request,
const folly::IOBuf* chainBuf)>& onBodyFunc) {
onBodyFunc_ = onBodyFunc;
}

private:
// Conn setup callback
void onConnectionSetupError(quic::QuicError code) noexcept override;
Expand Down Expand Up @@ -99,6 +107,10 @@ class HQClient : private quic::QuicSocket::ConnectionSetupCallback {

std::deque<std::chrono::milliseconds> requestGaps_;

folly::Optional<std::function<void(const proxygen::HTTPMessage& request,
const folly::IOBuf* chainBuf)>>
onBodyFunc_;

bool failed_{false};

bool replaySafe_{false};
Expand Down
1 change: 1 addition & 0 deletions proxygen/httpserver/samples/hq/HQCommandLine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ HQToolParamsBuilderFromCmdline::HQToolParamsBuilderFromCmdline(
gflags::FlagSaver saver;

for (auto& kv : initial) {
LOG(INFO) << "Overriding HQToolParams " << kv.first << " to " << kv.second;
gflags::SetCommandLineOptionWithMode(
kv.first.c_str(),
kv.second.c_str(),
Expand Down
4 changes: 2 additions & 2 deletions proxygen/httpserver/samples/hq/HQCommandLine.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ struct HQToolClientParams : public HQBaseParams {
proxygen::HTTPHeaders httpHeaders;
std::string httpBody;
proxygen::HTTPMethod httpMethod;
std::vector<folly::StringPiece> httpPaths;
std::vector<std::string> httpPaths;
bool migrateClient{false};
bool sendRequestsSequentially;
std::vector<folly::StringPiece> requestGaps;
std::vector<std::string> requestGaps;
};

struct HQToolServerParams : public HQServerParams {
Expand Down

0 comments on commit cd7a030

Please sign in to comment.