oschaaf commented on a change in pull request #1969: Integration of envoy fetcher in pagespeed URL: https://github.com/apache/incubator-pagespeed-mod/pull/1969#discussion_r360335447
########## File path: pagespeed/envoy/envoy_fetch.cc ########## @@ -0,0 +1,151 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "envoy_fetch.h" + +#include <algorithm> +#include <string> +#include <vector> +#include <list> +#include <map> +#include <set> + +#include "net/instaweb/http/public/async_fetch.h" +#include "net/instaweb/http/public/inflating_fetch.h" +#include "pagespeed/kernel/base/basictypes.h" +#include "pagespeed/kernel/base/condvar.h" +#include "pagespeed/kernel/base/message_handler.h" +#include "pagespeed/kernel/base/pool.h" +#include "pagespeed/kernel/base/pool_element.h" +#include "pagespeed/kernel/base/statistics.h" +#include "pagespeed/kernel/base/string_util.h" +#include "pagespeed/kernel/base/thread_system.h" +#include "pagespeed/kernel/base/timer.h" +#include "pagespeed/kernel/base/writer.h" +#include "pagespeed/kernel/http/request_headers.h" +#include "pagespeed/kernel/http/response_headers.h" +#include "pagespeed/kernel/http/response_headers_parser.h" +#include "pagespeed_remote_data_fetcher.h" +#include "pagespeed/envoy/header_utils.h" + +namespace net_instaweb { + +// Default keepalive 60s. +const int64 keepalive_timeout_ms = 60000; + +PagespeedDataFetcherCallback::PagespeedDataFetcherCallback(EnvoyFetch* fetch) { + fetch_ = fetch; +} + +void PagespeedDataFetcherCallback::onSuccess(Envoy::Http::MessagePtr& response) { + fetch_->setResponse(response->headers(), response->body()); +} + +void PagespeedDataFetcherCallback::onFailure(FailureReason reason) { + std::cout << "PagespeedDataFetcherCallback::onFailure\n"; + std::cout.flush(); +} + +EnvoyFetch::EnvoyFetch(const GoogleString& url, + AsyncFetch* async_fetch, + MessageHandler* message_handler, + EnvoyClusterManager& cluster_manager) + : str_url_(url), + fetcher_(NULL), + async_fetch_(async_fetch), + message_handler_(message_handler), + cluster_manager_(cluster_manager), + done_(false), + content_length_(-1), + content_length_known_(false) { +} + +EnvoyFetch::~EnvoyFetch() { +} + +void EnvoyFetch::FetchWithEnvoy() { + envoy::api::v2::core::HttpUri http_uri; + http_uri.set_uri("http://localhost:80"); + http_uri.set_cluster("cluster1"); + std::string uriHash("123456789"); + + cb_ptr_ = std::make_unique<PagespeedDataFetcherCallback>(this); + std::unique_ptr<PagespeedRemoteDataFetcher> PagespeedRemoteDataFetcherPtr = std::make_unique<PagespeedRemoteDataFetcher>( + cluster_manager_.getClusterManager(), http_uri, uriHash, *cb_ptr_); + + PagespeedRemoteDataFetcherPtr->fetch(); + cluster_manager_.getDispatcher()->run(Envoy::Event::Dispatcher::RunType::Block); Review comment: So we probably need start to run the dispatcher earlier, after starting up, and then pass in the argument that indicates we want it to remain running until `dispatcher->exit()` is called. Then we can remove this line. Exit should be called right before we shut down the thread. Please avoid using the destructor for that, a separate `shutdown()` call is warranted here. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services