Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2016 gRPC authors. |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
Craig Tiller | 9eb0fde | 2017-03-31 16:59:30 -0700 | [diff] [blame] | 19 | #include "src/core/ext/filters/client_channel/lb_policy/grpclb/load_balancer_api.h" |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 20 | #include "third_party/nanopb/pb_decode.h" |
| 21 | #include "third_party/nanopb/pb_encode.h" |
| 22 | |
| 23 | #include <grpc/support/alloc.h> |
| 24 | |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 25 | /* invoked once for every Server in ServerList */ |
| 26 | static bool count_serverlist(pb_istream_t *stream, const pb_field_t *field, |
| 27 | void **arg) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 28 | grpc_grpclb_serverlist *sl = (grpc_grpclb_serverlist *)*arg; |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 29 | grpc_grpclb_server server; |
| 30 | if (!pb_decode(stream, grpc_lb_v1_Server_fields, &server)) { |
| 31 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream)); |
| 32 | return false; |
| 33 | } |
| 34 | ++sl->num_servers; |
| 35 | return true; |
| 36 | } |
| 37 | |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 38 | typedef struct decode_serverlist_arg { |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 39 | /* The decoding callback is invoked once per server in serverlist. Remember |
| 40 | * which index of the serverlist are we currently decoding */ |
| 41 | size_t decoding_idx; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 42 | /* The decoded serverlist */ |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 43 | grpc_grpclb_serverlist *serverlist; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 44 | } decode_serverlist_arg; |
| 45 | |
| 46 | /* invoked once for every Server in ServerList */ |
| 47 | static bool decode_serverlist(pb_istream_t *stream, const pb_field_t *field, |
| 48 | void **arg) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 49 | decode_serverlist_arg *dec_arg = (decode_serverlist_arg *)*arg; |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 50 | GPR_ASSERT(dec_arg->serverlist->num_servers >= dec_arg->decoding_idx); |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 51 | grpc_grpclb_server *server = |
| 52 | (grpc_grpclb_server *)gpr_zalloc(sizeof(grpc_grpclb_server)); |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 53 | if (!pb_decode(stream, grpc_lb_v1_Server_fields, server)) { |
| 54 | gpr_free(server); |
| 55 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(stream)); |
| 56 | return false; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 57 | } |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 58 | dec_arg->serverlist->servers[dec_arg->decoding_idx++] = server; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 59 | return true; |
| 60 | } |
| 61 | |
| 62 | grpc_grpclb_request *grpc_grpclb_request_create(const char *lb_service_name) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 63 | grpc_grpclb_request *req = |
| 64 | (grpc_grpclb_request *)gpr_malloc(sizeof(grpc_grpclb_request)); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 65 | req->has_client_stats = false; |
| 66 | req->has_initial_request = true; |
| 67 | req->initial_request.has_name = true; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 68 | strncpy(req->initial_request.name, lb_service_name, |
| 69 | GRPC_GRPCLB_SERVICE_NAME_MAX_LENGTH); |
| 70 | return req; |
| 71 | } |
| 72 | |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 73 | static void populate_timestamp(gpr_timespec timestamp, |
| 74 | struct _grpc_lb_v1_Timestamp *timestamp_pb) { |
| 75 | timestamp_pb->has_seconds = true; |
| 76 | timestamp_pb->seconds = timestamp.tv_sec; |
| 77 | timestamp_pb->has_nanos = true; |
| 78 | timestamp_pb->nanos = timestamp.tv_nsec; |
| 79 | } |
| 80 | |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 81 | static bool encode_string(pb_ostream_t *stream, const pb_field_t *field, |
| 82 | void *const *arg) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 83 | char *str = (char *)*arg; |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 84 | if (!pb_encode_tag_for_field(stream, field)) return false; |
| 85 | return pb_encode_string(stream, (uint8_t *)str, strlen(str)); |
| 86 | } |
| 87 | |
| 88 | static bool encode_drops(pb_ostream_t *stream, const pb_field_t *field, |
| 89 | void *const *arg) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 90 | grpc_grpclb_dropped_call_counts *drop_entries = |
| 91 | (grpc_grpclb_dropped_call_counts *)*arg; |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 92 | if (drop_entries == NULL) return true; |
| 93 | for (size_t i = 0; i < drop_entries->num_entries; ++i) { |
| 94 | if (!pb_encode_tag_for_field(stream, field)) return false; |
| 95 | grpc_lb_v1_ClientStatsPerToken drop_message; |
| 96 | drop_message.load_balance_token.funcs.encode = encode_string; |
| 97 | drop_message.load_balance_token.arg = drop_entries->token_counts[i].token; |
| 98 | drop_message.has_num_calls = true; |
| 99 | drop_message.num_calls = drop_entries->token_counts[i].count; |
| 100 | if (!pb_encode_submessage(stream, grpc_lb_v1_ClientStatsPerToken_fields, |
| 101 | &drop_message)) { |
| 102 | return false; |
| 103 | } |
| 104 | } |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | grpc_grpclb_request *grpc_grpclb_load_report_request_create_locked( |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 109 | grpc_grpclb_client_stats *client_stats) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 110 | grpc_grpclb_request *req = |
| 111 | (grpc_grpclb_request *)gpr_zalloc(sizeof(grpc_grpclb_request)); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 112 | req->has_client_stats = true; |
| 113 | req->client_stats.has_timestamp = true; |
| 114 | populate_timestamp(gpr_now(GPR_CLOCK_REALTIME), &req->client_stats.timestamp); |
| 115 | req->client_stats.has_num_calls_started = true; |
| 116 | req->client_stats.has_num_calls_finished = true; |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 117 | req->client_stats.has_num_calls_finished_with_client_failed_to_send = true; |
| 118 | req->client_stats.has_num_calls_finished_with_client_failed_to_send = true; |
| 119 | req->client_stats.has_num_calls_finished_known_received = true; |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 120 | req->client_stats.calls_finished_with_drop.funcs.encode = encode_drops; |
| 121 | grpc_grpclb_client_stats_get_locked( |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 122 | client_stats, &req->client_stats.num_calls_started, |
| 123 | &req->client_stats.num_calls_finished, |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 124 | &req->client_stats.num_calls_finished_with_client_failed_to_send, |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 125 | &req->client_stats.num_calls_finished_known_received, |
| 126 | (grpc_grpclb_dropped_call_counts **)&req->client_stats |
| 127 | .calls_finished_with_drop.arg); |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 128 | return req; |
| 129 | } |
| 130 | |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 131 | grpc_slice grpc_grpclb_request_encode(const grpc_grpclb_request *request) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 132 | size_t encoded_length; |
| 133 | pb_ostream_t sizestream; |
| 134 | pb_ostream_t outputstream; |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 135 | grpc_slice slice; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 136 | memset(&sizestream, 0, sizeof(pb_ostream_t)); |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 137 | pb_encode(&sizestream, grpc_lb_v1_LoadBalanceRequest_fields, request); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 138 | encoded_length = sizestream.bytes_written; |
| 139 | |
Craig Tiller | 423d6fd | 2017-04-12 13:15:45 -0700 | [diff] [blame] | 140 | slice = GRPC_SLICE_MALLOC(encoded_length); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 141 | outputstream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 142 | pb_ostream_from_buffer(GRPC_SLICE_START_PTR(slice), encoded_length); |
David Garcia Quintas | 7f0793a | 2016-04-25 12:35:58 -0700 | [diff] [blame] | 143 | GPR_ASSERT(pb_encode(&outputstream, grpc_lb_v1_LoadBalanceRequest_fields, |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 144 | request) != 0); |
| 145 | return slice; |
| 146 | } |
| 147 | |
| 148 | void grpc_grpclb_request_destroy(grpc_grpclb_request *request) { |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 149 | if (request->has_client_stats) { |
| 150 | grpc_grpclb_dropped_call_counts *drop_entries = |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 151 | (grpc_grpclb_dropped_call_counts *) |
| 152 | request->client_stats.calls_finished_with_drop.arg; |
Mark D. Roth | e775180 | 2017-07-27 12:31:45 -0700 | [diff] [blame] | 153 | grpc_grpclb_dropped_call_counts_destroy(drop_entries); |
| 154 | } |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 155 | gpr_free(request); |
| 156 | } |
| 157 | |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 158 | typedef grpc_lb_v1_LoadBalanceResponse grpc_grpclb_response; |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 159 | grpc_grpclb_initial_response *grpc_grpclb_initial_response_parse( |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 160 | grpc_slice encoded_grpc_grpclb_response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 161 | pb_istream_t stream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 162 | pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response), |
| 163 | GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response)); |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 164 | grpc_grpclb_response res; |
| 165 | memset(&res, 0, sizeof(grpc_grpclb_response)); |
| 166 | if (!pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res)) { |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 167 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 168 | return NULL; |
| 169 | } |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 170 | |
| 171 | if (!res.has_initial_response) return NULL; |
| 172 | |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 173 | grpc_grpclb_initial_response *initial_res = |
Yash Tibrewal | bc130da | 2017-09-12 22:44:08 -0700 | [diff] [blame] | 174 | (grpc_grpclb_initial_response *)gpr_malloc( |
| 175 | sizeof(grpc_grpclb_initial_response)); |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 176 | memcpy(initial_res, &res.initial_response, |
| 177 | sizeof(grpc_grpclb_initial_response)); |
| 178 | |
| 179 | return initial_res; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | grpc_grpclb_serverlist *grpc_grpclb_response_parse_serverlist( |
Craig Tiller | d41a4a7 | 2016-10-26 16:16:06 -0700 | [diff] [blame] | 183 | grpc_slice encoded_grpc_grpclb_response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 184 | pb_istream_t stream = |
Craig Tiller | 618e67d | 2016-10-26 21:08:10 -0700 | [diff] [blame] | 185 | pb_istream_from_buffer(GRPC_SLICE_START_PTR(encoded_grpc_grpclb_response), |
| 186 | GRPC_SLICE_LENGTH(encoded_grpc_grpclb_response)); |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 187 | pb_istream_t stream_at_start = stream; |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 188 | grpc_grpclb_serverlist *sl = |
| 189 | (grpc_grpclb_serverlist *)gpr_zalloc(sizeof(grpc_grpclb_serverlist)); |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 190 | grpc_grpclb_response res; |
| 191 | memset(&res, 0, sizeof(grpc_grpclb_response)); |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 192 | // First pass: count number of servers. |
| 193 | res.server_list.servers.funcs.decode = count_serverlist; |
| 194 | res.server_list.servers.arg = sl; |
| 195 | bool status = pb_decode(&stream, grpc_lb_v1_LoadBalanceResponse_fields, &res); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 196 | if (!status) { |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 197 | gpr_free(sl); |
David Garcia Quintas | 18bc43b | 2016-08-22 15:05:32 -0700 | [diff] [blame] | 198 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 199 | return NULL; |
| 200 | } |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 201 | // Second pass: populate servers. |
| 202 | if (sl->num_servers > 0) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 203 | sl->servers = (grpc_grpclb_server **)gpr_zalloc( |
| 204 | sizeof(grpc_grpclb_server *) * sl->num_servers); |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 205 | decode_serverlist_arg decode_arg; |
| 206 | memset(&decode_arg, 0, sizeof(decode_arg)); |
| 207 | decode_arg.serverlist = sl; |
| 208 | res.server_list.servers.funcs.decode = decode_serverlist; |
| 209 | res.server_list.servers.arg = &decode_arg; |
| 210 | status = pb_decode(&stream_at_start, grpc_lb_v1_LoadBalanceResponse_fields, |
| 211 | &res); |
| 212 | if (!status) { |
| 213 | grpc_grpclb_destroy_serverlist(sl); |
| 214 | gpr_log(GPR_ERROR, "nanopb error: %s", PB_GET_ERROR(&stream)); |
| 215 | return NULL; |
| 216 | } |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 217 | } |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 218 | if (res.server_list.has_expiration_interval) { |
| 219 | sl->expiration_interval = res.server_list.expiration_interval; |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 220 | } |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 221 | return sl; |
| 222 | } |
| 223 | |
| 224 | void grpc_grpclb_destroy_serverlist(grpc_grpclb_serverlist *serverlist) { |
David Garcia Quintas | bf2e73d | 2016-04-04 18:03:47 -0700 | [diff] [blame] | 225 | if (serverlist == NULL) { |
| 226 | return; |
| 227 | } |
| 228 | for (size_t i = 0; i < serverlist->num_servers; i++) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 229 | gpr_free(serverlist->servers[i]); |
| 230 | } |
| 231 | gpr_free(serverlist->servers); |
| 232 | gpr_free(serverlist); |
| 233 | } |
| 234 | |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 235 | grpc_grpclb_serverlist *grpc_grpclb_serverlist_copy( |
| 236 | const grpc_grpclb_serverlist *sl) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 237 | grpc_grpclb_serverlist *copy = |
| 238 | (grpc_grpclb_serverlist *)gpr_zalloc(sizeof(grpc_grpclb_serverlist)); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 239 | copy->num_servers = sl->num_servers; |
| 240 | memcpy(©->expiration_interval, &sl->expiration_interval, |
| 241 | sizeof(grpc_grpclb_duration)); |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 242 | copy->servers = (grpc_grpclb_server **)gpr_malloc( |
| 243 | sizeof(grpc_grpclb_server *) * sl->num_servers); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 244 | for (size_t i = 0; i < sl->num_servers; i++) { |
Yash Tibrewal | ca3c1c0 | 2017-09-07 22:47:16 -0700 | [diff] [blame] | 245 | copy->servers[i] = |
| 246 | (grpc_grpclb_server *)gpr_malloc(sizeof(grpc_grpclb_server)); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 247 | memcpy(copy->servers[i], sl->servers[i], sizeof(grpc_grpclb_server)); |
| 248 | } |
| 249 | return copy; |
| 250 | } |
| 251 | |
| 252 | bool grpc_grpclb_serverlist_equals(const grpc_grpclb_serverlist *lhs, |
| 253 | const grpc_grpclb_serverlist *rhs) { |
Mark D. Roth | d7389b4 | 2017-05-17 12:22:17 -0700 | [diff] [blame] | 254 | if (lhs == NULL || rhs == NULL) { |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 255 | return false; |
| 256 | } |
| 257 | if (lhs->num_servers != rhs->num_servers) { |
| 258 | return false; |
| 259 | } |
| 260 | if (grpc_grpclb_duration_compare(&lhs->expiration_interval, |
| 261 | &rhs->expiration_interval) != 0) { |
| 262 | return false; |
| 263 | } |
| 264 | for (size_t i = 0; i < lhs->num_servers; i++) { |
| 265 | if (!grpc_grpclb_server_equals(lhs->servers[i], rhs->servers[i])) { |
| 266 | return false; |
| 267 | } |
| 268 | } |
| 269 | return true; |
| 270 | } |
| 271 | |
| 272 | bool grpc_grpclb_server_equals(const grpc_grpclb_server *lhs, |
| 273 | const grpc_grpclb_server *rhs) { |
| 274 | return memcmp(lhs, rhs, sizeof(grpc_grpclb_server)) == 0; |
| 275 | } |
| 276 | |
| 277 | int grpc_grpclb_duration_compare(const grpc_grpclb_duration *lhs, |
| 278 | const grpc_grpclb_duration *rhs) { |
| 279 | GPR_ASSERT(lhs && rhs); |
| 280 | if (lhs->has_seconds && rhs->has_seconds) { |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 281 | if (lhs->seconds < rhs->seconds) return -1; |
| 282 | if (lhs->seconds > rhs->seconds) return 1; |
| 283 | } else if (lhs->has_seconds) { |
| 284 | return 1; |
| 285 | } else if (rhs->has_seconds) { |
| 286 | return -1; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 287 | } |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 288 | |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 289 | GPR_ASSERT(lhs->seconds == rhs->seconds); |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 290 | if (lhs->has_nanos && rhs->has_nanos) { |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 291 | if (lhs->nanos < rhs->nanos) return -1; |
| 292 | if (lhs->nanos > rhs->nanos) return 1; |
| 293 | } else if (lhs->has_nanos) { |
| 294 | return 1; |
| 295 | } else if (rhs->has_nanos) { |
| 296 | return -1; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 297 | } |
David Garcia Quintas | 4166cb0 | 2016-07-29 14:33:15 -0700 | [diff] [blame] | 298 | |
| 299 | return 0; |
David Garcia Quintas | 3fb8f73 | 2016-06-15 22:53:08 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Mark D. Roth | 09e458c | 2017-05-02 08:13:26 -0700 | [diff] [blame] | 302 | gpr_timespec grpc_grpclb_duration_to_timespec( |
| 303 | grpc_grpclb_duration *duration_pb) { |
| 304 | gpr_timespec duration; |
| 305 | duration.tv_sec = duration_pb->has_seconds ? duration_pb->seconds : 0; |
| 306 | duration.tv_nsec = duration_pb->has_nanos ? duration_pb->nanos : 0; |
| 307 | duration.clock_type = GPR_TIMESPAN; |
| 308 | return duration; |
| 309 | } |
| 310 | |
David Garcia Quintas | 1d5cb2a | 2016-07-18 12:56:53 -0700 | [diff] [blame] | 311 | void grpc_grpclb_initial_response_destroy( |
| 312 | grpc_grpclb_initial_response *response) { |
Craig Tiller | eb841e2 | 2016-02-11 15:49:16 -0800 | [diff] [blame] | 313 | gpr_free(response); |
| 314 | } |