naming_service.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. #ifndef BRPC_NAMING_SERVICE_H
  18. #define BRPC_NAMING_SERVICE_H
  19. #include <vector> // std::vector
  20. #include <string> // std::string
  21. #include <ostream> // std::ostream
  22. #include "butil/endpoint.h" // butil::EndPoint
  23. #include "butil/macros.h" // BAIDU_CONCAT
  24. #include "brpc/describable.h"
  25. #include "brpc/destroyable.h"
  26. #include "brpc/extension.h" // Extension<T>
  27. #include "brpc/server_node.h" // ServerNode
  28. namespace brpc {
  29. // Continuing actions to added/removed servers.
  30. // NOTE: You don't have to implement this class.
  31. class NamingServiceActions {
  32. public:
  33. virtual ~NamingServiceActions() {}
  34. virtual void AddServers(const std::vector<ServerNode>& servers) = 0;
  35. virtual void RemoveServers(const std::vector<ServerNode>& servers) = 0;
  36. virtual void ResetServers(const std::vector<ServerNode>& servers) = 0;
  37. };
  38. // Mapping a name to ServerNodes.
  39. class NamingService : public Describable, public Destroyable {
  40. public:
  41. // Implement this method to get servers associated with `service_name'
  42. // in periodic or event-driven manner, call methods of `actions' to
  43. // tell RPC system about server changes. This method will be run in
  44. // a dedicated bthread without access from other threads, thus the
  45. // implementation does NOT need to be thread-safe.
  46. // Return 0 on success, error code otherwise.
  47. virtual int RunNamingService(const char* service_name,
  48. NamingServiceActions* actions) = 0;
  49. // If this method returns true, RunNamingService will be called without
  50. // a dedicated bthread. As the name implies, this is suitable for static
  51. // and simple impl, saving the cost of creating a bthread. However most
  52. // impl of RunNamingService never quit, thread is a must to prevent the
  53. // method from blocking the caller.
  54. virtual bool RunNamingServiceReturnsQuickly() { return false; }
  55. // Create/destroy an instance.
  56. // Caller is responsible for Destroy() the instance after usage.
  57. virtual NamingService* New() const = 0;
  58. protected:
  59. virtual ~NamingService() {}
  60. };
  61. inline Extension<const NamingService>* NamingServiceExtension() {
  62. return Extension<const NamingService>::instance();
  63. }
  64. } // namespace brpc
  65. #endif // BRPC_NAMING_SERVICE_H