rpc_module.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. Copyright (c) 2021 Sogou, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #ifndef __RPC_MODULE_H__
  14. #define __RPC_MODULE_H__
  15. #include <mutex>
  16. #include <string>
  17. #include <list>
  18. #include "workflow/WFTask.h"
  19. #include "rpc_basic.h"
  20. #include "rpc_filter.h"
  21. namespace srpc
  22. {
  23. static constexpr char const *SRPC_COMPONENT_SRPC = "srpc.srpc";
  24. static constexpr char const *SRPC_SPAN_LOG = "srpc.log";
  25. static constexpr char const *SRPC_SPAN_EVENT = "event";
  26. static constexpr char const *SRPC_SPAN_MESSAGE = "message";
  27. static constexpr char const *SRPC_START_TIMESTAMP = "srpc.start_time";
  28. static constexpr char const *SRPC_FINISH_TIMESTAMP = "srpc.finish_time";
  29. static constexpr char const *SRPC_DURATION = "srpc.duration";
  30. static constexpr char const *SRPC_TIMEOUT_REASON = "srpc.timeout_reason";
  31. //for SnowFlake: u_id = [timestamp][group][machine][sequence]
  32. static constexpr int SRPC_TIMESTAMP_BITS = 38;
  33. static constexpr int SRPC_GROUP_BITS = 4;
  34. static constexpr int SRPC_MACHINE_BITS = 10;
  35. //static constexpr int SRPC_SEQUENCE_BITS = 12;
  36. static constexpr int SRPC_TOTAL_BITS = 64;
  37. class RPCModule
  38. {
  39. protected:
  40. virtual bool client_begin(SubTask *task, RPCModuleData& data) = 0;
  41. virtual bool server_begin(SubTask *task, RPCModuleData& data) = 0;
  42. virtual bool client_end(SubTask *task, RPCModuleData& data) = 0;
  43. virtual bool server_end(SubTask *task, RPCModuleData& data) = 0;
  44. public:
  45. void add_filter(RPCFilter *filter)
  46. {
  47. this->mutex.lock();
  48. this->filters.push_back(filter);
  49. this->mutex.unlock();
  50. }
  51. size_t get_filters_size() const { return this->filters.size(); }
  52. enum RPCModuleType get_module_type() const { return this->module_type; }
  53. bool client_task_begin(SubTask *task, RPCModuleData& data);
  54. bool server_task_begin(SubTask *task, RPCModuleData& data);
  55. bool client_task_end(SubTask *task, RPCModuleData& data);
  56. bool server_task_end(SubTask *task, RPCModuleData& data);
  57. RPCModule(enum RPCModuleType module_type)
  58. {
  59. this->module_type = module_type;
  60. }
  61. virtual ~RPCModule() {}
  62. private:
  63. enum RPCModuleType module_type;
  64. std::mutex mutex;
  65. std::list<RPCFilter *> filters;
  66. };
  67. class SnowFlake
  68. {
  69. public:
  70. SnowFlake(int timestamp_bits, int group_bits, int machine_bits);
  71. SnowFlake() :
  72. SnowFlake(SRPC_TIMESTAMP_BITS, SRPC_GROUP_BITS, SRPC_MACHINE_BITS)
  73. {
  74. }
  75. bool get_id(long long group_id, long long machine_id,
  76. unsigned long long *uid);
  77. private:
  78. std::atomic<long long> last_timestamp; // -1L;
  79. std::atomic<long long> sequence; // 0L;
  80. int timestamp_bits;
  81. int group_bits;
  82. int machine_bits;
  83. int sequence_bits;
  84. long long group_id_max;
  85. long long machine_id_max;
  86. long long sequence_max;
  87. long long timestamp_shift;
  88. long long group_shift;
  89. long long machine_shift;
  90. };
  91. bool http_get_header_module_data(const protocol::HttpMessage *msg,
  92. RPCModuleData& data);
  93. bool http_set_header_module_data(const RPCModuleData& data,
  94. protocol::HttpMessage *msg);
  95. } // end namespace srpc
  96. #endif