Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: third_party/grpc/test/core/util/reconnect_server.c

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 #include "test/core/util/reconnect_server.h"
35
36 #include <grpc/grpc.h>
37 #include <grpc/support/alloc.h>
38 #include <grpc/support/host_port.h>
39 #include <grpc/support/log.h>
40 #include <grpc/support/sync.h>
41 #include <grpc/support/time.h>
42 #include <string.h>
43 #include "src/core/iomgr/endpoint.h"
44 #include "src/core/iomgr/sockaddr.h"
45 #include "src/core/iomgr/tcp_server.h"
46 #include "test/core/util/port.h"
47 #include "test/core/util/test_tcp_server.h"
48
49 static void pretty_print_backoffs(reconnect_server *server) {
50 gpr_timespec diff;
51 int i = 1;
52 double expected_backoff = 1000.0, backoff;
53 timestamp_list *head = server->head;
54 gpr_log(GPR_INFO, "reconnect server: new connection");
55 for (head = server->head; head && head->next; head = head->next, i++) {
56 diff = gpr_time_sub(head->next->timestamp, head->timestamp);
57 backoff = gpr_time_to_millis(diff);
58 gpr_log(GPR_INFO,
59 "retry %2d:backoff %6.2fs,expected backoff %6.2fs, jitter %4.2f%%",
60 i, backoff / 1000.0, expected_backoff / 1000.0,
61 (backoff - expected_backoff) * 100.0 / expected_backoff);
62 expected_backoff *= 1.6;
63 if (expected_backoff > 120 * 1000) {
64 expected_backoff = 120 * 1000;
65 }
66 }
67 }
68
69 static void on_connect(grpc_exec_ctx *exec_ctx, void *arg, grpc_endpoint *tcp,
70 grpc_tcp_server_acceptor *acceptor) {
71 char *peer;
72 char *last_colon;
73 reconnect_server *server = (reconnect_server *)arg;
74 gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME);
75 timestamp_list *new_tail;
76 peer = grpc_endpoint_get_peer(tcp);
77 grpc_endpoint_shutdown(exec_ctx, tcp);
78 grpc_endpoint_destroy(exec_ctx, tcp);
79 if (peer) {
80 last_colon = strrchr(peer, ':');
81 if (server->peer == NULL) {
82 server->peer = peer;
83 } else {
84 if (last_colon == NULL) {
85 gpr_log(GPR_ERROR, "peer does not contain a ':'");
86 } else if (strncmp(server->peer, peer, (size_t)(last_colon - peer)) !=
87 0) {
88 gpr_log(GPR_ERROR, "mismatched peer! %s vs %s", server->peer, peer);
89 }
90 gpr_free(peer);
91 }
92 }
93 new_tail = gpr_malloc(sizeof(timestamp_list));
94 new_tail->timestamp = now;
95 new_tail->next = NULL;
96 if (server->tail == NULL) {
97 server->head = new_tail;
98 server->tail = new_tail;
99 } else {
100 server->tail->next = new_tail;
101 server->tail = new_tail;
102 }
103 pretty_print_backoffs(server);
104 }
105
106 void reconnect_server_init(reconnect_server *server) {
107 test_tcp_server_init(&server->tcp_server, on_connect, server);
108 server->head = NULL;
109 server->tail = NULL;
110 server->peer = NULL;
111 }
112
113 void reconnect_server_start(reconnect_server *server, int port) {
114 test_tcp_server_start(&server->tcp_server, port);
115 }
116
117 void reconnect_server_poll(reconnect_server *server, int seconds) {
118 test_tcp_server_poll(&server->tcp_server, seconds);
119 }
120
121 void reconnect_server_clear_timestamps(reconnect_server *server) {
122 timestamp_list *new_head = server->head;
123 while (server->head) {
124 new_head = server->head->next;
125 gpr_free(server->head);
126 server->head = new_head;
127 }
128 server->tail = NULL;
129 gpr_free(server->peer);
130 server->peer = NULL;
131 }
132
133 void reconnect_server_destroy(reconnect_server *server) {
134 reconnect_server_clear_timestamps(server);
135 test_tcp_server_destroy(&server->tcp_server);
136 }
OLDNEW
« no previous file with comments | « third_party/grpc/test/core/util/reconnect_server.h ('k') | third_party/grpc/test/core/util/slice_splitter.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698