proxygen
MultiFilePoller.h
Go to the documentation of this file.
1 /*
2  * Copyright 2004-present Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 
18 #include <chrono>
19 #include <unordered_map>
20 
21 #include <folly/Function.h>
22 #include <folly/SharedMutex.h>
23 #include <wangle/util/FilePoller.h>
24 
25 namespace wangle {
26 
33  public:
57  using CallbackArg = std::unordered_map<std::string, std::string>;
59 
64  class CallbackId final {
65  private:
66  explicit CallbackId(size_t id) : id_(id) {
67  }
68  friend class MultiFilePoller;
69  size_t id_;
70  };
71 
77  explicit MultiFilePoller(std::chrono::milliseconds pollInterval);
78 
79  ~MultiFilePoller() = default;
80 
88 
95  CallbackId registerFiles(const std::vector<std::string>& paths, Callback cb);
96 
101  void cancelCallback(const CallbackId& cbId);
102 
103  private:
107  void onFileUpdated(const std::string& triggeredPath);
108 
112  size_t getNextCallbackId();
113 
114  using StringReferences =
115  std::vector<std::reference_wrapper<const std::string>>;
116 
117  struct CallbackDetail {
119  : files_(std::move(files)), cb_(std::move(cb)) {
120  }
123  };
124 
125  // The following data structures are protected by the mutex.
127  size_t lastCallbackId_ = 0;
128  std::unordered_map<std::string, std::vector<size_t>> pathsToCallbackIds_;
129  std::unordered_map<size_t, CallbackDetail> idsToCallbacks_;
130 
131  // The following data structures are set by ctor only.
133 };
134 
135 } // namespace wangle
std::unordered_map< std::string, std::vector< size_t > > pathsToCallbackIds_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
STL namespace.
void onFileUpdated(const std::string &triggeredPath)
std::unordered_map< std::string, std::string > CallbackArg
std::vector< std::reference_wrapper< const std::string >> StringReferences
std::unique_ptr< AsyncFizzServer::HandshakeCallback > cb_
wangle::FilePoller poller_
std::unordered_map< size_t, CallbackDetail > idsToCallbacks_
void cancelCallback(const CallbackId &cbId)
folly::SharedMutex rwlock_
CallbackId registerFile(std::string path, Callback cb)
const char * string
Definition: Conv.cpp:212
CallbackDetail(StringReferences files, Callback cb)
CallbackId registerFiles(const std::vector< std::string > &paths, Callback cb)