shibd commented on PR #170:
URL: 
https://github.com/apache/pulsar-client-cpp/pull/170#issuecomment-1396581433

   > In short, a natural design is to save the `ServiceUrlProvider` in `Client` 
and call `getServiceUrl()` method (in C++ it's just a function invocation) each 
time we need a service URL. Maybe we need to maintain a key-value (`serviceUrl 
-> ServiceNameResolver`), when the `serviceUrlProvider()` returns a different 
result from the key, creating a new `ServiceNameResolver` for related lookup 
requests.
   
   I think you only need the client to provide the `updateServiceUrl` method. 
   
   Users can take advantage of this approach to implement their own services. 
(Pseudocode)
   
   
   ```c++
   class Store;
   class AutomaticClusterService {
      public:
       explicit AutomaticClusterService(const Store& store, const Client 
pulsarClient)
           : store_(store), pulsarClient_(pulsarClient) {
           serviceUrlCache_ = store.queryServiceUrl();
           updateServiceUrlInterval_ = boost::posix_time::seconds(60);
           runUpdateServiceurl();
       }
   
       void start() {
           runUpdateServiceurl();
       }
   
       const std::string& getServiceUrl(){
           return serviceUrlCache_;
       }
   
       const void runUpdateServiceurl() {
           timer_->expires_from_now(updateServiceUrlInterval_);
           auto availableServiceUrl = store_.queryServiceUrl();
           timer_->async_wait([this](const boost::system::error_code& ec) {
             // Maybe some other implementation
             if (serviceUrlCache_ != availableServiceUrl) {
                 serviceUrlCache_ = availableServiceUrl;
                 pulsarClient_.updateServiceUrl(serviceUrlCache_);
               }
               runUpdateServiceurl();
           });
       }
   
   
      private:
       // External storage services
       Store store_;
       const Client pulsarClient_;
       std::string serviceUrlCache_;
       std::shared_ptr<boost::asio::deadline_timer> timer_;
       boost::posix_time::time_duration updateServiceUrlInterval_;
   };
   
   Store store;
   Client client(store.queryServiceUrl());
   
   AutomaticClusterService  automaticClusterService(store, client);
   automaticClusterService.start();
   
   //....
   
   ```


-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to