mirror of https://github.com/grpc/grpc.git
commit
22de7b65c0
58 changed files with 1475 additions and 893 deletions
@ -1,168 +0,0 @@ |
|||||||
/*
|
|
||||||
* |
|
||||||
* Copyright 2015, Google Inc. |
|
||||||
* All rights reserved. |
|
||||||
* |
|
||||||
* Redistribution and use in source and binary forms, with or without |
|
||||||
* modification, are permitted provided that the following conditions are |
|
||||||
* met: |
|
||||||
* |
|
||||||
* * Redistributions of source code must retain the above copyright |
|
||||||
* notice, this list of conditions and the following disclaimer. |
|
||||||
* * Redistributions in binary form must reproduce the above |
|
||||||
* copyright notice, this list of conditions and the following disclaimer |
|
||||||
* in the documentation and/or other materials provided with the |
|
||||||
* distribution. |
|
||||||
* * Neither the name of Google Inc. nor the names of its |
|
||||||
* contributors may be used to endorse or promote products derived from |
|
||||||
* this software without specific prior written permission. |
|
||||||
* |
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
||||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
||||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
||||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
||||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
||||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
||||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
||||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
||||||
* |
|
||||||
*/ |
|
||||||
|
|
||||||
#include <grpc/support/port_platform.h> |
|
||||||
|
|
||||||
#ifdef GPR_POSIX_SOCKET |
|
||||||
#include "src/core/iomgr/pollset_kick_posix.h" |
|
||||||
|
|
||||||
#include <errno.h> |
|
||||||
#include <string.h> |
|
||||||
#include <unistd.h> |
|
||||||
|
|
||||||
#include "src/core/iomgr/socket_utils_posix.h" |
|
||||||
#include "src/core/iomgr/wakeup_fd_posix.h" |
|
||||||
#include <grpc/support/alloc.h> |
|
||||||
#include <grpc/support/log.h> |
|
||||||
|
|
||||||
/* This implementation is based on a freelist of wakeup fds, with extra logic to
|
|
||||||
* handle kicks while there is no attached fd. */ |
|
||||||
|
|
||||||
/* TODO(klempner): Autosize this, and consider providing a way to disable the
|
|
||||||
* cap entirely on systems with large fd limits */ |
|
||||||
#define GRPC_MAX_CACHED_WFDS 50 |
|
||||||
|
|
||||||
static grpc_kick_fd_info *fd_freelist = NULL; |
|
||||||
static int fd_freelist_count = 0; |
|
||||||
static gpr_mu fd_freelist_mu; |
|
||||||
|
|
||||||
static grpc_kick_fd_info *allocate_wfd(void) { |
|
||||||
grpc_kick_fd_info *info = NULL; |
|
||||||
gpr_mu_lock(&fd_freelist_mu); |
|
||||||
if (fd_freelist != NULL) { |
|
||||||
info = fd_freelist; |
|
||||||
fd_freelist = fd_freelist->next; |
|
||||||
--fd_freelist_count; |
|
||||||
} |
|
||||||
gpr_mu_unlock(&fd_freelist_mu); |
|
||||||
if (info == NULL) { |
|
||||||
info = gpr_malloc(sizeof(*info)); |
|
||||||
grpc_wakeup_fd_create(&info->wakeup_fd); |
|
||||||
info->next = NULL; |
|
||||||
} |
|
||||||
return info; |
|
||||||
} |
|
||||||
|
|
||||||
static void destroy_wfd(grpc_kick_fd_info *wfd) { |
|
||||||
grpc_wakeup_fd_destroy(&wfd->wakeup_fd); |
|
||||||
gpr_free(wfd); |
|
||||||
} |
|
||||||
|
|
||||||
static void free_wfd(grpc_kick_fd_info *fd_info) { |
|
||||||
gpr_mu_lock(&fd_freelist_mu); |
|
||||||
if (fd_freelist_count < GRPC_MAX_CACHED_WFDS) { |
|
||||||
fd_info->next = fd_freelist; |
|
||||||
fd_freelist = fd_info; |
|
||||||
fd_freelist_count++; |
|
||||||
fd_info = NULL; |
|
||||||
} |
|
||||||
gpr_mu_unlock(&fd_freelist_mu); |
|
||||||
|
|
||||||
if (fd_info) { |
|
||||||
destroy_wfd(fd_info); |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
void grpc_pollset_kick_init(grpc_pollset_kick_state *kick_state) { |
|
||||||
gpr_mu_init(&kick_state->mu); |
|
||||||
kick_state->kicked = 0; |
|
||||||
kick_state->fd_list.next = kick_state->fd_list.prev = &kick_state->fd_list; |
|
||||||
} |
|
||||||
|
|
||||||
void grpc_pollset_kick_destroy(grpc_pollset_kick_state *kick_state) { |
|
||||||
gpr_mu_destroy(&kick_state->mu); |
|
||||||
GPR_ASSERT(kick_state->fd_list.next == &kick_state->fd_list); |
|
||||||
} |
|
||||||
|
|
||||||
grpc_kick_fd_info *grpc_pollset_kick_pre_poll( |
|
||||||
grpc_pollset_kick_state *kick_state) { |
|
||||||
grpc_kick_fd_info *fd_info; |
|
||||||
gpr_mu_lock(&kick_state->mu); |
|
||||||
if (kick_state->kicked) { |
|
||||||
kick_state->kicked = 0; |
|
||||||
gpr_mu_unlock(&kick_state->mu); |
|
||||||
return NULL; |
|
||||||
} |
|
||||||
fd_info = allocate_wfd(); |
|
||||||
fd_info->next = &kick_state->fd_list; |
|
||||||
fd_info->prev = fd_info->next->prev; |
|
||||||
fd_info->next->prev = fd_info->prev->next = fd_info; |
|
||||||
gpr_mu_unlock(&kick_state->mu); |
|
||||||
return fd_info; |
|
||||||
} |
|
||||||
|
|
||||||
void grpc_pollset_kick_consume(grpc_pollset_kick_state *kick_state, |
|
||||||
grpc_kick_fd_info *fd_info) { |
|
||||||
grpc_wakeup_fd_consume_wakeup(&fd_info->wakeup_fd); |
|
||||||
} |
|
||||||
|
|
||||||
void grpc_pollset_kick_post_poll(grpc_pollset_kick_state *kick_state, |
|
||||||
grpc_kick_fd_info *fd_info) { |
|
||||||
gpr_mu_lock(&kick_state->mu); |
|
||||||
fd_info->next->prev = fd_info->prev; |
|
||||||
fd_info->prev->next = fd_info->next; |
|
||||||
free_wfd(fd_info); |
|
||||||
gpr_mu_unlock(&kick_state->mu); |
|
||||||
} |
|
||||||
|
|
||||||
void grpc_pollset_kick_kick(grpc_pollset_kick_state *kick_state) { |
|
||||||
gpr_mu_lock(&kick_state->mu); |
|
||||||
if (kick_state->fd_list.next != &kick_state->fd_list) { |
|
||||||
grpc_wakeup_fd_wakeup(&kick_state->fd_list.next->wakeup_fd); |
|
||||||
} else { |
|
||||||
kick_state->kicked = 1; |
|
||||||
} |
|
||||||
gpr_mu_unlock(&kick_state->mu); |
|
||||||
} |
|
||||||
|
|
||||||
void grpc_pollset_kick_global_init_fallback_fd(void) { |
|
||||||
gpr_mu_init(&fd_freelist_mu); |
|
||||||
grpc_wakeup_fd_global_init_force_fallback(); |
|
||||||
} |
|
||||||
|
|
||||||
void grpc_pollset_kick_global_init(void) { |
|
||||||
gpr_mu_init(&fd_freelist_mu); |
|
||||||
grpc_wakeup_fd_global_init(); |
|
||||||
} |
|
||||||
|
|
||||||
void grpc_pollset_kick_global_destroy(void) { |
|
||||||
while (fd_freelist != NULL) { |
|
||||||
grpc_kick_fd_info *current = fd_freelist; |
|
||||||
fd_freelist = fd_freelist->next; |
|
||||||
destroy_wfd(current); |
|
||||||
} |
|
||||||
grpc_wakeup_fd_global_destroy(); |
|
||||||
gpr_mu_destroy(&fd_freelist_mu); |
|
||||||
} |
|
||||||
|
|
||||||
#endif /* GPR_POSIX_SOCKET */ |
|
@ -1,93 +0,0 @@ |
|||||||
/*
|
|
||||||
* |
|
||||||
* Copyright 2015, Google Inc. |
|
||||||
* All rights reserved. |
|
||||||
* |
|
||||||
* Redistribution and use in source and binary forms, with or without |
|
||||||
* modification, are permitted provided that the following conditions are |
|
||||||
* met: |
|
||||||
* |
|
||||||
* * Redistributions of source code must retain the above copyright |
|
||||||
* notice, this list of conditions and the following disclaimer. |
|
||||||
* * Redistributions in binary form must reproduce the above |
|
||||||
* copyright notice, this list of conditions and the following disclaimer |
|
||||||
* in the documentation and/or other materials provided with the |
|
||||||
* distribution. |
|
||||||
* * Neither the name of Google Inc. nor the names of its |
|
||||||
* contributors may be used to endorse or promote products derived from |
|
||||||
* this software without specific prior written permission. |
|
||||||
* |
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
||||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
||||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
||||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
||||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
||||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
||||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
||||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
||||||
* |
|
||||||
*/ |
|
||||||
|
|
||||||
#ifndef GRPC_INTERNAL_CORE_IOMGR_POLLSET_KICK_POSIX_H |
|
||||||
#define GRPC_INTERNAL_CORE_IOMGR_POLLSET_KICK_POSIX_H |
|
||||||
|
|
||||||
#include "src/core/iomgr/wakeup_fd_posix.h" |
|
||||||
#include <grpc/support/sync.h> |
|
||||||
|
|
||||||
/* pollset kicking allows breaking a thread out of polling work for
|
|
||||||
a given pollset. |
|
||||||
writing a byte to a pipe is used as a posix-ly portable base |
|
||||||
mechanism, and eventfds are utilized on Linux for better performance. */ |
|
||||||
|
|
||||||
typedef struct grpc_kick_fd_info { |
|
||||||
grpc_wakeup_fd_info wakeup_fd; |
|
||||||
/* used for polling list and free list */ |
|
||||||
struct grpc_kick_fd_info *next; |
|
||||||
/* only used when polling */ |
|
||||||
struct grpc_kick_fd_info *prev; |
|
||||||
} grpc_kick_fd_info; |
|
||||||
|
|
||||||
typedef struct grpc_pollset_kick_state { |
|
||||||
gpr_mu mu; |
|
||||||
int kicked; |
|
||||||
struct grpc_kick_fd_info fd_list; |
|
||||||
} grpc_pollset_kick_state; |
|
||||||
|
|
||||||
#define GRPC_POLLSET_KICK_GET_FD(kick_fd_info) \ |
|
||||||
GRPC_WAKEUP_FD_GET_READ_FD(&(kick_fd_info)->wakeup_fd) |
|
||||||
|
|
||||||
/* This is an abstraction around the typical pipe mechanism for waking up a
|
|
||||||
thread sitting in a poll() style call. */ |
|
||||||
|
|
||||||
void grpc_pollset_kick_global_init(void); |
|
||||||
void grpc_pollset_kick_global_destroy(void); |
|
||||||
|
|
||||||
void grpc_pollset_kick_init(grpc_pollset_kick_state *kick_state); |
|
||||||
void grpc_pollset_kick_destroy(grpc_pollset_kick_state *kick_state); |
|
||||||
|
|
||||||
/* Guarantees a pure posix implementation rather than a specialized one, if
|
|
||||||
* applicable. Intended for testing. */ |
|
||||||
void grpc_pollset_kick_global_init_fallback_fd(void); |
|
||||||
|
|
||||||
/* Must be called before entering poll(). If return value is NULL, this consumed
|
|
||||||
an existing kick. Otherwise the return value is an FD to add to the poll set. |
|
||||||
*/ |
|
||||||
grpc_kick_fd_info *grpc_pollset_kick_pre_poll( |
|
||||||
grpc_pollset_kick_state *kick_state); |
|
||||||
|
|
||||||
/* Consume an existing kick. Must be called after poll returns that the fd was
|
|
||||||
readable, and before calling kick_post_poll. */ |
|
||||||
void grpc_pollset_kick_consume(grpc_pollset_kick_state *kick_state, |
|
||||||
grpc_kick_fd_info *fd_info); |
|
||||||
|
|
||||||
/* Must be called after pre_poll, and after consume if applicable */ |
|
||||||
void grpc_pollset_kick_post_poll(grpc_pollset_kick_state *kick_state, |
|
||||||
grpc_kick_fd_info *fd_info); |
|
||||||
|
|
||||||
/* Actually kick */ |
|
||||||
void grpc_pollset_kick_kick(grpc_pollset_kick_state *kick_state); |
|
||||||
|
|
||||||
#endif /* GRPC_INTERNAL_CORE_IOMGR_POLLSET_KICK_POSIX_H */ |
|
@ -1,130 +0,0 @@ |
|||||||
/*
|
|
||||||
* |
|
||||||
* Copyright 2015, Google Inc. |
|
||||||
* All rights reserved. |
|
||||||
* |
|
||||||
* Redistribution and use in source and binary forms, with or without |
|
||||||
* modification, are permitted provided that the following conditions are |
|
||||||
* met: |
|
||||||
* |
|
||||||
* * Redistributions of source code must retain the above copyright |
|
||||||
* notice, this list of conditions and the following disclaimer. |
|
||||||
* * Redistributions in binary form must reproduce the above |
|
||||||
* copyright notice, this list of conditions and the following disclaimer |
|
||||||
* in the documentation and/or other materials provided with the |
|
||||||
* distribution. |
|
||||||
* * Neither the name of Google Inc. nor the names of its |
|
||||||
* contributors may be used to endorse or promote products derived from |
|
||||||
* this software without specific prior written permission. |
|
||||||
* |
|
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
||||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
||||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
||||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
||||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
||||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
||||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
||||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
||||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
||||||
* |
|
||||||
*/ |
|
||||||
|
|
||||||
#include "src/core/iomgr/pollset_kick_posix.h" |
|
||||||
|
|
||||||
#include <grpc/support/alloc.h> |
|
||||||
#include <grpc/support/log.h> |
|
||||||
#include "test/core/util/test_config.h" |
|
||||||
|
|
||||||
static void test_allocation(void) { |
|
||||||
grpc_pollset_kick_state state; |
|
||||||
grpc_pollset_kick_init(&state); |
|
||||||
grpc_pollset_kick_destroy(&state); |
|
||||||
} |
|
||||||
|
|
||||||
static void test_non_kick(void) { |
|
||||||
grpc_pollset_kick_state state; |
|
||||||
grpc_kick_fd_info *kfd; |
|
||||||
|
|
||||||
grpc_pollset_kick_init(&state); |
|
||||||
kfd = grpc_pollset_kick_pre_poll(&state); |
|
||||||
GPR_ASSERT(kfd != NULL); |
|
||||||
|
|
||||||
grpc_pollset_kick_post_poll(&state, kfd); |
|
||||||
grpc_pollset_kick_destroy(&state); |
|
||||||
} |
|
||||||
|
|
||||||
static void test_basic_kick(void) { |
|
||||||
/* Kicked during poll */ |
|
||||||
grpc_pollset_kick_state state; |
|
||||||
grpc_kick_fd_info *kfd; |
|
||||||
grpc_pollset_kick_init(&state); |
|
||||||
|
|
||||||
kfd = grpc_pollset_kick_pre_poll(&state); |
|
||||||
GPR_ASSERT(kfd != NULL); |
|
||||||
|
|
||||||
grpc_pollset_kick_kick(&state); |
|
||||||
|
|
||||||
/* Now hypothetically we polled and found that we were kicked */ |
|
||||||
grpc_pollset_kick_consume(&state, kfd); |
|
||||||
|
|
||||||
grpc_pollset_kick_post_poll(&state, kfd); |
|
||||||
|
|
||||||
grpc_pollset_kick_destroy(&state); |
|
||||||
} |
|
||||||
|
|
||||||
static void test_non_poll_kick(void) { |
|
||||||
/* Kick before entering poll */ |
|
||||||
grpc_pollset_kick_state state; |
|
||||||
grpc_kick_fd_info *kfd; |
|
||||||
|
|
||||||
grpc_pollset_kick_init(&state); |
|
||||||
|
|
||||||
grpc_pollset_kick_kick(&state); |
|
||||||
kfd = grpc_pollset_kick_pre_poll(&state); |
|
||||||
GPR_ASSERT(kfd == NULL); |
|
||||||
grpc_pollset_kick_destroy(&state); |
|
||||||
} |
|
||||||
|
|
||||||
#define GRPC_MAX_CACHED_PIPES 50 |
|
||||||
|
|
||||||
static void test_over_free(void) { |
|
||||||
/* Check high watermark pipe free logic */ |
|
||||||
int i; |
|
||||||
grpc_kick_fd_info **kfds = |
|
||||||
gpr_malloc(sizeof(grpc_kick_fd_info *) * GRPC_MAX_CACHED_PIPES); |
|
||||||
grpc_pollset_kick_state state; |
|
||||||
grpc_pollset_kick_init(&state); |
|
||||||
for (i = 0; i < GRPC_MAX_CACHED_PIPES; ++i) { |
|
||||||
kfds[i] = grpc_pollset_kick_pre_poll(&state); |
|
||||||
GPR_ASSERT(kfds[i] != NULL); |
|
||||||
} |
|
||||||
|
|
||||||
for (i = 0; i < GRPC_MAX_CACHED_PIPES; ++i) { |
|
||||||
grpc_pollset_kick_post_poll(&state, kfds[i]); |
|
||||||
} |
|
||||||
grpc_pollset_kick_destroy(&state); |
|
||||||
gpr_free(kfds); |
|
||||||
} |
|
||||||
|
|
||||||
static void run_tests(void) { |
|
||||||
test_allocation(); |
|
||||||
test_basic_kick(); |
|
||||||
test_non_poll_kick(); |
|
||||||
test_non_kick(); |
|
||||||
test_over_free(); |
|
||||||
} |
|
||||||
|
|
||||||
int main(int argc, char **argv) { |
|
||||||
grpc_test_init(argc, argv); |
|
||||||
|
|
||||||
grpc_pollset_kick_global_init(); |
|
||||||
run_tests(); |
|
||||||
grpc_pollset_kick_global_destroy(); |
|
||||||
|
|
||||||
grpc_pollset_kick_global_init_fallback_fd(); |
|
||||||
run_tests(); |
|
||||||
grpc_pollset_kick_global_destroy(); |
|
||||||
return 0; |
|
||||||
} |
|
@ -0,0 +1,160 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "test/core/util/reconnect_server.h" |
||||||
|
|
||||||
|
#include <arpa/inet.h> |
||||||
|
#include <grpc/grpc.h> |
||||||
|
#include <grpc/support/alloc.h> |
||||||
|
#include <grpc/support/host_port.h> |
||||||
|
#include <grpc/support/log.h> |
||||||
|
#include <grpc/support/sync.h> |
||||||
|
#include <grpc/support/time.h> |
||||||
|
#include <string.h> |
||||||
|
#include "src/core/iomgr/endpoint.h" |
||||||
|
#include "src/core/iomgr/tcp_server.h" |
||||||
|
#include "test/core/util/port.h" |
||||||
|
|
||||||
|
static void pretty_print_backoffs(reconnect_server *server) { |
||||||
|
gpr_timespec diff; |
||||||
|
int i = 1; |
||||||
|
double expected_backoff = 1000.0, backoff; |
||||||
|
timestamp_list *head = server->head; |
||||||
|
gpr_log(GPR_INFO, "reconnect server: new connection"); |
||||||
|
for (head = server->head; head && head->next; head = head->next, i++) { |
||||||
|
diff = gpr_time_sub(head->next->timestamp, head->timestamp); |
||||||
|
backoff = gpr_time_to_millis(diff); |
||||||
|
gpr_log(GPR_INFO, |
||||||
|
"retry %2d:backoff %6.2fs,expected backoff %6.2fs, jitter %4.2f%%", |
||||||
|
i, backoff / 1000.0, expected_backoff / 1000.0, |
||||||
|
(backoff - expected_backoff) * 100.0 / expected_backoff); |
||||||
|
expected_backoff *= 1.6; |
||||||
|
if (expected_backoff > 120 * 1000) { |
||||||
|
expected_backoff = 120 * 1000; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
static void on_connect(void *arg, grpc_endpoint *tcp) { |
||||||
|
char *peer; |
||||||
|
char *last_colon; |
||||||
|
reconnect_server *server = (reconnect_server *)arg; |
||||||
|
gpr_timespec now = gpr_now(GPR_CLOCK_REALTIME); |
||||||
|
timestamp_list *new_tail; |
||||||
|
peer = grpc_endpoint_get_peer(tcp); |
||||||
|
grpc_endpoint_shutdown(tcp); |
||||||
|
grpc_endpoint_destroy(tcp); |
||||||
|
if (peer) { |
||||||
|
last_colon = strrchr(peer, ':'); |
||||||
|
if (server->peer == NULL) { |
||||||
|
server->peer = peer; |
||||||
|
} else { |
||||||
|
if (last_colon == NULL) { |
||||||
|
gpr_log(GPR_ERROR, "peer does not contain a ':'"); |
||||||
|
} else if (strncmp(server->peer, peer, last_colon - peer) != 0) { |
||||||
|
gpr_log(GPR_ERROR, "mismatched peer! %s vs %s", server->peer, peer); |
||||||
|
} |
||||||
|
gpr_free(peer); |
||||||
|
} |
||||||
|
} |
||||||
|
new_tail = gpr_malloc(sizeof(timestamp_list)); |
||||||
|
new_tail->timestamp = now; |
||||||
|
new_tail->next = NULL; |
||||||
|
if (server->tail == NULL) { |
||||||
|
server->head = new_tail; |
||||||
|
server->tail = new_tail; |
||||||
|
} else { |
||||||
|
server->tail->next = new_tail; |
||||||
|
server->tail = new_tail; |
||||||
|
} |
||||||
|
pretty_print_backoffs(server); |
||||||
|
} |
||||||
|
|
||||||
|
void reconnect_server_init(reconnect_server *server) { |
||||||
|
grpc_init(); |
||||||
|
server->tcp_server = NULL; |
||||||
|
grpc_pollset_init(&server->pollset); |
||||||
|
server->pollsets[0] = &server->pollset; |
||||||
|
server->head = NULL; |
||||||
|
server->tail = NULL; |
||||||
|
server->peer = NULL; |
||||||
|
} |
||||||
|
|
||||||
|
void reconnect_server_start(reconnect_server *server, int port) { |
||||||
|
struct sockaddr_in addr; |
||||||
|
int port_added; |
||||||
|
|
||||||
|
addr.sin_family = AF_INET; |
||||||
|
addr.sin_port = htons(port); |
||||||
|
inet_pton(AF_INET, "0.0.0.0", &addr.sin_addr); |
||||||
|
|
||||||
|
server->tcp_server = grpc_tcp_server_create(); |
||||||
|
port_added = |
||||||
|
grpc_tcp_server_add_port(server->tcp_server, &addr, sizeof(addr)); |
||||||
|
GPR_ASSERT(port_added == port); |
||||||
|
|
||||||
|
grpc_tcp_server_start(server->tcp_server, server->pollsets, 1, on_connect, |
||||||
|
server); |
||||||
|
gpr_log(GPR_INFO, "reconnect tcp server listening on 0.0.0.0:%d", port); |
||||||
|
} |
||||||
|
|
||||||
|
void reconnect_server_poll(reconnect_server *server, int seconds) { |
||||||
|
gpr_timespec deadline = |
||||||
|
gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
||||||
|
gpr_time_from_seconds(seconds, GPR_TIMESPAN)); |
||||||
|
gpr_mu_lock(GRPC_POLLSET_MU(&server->pollset)); |
||||||
|
grpc_pollset_work(&server->pollset, deadline); |
||||||
|
gpr_mu_unlock(GRPC_POLLSET_MU(&server->pollset)); |
||||||
|
} |
||||||
|
|
||||||
|
void reconnect_server_clear_timestamps(reconnect_server *server) { |
||||||
|
timestamp_list *new_head = server->head; |
||||||
|
while (server->head) { |
||||||
|
new_head = server->head->next; |
||||||
|
gpr_free(server->head); |
||||||
|
server->head = new_head; |
||||||
|
} |
||||||
|
server->tail = NULL; |
||||||
|
gpr_free(server->peer); |
||||||
|
server->peer = NULL; |
||||||
|
} |
||||||
|
|
||||||
|
static void do_nothing(void *ignored) {} |
||||||
|
|
||||||
|
void reconnect_server_destroy(reconnect_server *server) { |
||||||
|
grpc_tcp_server_destroy(server->tcp_server, do_nothing, NULL); |
||||||
|
reconnect_server_clear_timestamps(server); |
||||||
|
grpc_pollset_shutdown(&server->pollset, do_nothing, NULL); |
||||||
|
grpc_pollset_destroy(&server->pollset); |
||||||
|
grpc_shutdown(); |
||||||
|
} |
@ -0,0 +1,69 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#ifndef GRPC_TEST_CORE_UTIL_RECONNECT_SERVER_H |
||||||
|
#define GRPC_TEST_CORE_UTIL_RECONNECT_SERVER_H |
||||||
|
|
||||||
|
#include <grpc/support/sync.h> |
||||||
|
#include <grpc/support/time.h> |
||||||
|
#include "src/core/iomgr/tcp_server.h" |
||||||
|
|
||||||
|
#ifdef __cplusplus |
||||||
|
extern "C" { |
||||||
|
#endif |
||||||
|
|
||||||
|
typedef struct timestamp_list { |
||||||
|
gpr_timespec timestamp; |
||||||
|
struct timestamp_list *next; |
||||||
|
} timestamp_list; |
||||||
|
|
||||||
|
typedef struct reconnect_server { |
||||||
|
grpc_tcp_server *tcp_server; |
||||||
|
grpc_pollset pollset; |
||||||
|
grpc_pollset *pollsets[1]; |
||||||
|
timestamp_list *head; |
||||||
|
timestamp_list *tail; |
||||||
|
char *peer; |
||||||
|
} reconnect_server; |
||||||
|
|
||||||
|
void reconnect_server_init(reconnect_server *server); |
||||||
|
void reconnect_server_start(reconnect_server *server, int port); |
||||||
|
void reconnect_server_poll(reconnect_server *server, int seconds); |
||||||
|
void reconnect_server_destroy(reconnect_server *server); |
||||||
|
void reconnect_server_clear_timestamps(reconnect_server *server); |
||||||
|
|
||||||
|
#ifdef __cplusplus |
||||||
|
} |
||||||
|
#endif |
||||||
|
|
||||||
|
#endif /* GRPC_TEST_CORE_UTIL_RECONNECT_SERVER_H */ |
@ -0,0 +1,103 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <memory> |
||||||
|
#include <sstream> |
||||||
|
|
||||||
|
#include <grpc/grpc.h> |
||||||
|
#include <grpc/support/log.h> |
||||||
|
#include <gflags/gflags.h> |
||||||
|
#include <grpc++/channel_interface.h> |
||||||
|
#include <grpc++/client_context.h> |
||||||
|
#include <grpc++/status.h> |
||||||
|
#include "test/cpp/util/create_test_channel.h" |
||||||
|
#include "test/cpp/util/test_config.h" |
||||||
|
#include "test/proto/test.grpc.pb.h" |
||||||
|
#include "test/proto/empty.grpc.pb.h" |
||||||
|
#include "test/proto/messages.grpc.pb.h" |
||||||
|
|
||||||
|
DEFINE_int32(server_control_port, 0, "Server port for control rpcs."); |
||||||
|
DEFINE_int32(server_retry_port, 0, "Server port for testing reconnection."); |
||||||
|
DEFINE_string(server_host, "127.0.0.1", "Server host to connect to"); |
||||||
|
|
||||||
|
using grpc::ChannelInterface; |
||||||
|
using grpc::ClientContext; |
||||||
|
using grpc::CreateTestChannel; |
||||||
|
using grpc::Status; |
||||||
|
using grpc::testing::Empty; |
||||||
|
using grpc::testing::ReconnectInfo; |
||||||
|
using grpc::testing::ReconnectService; |
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
grpc::testing::InitTest(&argc, &argv, true); |
||||||
|
GPR_ASSERT(FLAGS_server_control_port); |
||||||
|
GPR_ASSERT(FLAGS_server_retry_port); |
||||||
|
|
||||||
|
std::ostringstream server_address; |
||||||
|
server_address << FLAGS_server_host << ':' << FLAGS_server_control_port; |
||||||
|
std::unique_ptr<ReconnectService::Stub> control_stub( |
||||||
|
ReconnectService::NewStub( |
||||||
|
CreateTestChannel(server_address.str(), false))); |
||||||
|
ClientContext start_context; |
||||||
|
Empty empty_request; |
||||||
|
Empty empty_response; |
||||||
|
Status start_status = |
||||||
|
control_stub->Start(&start_context, empty_request, &empty_response); |
||||||
|
GPR_ASSERT(start_status.ok()); |
||||||
|
|
||||||
|
gpr_log(GPR_INFO, "Starting connections with retries."); |
||||||
|
server_address.str(""); |
||||||
|
server_address << FLAGS_server_host << ':' << FLAGS_server_retry_port; |
||||||
|
std::shared_ptr<ChannelInterface> retry_channel = |
||||||
|
CreateTestChannel(server_address.str(), true); |
||||||
|
// About 13 retries.
|
||||||
|
const int kDeadlineSeconds = 540; |
||||||
|
// Use any rpc to test retry.
|
||||||
|
std::unique_ptr<ReconnectService::Stub> retry_stub( |
||||||
|
ReconnectService::NewStub(retry_channel)); |
||||||
|
ClientContext retry_context; |
||||||
|
retry_context.set_deadline(std::chrono::system_clock::now() + |
||||||
|
std::chrono::seconds(kDeadlineSeconds)); |
||||||
|
Status retry_status = |
||||||
|
retry_stub->Start(&retry_context, empty_request, &empty_response); |
||||||
|
GPR_ASSERT(retry_status.error_code() == grpc::StatusCode::DEADLINE_EXCEEDED); |
||||||
|
gpr_log(GPR_INFO, "Done retrying, getting final data from server"); |
||||||
|
|
||||||
|
ClientContext stop_context; |
||||||
|
ReconnectInfo response; |
||||||
|
Status stop_status = |
||||||
|
control_stub->Stop(&stop_context, empty_request, &response); |
||||||
|
GPR_ASSERT(stop_status.ok()); |
||||||
|
GPR_ASSERT(response.passed() == true); |
||||||
|
return 0; |
||||||
|
} |
@ -0,0 +1,190 @@ |
|||||||
|
/*
|
||||||
|
* |
||||||
|
* Copyright 2015, Google Inc. |
||||||
|
* All rights reserved. |
||||||
|
* |
||||||
|
* Redistribution and use in source and binary forms, with or without |
||||||
|
* modification, are permitted provided that the following conditions are |
||||||
|
* met: |
||||||
|
* |
||||||
|
* * Redistributions of source code must retain the above copyright |
||||||
|
* notice, this list of conditions and the following disclaimer. |
||||||
|
* * Redistributions in binary form must reproduce the above |
||||||
|
* copyright notice, this list of conditions and the following disclaimer |
||||||
|
* in the documentation and/or other materials provided with the |
||||||
|
* distribution. |
||||||
|
* * Neither the name of Google Inc. nor the names of its |
||||||
|
* contributors may be used to endorse or promote products derived from |
||||||
|
* this software without specific prior written permission. |
||||||
|
* |
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||||
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||||
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||||
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||||
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||||
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||||
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||||
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
|
* |
||||||
|
*/ |
||||||
|
|
||||||
|
#include <condition_variable> |
||||||
|
#include <memory> |
||||||
|
#include <mutex> |
||||||
|
#include <sstream> |
||||||
|
|
||||||
|
#include <signal.h> |
||||||
|
#include <unistd.h> |
||||||
|
|
||||||
|
#include <gflags/gflags.h> |
||||||
|
#include <grpc/grpc.h> |
||||||
|
#include <grpc/support/log.h> |
||||||
|
#include <grpc++/config.h> |
||||||
|
#include <grpc++/server.h> |
||||||
|
#include <grpc++/server_builder.h> |
||||||
|
#include <grpc++/server_context.h> |
||||||
|
#include <grpc++/server_credentials.h> |
||||||
|
#include <grpc++/status.h> |
||||||
|
#include "test/core/util/reconnect_server.h" |
||||||
|
#include "test/cpp/util/test_config.h" |
||||||
|
#include "test/proto/test.grpc.pb.h" |
||||||
|
#include "test/proto/empty.grpc.pb.h" |
||||||
|
#include "test/proto/messages.grpc.pb.h" |
||||||
|
|
||||||
|
DEFINE_int32(control_port, 0, "Server port for controlling the server."); |
||||||
|
DEFINE_int32(retry_port, 0, |
||||||
|
"Server port for raw tcp connections. All incoming " |
||||||
|
"connections will be closed immediately."); |
||||||
|
|
||||||
|
using grpc::Server; |
||||||
|
using grpc::ServerBuilder; |
||||||
|
using grpc::ServerContext; |
||||||
|
using grpc::ServerCredentials; |
||||||
|
using grpc::ServerReader; |
||||||
|
using grpc::ServerReaderWriter; |
||||||
|
using grpc::ServerWriter; |
||||||
|
using grpc::SslServerCredentialsOptions; |
||||||
|
using grpc::Status; |
||||||
|
using grpc::testing::Empty; |
||||||
|
using grpc::testing::ReconnectService; |
||||||
|
using grpc::testing::ReconnectInfo; |
||||||
|
|
||||||
|
static bool got_sigint = false; |
||||||
|
|
||||||
|
class ReconnectServiceImpl : public ReconnectService::Service { |
||||||
|
public: |
||||||
|
explicit ReconnectServiceImpl(int retry_port) |
||||||
|
: retry_port_(retry_port), serving_(false), shutdown_(false) { |
||||||
|
reconnect_server_init(&tcp_server_); |
||||||
|
} |
||||||
|
|
||||||
|
~ReconnectServiceImpl() { |
||||||
|
if (tcp_server_.tcp_server) { |
||||||
|
reconnect_server_destroy(&tcp_server_); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void Poll(int seconds) { reconnect_server_poll(&tcp_server_, seconds); } |
||||||
|
|
||||||
|
Status Start(ServerContext* context, const Empty* request, Empty* response) { |
||||||
|
std::unique_lock<std::mutex> lock(mu_); |
||||||
|
while (serving_ && !shutdown_) { |
||||||
|
cv_.wait(lock); |
||||||
|
} |
||||||
|
if (shutdown_) { |
||||||
|
return Status(grpc::StatusCode::UNAVAILABLE, "shutting down"); |
||||||
|
} |
||||||
|
serving_ = true; |
||||||
|
lock.unlock(); |
||||||
|
|
||||||
|
if (!tcp_server_.tcp_server) { |
||||||
|
reconnect_server_start(&tcp_server_, retry_port_); |
||||||
|
} else { |
||||||
|
reconnect_server_clear_timestamps(&tcp_server_); |
||||||
|
} |
||||||
|
return Status::OK; |
||||||
|
} |
||||||
|
|
||||||
|
Status Stop(ServerContext* context, const Empty* request, |
||||||
|
ReconnectInfo* response) { |
||||||
|
// extract timestamps and set response
|
||||||
|
Verify(response); |
||||||
|
reconnect_server_clear_timestamps(&tcp_server_); |
||||||
|
std::lock_guard<std::mutex> lock(mu_); |
||||||
|
serving_ = false; |
||||||
|
cv_.notify_one(); |
||||||
|
return Status::OK; |
||||||
|
} |
||||||
|
|
||||||
|
void Verify(ReconnectInfo* response) { |
||||||
|
double expected_backoff = 1000.0; |
||||||
|
const double kTransmissionDelay = 100.0; |
||||||
|
const double kBackoffMultiplier = 1.6; |
||||||
|
const double kJitterFactor = 0.2; |
||||||
|
const int kMaxBackoffMs = 120 * 1000; |
||||||
|
bool passed = true; |
||||||
|
for (timestamp_list* cur = tcp_server_.head; cur && cur->next; |
||||||
|
cur = cur->next) { |
||||||
|
double backoff = gpr_time_to_millis( |
||||||
|
gpr_time_sub(cur->next->timestamp, cur->timestamp)); |
||||||
|
double min_backoff = expected_backoff * (1 - kJitterFactor); |
||||||
|
double max_backoff = expected_backoff * (1 + kJitterFactor); |
||||||
|
if (backoff < min_backoff - kTransmissionDelay || |
||||||
|
backoff > max_backoff + kTransmissionDelay) { |
||||||
|
passed = false; |
||||||
|
} |
||||||
|
response->add_backoff_ms(static_cast<gpr_int32>(backoff)); |
||||||
|
expected_backoff *= kBackoffMultiplier; |
||||||
|
expected_backoff = |
||||||
|
expected_backoff > kMaxBackoffMs ? kMaxBackoffMs : expected_backoff; |
||||||
|
} |
||||||
|
response->set_passed(passed); |
||||||
|
} |
||||||
|
|
||||||
|
void Shutdown() { |
||||||
|
std::lock_guard<std::mutex> lock(mu_); |
||||||
|
shutdown_ = true; |
||||||
|
cv_.notify_all(); |
||||||
|
} |
||||||
|
|
||||||
|
private: |
||||||
|
int retry_port_; |
||||||
|
reconnect_server tcp_server_; |
||||||
|
bool serving_; |
||||||
|
bool shutdown_; |
||||||
|
std::mutex mu_; |
||||||
|
std::condition_variable cv_; |
||||||
|
}; |
||||||
|
|
||||||
|
void RunServer() { |
||||||
|
std::ostringstream server_address; |
||||||
|
server_address << "0.0.0.0:" << FLAGS_control_port; |
||||||
|
ReconnectServiceImpl service(FLAGS_retry_port); |
||||||
|
|
||||||
|
ServerBuilder builder; |
||||||
|
builder.RegisterService(&service); |
||||||
|
builder.AddListeningPort(server_address.str(), |
||||||
|
grpc::InsecureServerCredentials()); |
||||||
|
std::unique_ptr<Server> server(builder.BuildAndStart()); |
||||||
|
gpr_log(GPR_INFO, "Server listening on %s", server_address.str().c_str()); |
||||||
|
while (!got_sigint) { |
||||||
|
service.Poll(5); |
||||||
|
} |
||||||
|
service.Shutdown(); |
||||||
|
} |
||||||
|
|
||||||
|
static void sigint_handler(int x) { got_sigint = true; } |
||||||
|
|
||||||
|
int main(int argc, char** argv) { |
||||||
|
grpc::testing::InitTest(&argc, &argv, true); |
||||||
|
signal(SIGINT, sigint_handler); |
||||||
|
|
||||||
|
GPR_ASSERT(FLAGS_control_port != 0); |
||||||
|
GPR_ASSERT(FLAGS_retry_port != 0); |
||||||
|
RunServer(); |
||||||
|
|
||||||
|
return 0; |
||||||
|
} |
File diff suppressed because one or more lines are too long
Loading…
Reference in new issue