Files
flipper/xplat/Flipper/FlipperClient.h
Michel Weststrate b9c3d99f44 Stop connecting disabled background plugins
Summary:
Background for this diff: https://fb.quip.com/KqEfAlKYlgme

Some plugins don't respect that stuff (livefeed and graphql), but for others it seems to work fine.

This is just a PoC, there are some present bugs concerning the combination of selecting and bg plugins

Questions to investigate:

- [x] make sure that LiveFeed and GraphQL disconnect properly. There might be more plugins that need that
- [x] verifiy that we don't loose one of the original goals of background plugins, e.g. QPL collecting and sending data from device start. Does this still work as intended after this change?
- [x] how can we observe / measure improvements? Are dev builds more responsive after this? Is the layout inspector smoother for example because no QPL plugins are interweaved?
- [x] how is forward and backward compatibility?
   - If Flipper is updated, but device not: No change I think, as getBackgroundPlugins() will return an empty set, and background plugins are initiated as usual, so old behavior
  - If device is updated, but Flipper not, background plugins won't be started until they are selected. This is a degradation, but hopefully explainable.
- [x] Verify QPL buffer is not unbounded
- [x] Share architecutre changes with team

For Graphql updates: D20943455

Added runtime stats to monitor network traffic (sadly had to redo that since scuba couldn't handle the data format used at first, so probably will hold of landing this diff a week to make sure we can see some effects)

Follow up work:

[x] wait until we released the stat tracking before we release this, to be able to measure the effect?
[x] make sure graphql fix lands
[ ] use side effects abstraction
[ ] fix other background plugins (android only) or fix it in a generic way:

{F234394286}

Changelog: Background plugins will no longer receive a Flipper connection if they are disabled. This should significantly reduce the overall load of Flipper both on the device and desktop when unused plugins are disabled used, which could otherwise generate 10MB/s of network traffic certain scenarios. All plugins *should* be able to handle to this gracefully, but since this is quite a fundamental change, reach out to the Flipper team when in doubt!

Reviewed By: jknoxville

Differential Revision: D20942453

fbshipit-source-id: b699199cb95c1b3e4c36e026b6dfaee7d1652e1f
2020-04-27 09:46:13 -07:00

119 lines
3.2 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <map>
#include <mutex>
#include <vector>
#include "FlipperConnectionImpl.h"
#include "FlipperConnectionManager.h"
#include "FlipperInitConfig.h"
#include "FlipperPlugin.h"
#include "FlipperState.h"
#include "FlipperStep.h"
namespace facebook {
namespace flipper {
class FlipperClient : public FlipperConnectionManager::Callbacks {
public:
/**
Call before accessing instance with FlipperClient::instance(). This will set
up all the state needed to establish a Flipper connection.
*/
static void init(FlipperInitConfig config);
/**
Standard accessor for the shared FlipperClient instance. This returns a
singleton instance to a shared FlipperClient. First call to this function
will create the shared FlipperClient. Must call
FlipperClient::initDeviceData() before first call to
FlipperClient::instance().
*/
static FlipperClient* instance();
/**
Only public for testing
*/
FlipperClient(
std::unique_ptr<FlipperConnectionManager> socket,
std::shared_ptr<FlipperState> state)
: socket_(std::move(socket)), flipperState_(state) {
auto step = flipperState_->start("Create client");
socket_->setCallbacks(this);
auto& conn = connections_["flipper-crash-report"];
conn = std::make_shared<FlipperConnectionImpl>(
socket_.get(), "flipper-crash-report");
step->complete();
}
void start() {
performAndReportError([this]() {
auto step = flipperState_->start("Start client");
socket_->start();
step->complete();
});
}
void stop() {
performAndReportError([this]() {
auto step = flipperState_->start("Stop client");
socket_->stop();
step->complete();
});
}
void onConnected() override;
void onDisconnected() override;
void onMessageReceived(
const folly::dynamic& message,
std::unique_ptr<FlipperResponder>) override;
void addPlugin(std::shared_ptr<FlipperPlugin> plugin);
void removePlugin(std::shared_ptr<FlipperPlugin> plugin);
void refreshPlugins();
void setStateListener(
std::shared_ptr<FlipperStateUpdateListener> stateListener);
std::shared_ptr<FlipperPlugin> getPlugin(const std::string& identifier);
std::string getState();
std::vector<StateElement> getStateElements();
template <typename P>
std::shared_ptr<P> getPlugin(const std::string& identifier) {
return std::static_pointer_cast<P>(getPlugin(identifier));
}
bool hasPlugin(const std::string& identifier);
void performAndReportError(const std::function<void()>& func);
private:
static FlipperClient* instance_;
bool connected_ = false;
std::unique_ptr<FlipperConnectionManager> socket_;
std::map<std::string, std::shared_ptr<FlipperPlugin>> plugins_;
std::map<std::string, std::shared_ptr<FlipperConnectionImpl>> connections_;
std::mutex mutex_;
std::shared_ptr<FlipperState> flipperState_;
void connect(std::shared_ptr<FlipperPlugin> plugin);
void disconnect(std::shared_ptr<FlipperPlugin> plugin);
std::string callstack();
void handleError(std::exception& e);
};
} // namespace flipper
} // namespace facebook