Partially remove dependency on folly async

Summary:
This change isolates the usage of folly async from Flipper. Is now self-contained in Flipper Folly schedulers.

Users of Flipper can decide not to use the types defined in that header and implement their own.

NOTE: changes are minimal, we are just replacing direct calls to folly event base with a scheduler which simply relays this on to folly.

Reviewed By: fabiomassimo

Differential Revision: D36052198

fbshipit-source-id: 170d64a324a1f1f100224e2622a59cbac3c8b642
This commit is contained in:
Lorenzo Blasa
2022-05-12 17:56:17 -07:00
committed by Facebook GitHub Bot
parent 216c926ca5
commit ade685c621
18 changed files with 132 additions and 116 deletions

View File

@@ -8,6 +8,7 @@
#include "FlipperClient.h"
#include <fstream>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <vector>
#include "ConnectionContextStore.h"

View File

@@ -8,6 +8,7 @@
#pragma once
#include <map>
#include <memory>
#include <mutex>
#include <vector>
#include "FlipperCertificateProvider.h"

View File

@@ -95,8 +95,8 @@ FlipperConnectionManagerImpl::FlipperConnectionManagerImpl(
securePort(config.securePort),
altInsecurePort(config.altInsecurePort),
altSecurePort(config.altSecurePort),
flipperEventBase_(config.callbackWorker),
connectionEventBase_(config.connectionWorker),
flipperScheduler_(config.callbackWorker),
connectionScheduler_(config.connectionWorker),
contextStore_(contextStore),
implWrapper_(std::make_shared<FlipperConnectionManagerWrapper>(this)) {
CHECK_THROW(config.callbackWorker, std::invalid_argument);
@@ -131,13 +131,10 @@ void FlipperConnectionManagerImpl::start() {
auto step = flipperState_->start("Start connection thread");
folly::makeFuture()
.via(flipperEventBase_->getEventBase())
.delayed(std::chrono::milliseconds(0))
.thenValue([this, step](auto&&) {
step->complete();
startSync();
});
flipperScheduler_->schedule([this, step]() {
step->complete();
startSync();
});
}
void FlipperConnectionManagerImpl::startSync() {
@@ -210,7 +207,7 @@ bool FlipperConnectionManagerImpl::connectAndExchangeCertificate() {
payload->medium = medium;
auto newClient = FlipperSocketProvider::socketCreate(
endpoint, std::move(payload), connectionEventBase_);
endpoint, std::move(payload), flipperScheduler_);
newClient->setEventHandler(ConnectionEvents(implWrapper_));
auto connectingInsecurely = flipperState_->start("Connect insecurely");
@@ -259,7 +256,7 @@ bool FlipperConnectionManagerImpl::connectSecurely() {
payload->csr_path = contextStore_->getCertificateDirectoryPath().c_str();
auto newClient = FlipperSocketProvider::socketCreate(
endpoint, std::move(payload), connectionEventBase_, contextStore_.get());
endpoint, std::move(payload), connectionScheduler_, contextStore_.get());
newClient->setEventHandler(ConnectionEvents(implWrapper_));
newClient->setMessageHandler([this](const std::string& msg) {
std::unique_ptr<FireAndForgetBasedFlipperResponder> responder;
@@ -294,10 +291,8 @@ void FlipperConnectionManagerImpl::reconnect() {
log("Not started");
return;
}
folly::makeFuture()
.via(flipperEventBase_->getEventBase())
.delayed(std::chrono::seconds(reconnectIntervalSeconds))
.thenValue([this](auto&&) { startSync(); });
flipperScheduler_->scheduleAfter(
[this]() { startSync(); }, reconnectIntervalSeconds * 1000.0f);
}
void FlipperConnectionManagerImpl::stop() {
@@ -325,7 +320,7 @@ void FlipperConnectionManagerImpl::setCallbacks(Callbacks* callbacks) {
}
void FlipperConnectionManagerImpl::sendMessage(const folly::dynamic& message) {
flipperEventBase_->add([this, message]() {
flipperScheduler_->schedule([this, message]() {
try {
if (client_) {
client_->send(message, []() {});
@@ -457,11 +452,11 @@ void FlipperConnectionManagerImpl::requestSignedCertificate() {
auto gettingCert = flipperState_->start("Getting cert from desktop");
certificateExchangeCompleted_ = false;
flipperEventBase_->add([this, message, gettingCert]() {
flipperScheduler_->schedule([this, message, gettingCert]() {
client_->sendExpectResponse(
folly::toJson(message),
[this, gettingCert](const std::string& response, bool isError) {
flipperEventBase_->add([this, gettingCert, response, isError]() {
flipperScheduler_->schedule([this, gettingCert, response, isError]() {
this->processSignedCertificateResponse(
gettingCert, response, isError);
});
@@ -471,7 +466,7 @@ void FlipperConnectionManagerImpl::requestSignedCertificate() {
}
bool FlipperConnectionManagerImpl::isRunningInOwnThread() {
return flipperEventBase_->isInEventBaseThread();
return flipperScheduler_->isRunningInOwnThread();
}
} // namespace flipper

View File

@@ -7,11 +7,10 @@
#pragma once
#include <folly/Executor.h>
#include <folly/io/async/EventBase.h>
#include <mutex>
#include "FlipperConnectionManager.h"
#include "FlipperInitConfig.h"
#include "FlipperScheduler.h"
#include "FlipperSocket.h"
#include "FlipperState.h"
@@ -64,8 +63,8 @@ class FlipperConnectionManagerImpl : public FlipperConnectionManager {
int altInsecurePort;
int altSecurePort;
folly::EventBase* flipperEventBase_;
folly::EventBase* connectionEventBase_;
Scheduler* flipperScheduler_;
Scheduler* connectionScheduler_;
std::unique_ptr<FlipperSocket> client_;

View File

@@ -7,8 +7,8 @@
#pragma once
#include <folly/io/async/EventBase.h>
#include <map>
#include "FlipperScheduler.h"
namespace facebook {
namespace flipper {
@@ -30,14 +30,14 @@ struct FlipperInitConfig {
DeviceData deviceData;
/**
EventBase on which client callbacks should be called.
Scheduler on which client callbacks should be called.
*/
folly::EventBase* callbackWorker;
Scheduler* callbackWorker;
/**
EventBase to be used to maintain the network connection.
Scheduler to be used to maintain the network connection.
*/
folly::EventBase* connectionWorker;
Scheduler* connectionWorker;
int insecurePort = 9089;
int securePort = 9088;

View File

@@ -21,19 +21,19 @@ std::unique_ptr<FlipperSocketProvider> FlipperSocketProvider::shelvedProvider_ =
std::unique_ptr<FlipperSocket> FlipperSocketProvider::socketCreate(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
folly::EventBase* eventBase) {
return provider_->create(std::move(endpoint), std::move(payload), eventBase);
Scheduler* scheduler) {
return provider_->create(std::move(endpoint), std::move(payload), scheduler);
}
std::unique_ptr<FlipperSocket> FlipperSocketProvider::socketCreate(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
folly::EventBase* eventBase,
Scheduler* scheduler,
ConnectionContextStore* connectionContextStore) {
return provider_->create(
std::move(endpoint),
std::move(payload),
eventBase,
scheduler,
connectionContextStore);
}

View File

@@ -7,8 +7,8 @@
#pragma once
#include <folly/io/async/EventBase.h>
#include <memory>
#include "FlipperScheduler.h"
namespace facebook {
namespace flipper {
@@ -32,35 +32,37 @@ class FlipperSocketProvider {
@param endpoint Endpoint to connect to.
@param payload Any configuration payload to establish a connection with
the specified endpoint.
@param eventBase A folly event base used to execute connection operations.
@param scheduler An scheduler used to schedule and execute connection
operations.
*/
virtual std::unique_ptr<FlipperSocket> create(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
folly::EventBase* eventBase) = 0;
Scheduler* scheduler) = 0;
/**
Create an instance of FlipperSocket.
@param endpoint Endpoint to connect to.
@param payload Any configuration payload to establish a connection with
the specified endpoint.
@param eventBase A folly event base used to execute connection operations.
@param scheduler An scheduler used to schedule and execute connection
operations.
@param connectionContextStore A connection context store used for obtaining
the certificate used for secure connections.
*/
virtual std::unique_ptr<FlipperSocket> create(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
folly::EventBase* eventBase,
Scheduler* scheduler,
ConnectionContextStore* connectionContextStore) = 0;
static std::unique_ptr<FlipperSocket> socketCreate(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
folly::EventBase* eventBase);
Scheduler* scheduler);
static std::unique_ptr<FlipperSocket> socketCreate(
FlipperConnectionEndpoint endpoint,
std::unique_ptr<FlipperSocketBasePayload> payload,
folly::EventBase* eventBase,
Scheduler* scheduler,
ConnectionContextStore* connectionContextStore);
static void setDefaultProvider(