1 // 2 // 3 // Copyright 2016 gRPC authors. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // 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, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 // 17 // 18 19 #ifndef GRPC_SRC_CORE_LOAD_BALANCING_GRPCLB_LOAD_BALANCER_API_H 20 #define GRPC_SRC_CORE_LOAD_BALANCING_GRPCLB_LOAD_BALANCER_API_H 21 #include <grpc/support/port_platform.h> 22 23 #include <stdint.h> 24 25 #include <vector> 26 27 #include "absl/strings/string_view.h" 28 #include "upb/mem/arena.h" 29 30 #include <grpc/slice.h> 31 32 #include "src/core/load_balancing/grpclb/grpclb_client_stats.h" 33 #include "src/core/lib/gprpp/time.h" 34 35 #define GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH 128 36 #define GRPC_GRPCLB_SERVER_IP_ADDRESS_MAX_SIZE 16 37 #define GRPC_GRPCLB_SERVER_LOAD_BALANCE_TOKEN_MAX_SIZE 50 38 39 namespace grpc_core { 40 41 // Contains server information. When the drop field is not true, use the other 42 // fields. 43 struct GrpcLbServer { 44 int32_t ip_size; 45 char ip_addr[GRPC_GRPCLB_SERVER_IP_ADDRESS_MAX_SIZE]; 46 int32_t port; 47 char load_balance_token[GRPC_GRPCLB_SERVER_LOAD_BALANCE_TOKEN_MAX_SIZE]; 48 bool drop; 49 50 bool operator==(const GrpcLbServer& other) const; 51 }; 52 53 struct GrpcLbResponse { 54 enum { INITIAL, SERVERLIST, FALLBACK } type; 55 Duration client_stats_report_interval; 56 std::vector<GrpcLbServer> serverlist; 57 }; 58 59 // Creates a serialized grpclb request. 60 grpc_slice GrpcLbRequestCreate(absl::string_view lb_service_name, 61 upb_Arena* arena); 62 63 // Creates a serialized grpclb load report request. 64 grpc_slice GrpcLbLoadReportRequestCreate( 65 int64_t num_calls_started, int64_t num_calls_finished, 66 int64_t num_calls_finished_with_client_failed_to_send, 67 int64_t num_calls_finished_known_received, 68 const GrpcLbClientStats::DroppedCallCounts* drop_token_counts, 69 upb_Arena* arena); 70 71 // Deserialize a grpclb response. 72 bool GrpcLbResponseParse(const grpc_slice& serialized_response, 73 upb_Arena* arena, GrpcLbResponse* result); 74 75 } // namespace grpc_core 76 77 #endif // GRPC_SRC_CORE_LOAD_BALANCING_GRPCLB_LOAD_BALANCER_API_H 78