proxygen
|
#include <MultiFilePoller.h>
Classes | |
struct | CallbackDetail |
class | CallbackId |
Public Types | |
using | CallbackArg = std::unordered_map< std::string, std::string > |
using | Callback = folly::Function< void(const CallbackArg &newData) noexcept > |
Public Member Functions | |
MultiFilePoller (std::chrono::milliseconds pollInterval) | |
~MultiFilePoller ()=default | |
CallbackId | registerFile (std::string path, Callback cb) |
CallbackId | registerFiles (const std::vector< std::string > &paths, Callback cb) |
void | cancelCallback (const CallbackId &cbId) |
Private Types | |
using | StringReferences = std::vector< std::reference_wrapper< const std::string >> |
Private Member Functions | |
void | onFileUpdated (const std::string &triggeredPath) |
size_t | getNextCallbackId () |
Private Attributes | |
folly::SharedMutex | rwlock_ |
size_t | lastCallbackId_ = 0 |
std::unordered_map< std::string, std::vector< size_t > > | pathsToCallbackIds_ |
std::unordered_map< size_t, CallbackDetail > | idsToCallbacks_ |
wangle::FilePoller | poller_ |
An extension to wangle::FilePoller with the ability to register one or more callback on a file, and to track one or more file in a callback, and to deliver cached file data to callbacks.
Definition at line 32 of file MultiFilePoller.h.
using wangle::MultiFilePoller::Callback = folly::Function<void(const CallbackArg& newData) noexcept> |
Definition at line 58 of file MultiFilePoller.h.
using wangle::MultiFilePoller::CallbackArg = std::unordered_map<std::string, std::string> |
A callback: (1) takes as argument a map from a file path to its latest content. Unreadable paths will not show up in the map. (2) is triggered when any file it registers is changed, in the context of FilePoller thread. (3) once registered, cannot have its file list or callback pointer changed. To make changes, cancel the existing one and register a new one. Caveat: (1) If there are N files registered, in worst case the first N-1 callbacks would not get latest content of all N files. It's up to the callback to determine what to do for those cases. (2) Once a file, say, F, changes, not only F, but all files needed to trigger all callbacks that use F will be read. For example, Callback A needs files {a, b}; Callback B needs files {b, c}; Callback C needs files {d, e}; File {a} changes -> Reads {a, b} -> Calls A(a, b). File {b} changes -> Reads {a, b, c} -> Calls A(a, b), B(b, c). File {c} changes -> Reads {b, c} -> Calls B(b, c). File {d} or {e} changes -> Reads {d, e} -> Calls C(d, e).
Definition at line 57 of file MultiFilePoller.h.
|
private |
Definition at line 115 of file MultiFilePoller.h.
|
explicit |
pollInterval | Interval between polls. Setting a value less than 1_s may cause undesirable behavior because the minimum granularity for wangle::FilePoller to detect mtime difference is 1_s. |
Definition at line 27 of file MultiFilePoller.cpp.
|
default |
void wangle::MultiFilePoller::cancelCallback | ( | const CallbackId & | cbId | ) |
Cancel the specified Callback. May throw std::out_of_range if not found.
cbId | ID of the callback returned when registering it. |
Definition at line 72 of file MultiFilePoller.cpp.
References wangle::MultiFilePoller::CallbackId::id_, idsToCallbacks_, pathsToCallbackIds_, poller_, wangle::FilePoller::removeFileToTrack(), and rwlock_.
|
private |
Find an unused size_t value as callback Id. Caller must acquire wlock.
Definition at line 30 of file MultiFilePoller.cpp.
References idsToCallbacks_, and lastCallbackId_.
Referenced by registerFiles().
|
private |
The callback dispatcher to be registered to wangle::FilePoller.
Definition at line 101 of file MultiFilePoller.cpp.
References data, idsToCallbacks_, folly::gen::move, pathsToCallbackIds_, folly::readFile(), rwlock_, and string.
Referenced by registerFiles().
MultiFilePoller::CallbackId wangle::MultiFilePoller::registerFile | ( | std::string | path, |
Callback | cb | ||
) |
Add a callback to trigger when the specified file changes.
path | The path to monitor. |
cb | The callback to trigger. |
Definition at line 43 of file MultiFilePoller.cpp.
References folly::gen::move, and registerFiles().
MultiFilePoller::CallbackId wangle::MultiFilePoller::registerFiles | ( | const std::vector< std::string > & | paths, |
Callback | cb | ||
) |
Add a callback to trigger when any of the files in paths changes.
paths | The list of paths to monitor. Must be non-empty. |
cb | The callback to trigger. @ return An ID of the callback, used for cancellation. |
Definition at line 49 of file MultiFilePoller.cpp.
References wangle::FilePoller::addFileToTrack(), getNextCallbackId(), idsToCallbacks_, join, folly::gen::move, onFileUpdated(), pathsToCallbackIds_, poller_, and rwlock_.
Referenced by registerFile().
|
private |
Definition at line 129 of file MultiFilePoller.h.
Referenced by cancelCallback(), getNextCallbackId(), onFileUpdated(), and registerFiles().
|
private |
Definition at line 127 of file MultiFilePoller.h.
Referenced by getNextCallbackId().
|
private |
Definition at line 128 of file MultiFilePoller.h.
Referenced by cancelCallback(), onFileUpdated(), and registerFiles().
|
private |
Definition at line 132 of file MultiFilePoller.h.
Referenced by cancelCallback(), and registerFiles().
|
private |
Definition at line 126 of file MultiFilePoller.h.
Referenced by cancelCallback(), onFileUpdated(), and registerFiles().