• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2015 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 #include "test/core/util/reconnect_server.h"
20 
21 #include <grpc/grpc.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/log.h>
24 #include <grpc/support/sync.h>
25 #include <grpc/support/time.h>
26 #include <string.h>
27 
28 #include "src/core/lib/iomgr/endpoint.h"
29 #include "src/core/lib/iomgr/sockaddr.h"
30 #include "src/core/lib/iomgr/tcp_server.h"
31 #include "test/core/util/port.h"
32 #include "test/core/util/test_tcp_server.h"
33 
pretty_print_backoffs(reconnect_server * server)34 static void pretty_print_backoffs(reconnect_server* server) {
35   gpr_timespec diff;
36   int i = 1;
37   double expected_backoff = 1000.0, backoff;
38   timestamp_list* head = server->head;
39   gpr_log(GPR_INFO, "reconnect server: new connection");
40   for (head = server->head; head && head->next; head = head->next, i++) {
41     diff = gpr_time_sub(head->next->timestamp, head->timestamp);
42     backoff = gpr_time_to_millis(diff);
43     gpr_log(GPR_INFO,
44             "retry %2d:backoff %6.2fs,expected backoff %6.2fs, jitter %4.2f%%",
45             i, backoff / 1000.0, expected_backoff / 1000.0,
46             (backoff - expected_backoff) * 100.0 / expected_backoff);
47     expected_backoff *= 1.6;
48     int max_reconnect_backoff_ms = 120 * 1000;
49     if (server->max_reconnect_backoff_ms > 0) {
50       max_reconnect_backoff_ms = server->max_reconnect_backoff_ms;
51     }
52     if (expected_backoff > max_reconnect_backoff_ms) {
53       expected_backoff = max_reconnect_backoff_ms;
54     }
55   }
56 }
57 
on_connect(void * arg,grpc_endpoint * tcp,grpc_pollset *,grpc_tcp_server_acceptor * acceptor)58 static void on_connect(void* arg, grpc_endpoint* tcp,
59                        grpc_pollset* /*accepting_pollset*/,
60                        grpc_tcp_server_acceptor* acceptor) {
61   gpr_free(acceptor);
62   char* peer;
63   char* last_colon;
64   reconnect_server* server = static_cast<reconnect_server*>(arg);
65   gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
66   timestamp_list* new_tail;
67   peer = grpc_endpoint_get_peer(tcp);
68   grpc_endpoint_shutdown(tcp,
69                          GRPC_ERROR_CREATE_FROM_STATIC_STRING("Connected"));
70   grpc_endpoint_destroy(tcp);
71   if (peer) {
72     last_colon = strrchr(peer, ':');
73     if (server->peer == nullptr) {
74       server->peer = peer;
75     } else {
76       if (last_colon == nullptr) {
77         gpr_log(GPR_ERROR, "peer does not contain a ':'");
78       } else if (strncmp(server->peer, peer,
79                          static_cast<size_t>(last_colon - peer)) != 0) {
80         gpr_log(GPR_ERROR, "mismatched peer! %s vs %s", server->peer, peer);
81       }
82       gpr_free(peer);
83     }
84   }
85   new_tail = static_cast<timestamp_list*>(gpr_malloc(sizeof(timestamp_list)));
86   new_tail->timestamp = now;
87   new_tail->next = nullptr;
88   if (server->tail == nullptr) {
89     server->head = new_tail;
90     server->tail = new_tail;
91   } else {
92     server->tail->next = new_tail;
93     server->tail = new_tail;
94   }
95   pretty_print_backoffs(server);
96 }
97 
reconnect_server_init(reconnect_server * server)98 void reconnect_server_init(reconnect_server* server) {
99   test_tcp_server_init(&server->tcp_server, on_connect, server);
100   server->head = nullptr;
101   server->tail = nullptr;
102   server->peer = nullptr;
103   server->max_reconnect_backoff_ms = 0;
104 }
105 
reconnect_server_start(reconnect_server * server,int port)106 void reconnect_server_start(reconnect_server* server, int port) {
107   test_tcp_server_start(&server->tcp_server, port);
108 }
109 
reconnect_server_poll(reconnect_server * server,int seconds)110 void reconnect_server_poll(reconnect_server* server, int seconds) {
111   test_tcp_server_poll(&server->tcp_server, 1000 * seconds);
112 }
113 
reconnect_server_clear_timestamps(reconnect_server * server)114 void reconnect_server_clear_timestamps(reconnect_server* server) {
115   timestamp_list* new_head = server->head;
116   while (server->head) {
117     new_head = server->head->next;
118     gpr_free(server->head);
119     server->head = new_head;
120   }
121   server->tail = nullptr;
122   gpr_free(server->peer);
123   server->peer = nullptr;
124 }
125 
reconnect_server_destroy(reconnect_server * server)126 void reconnect_server_destroy(reconnect_server* server) {
127   reconnect_server_clear_timestamps(server);
128   test_tcp_server_destroy(&server->tcp_server);
129 }
130