017242bd9d8810d869866e88cfa484a08d347d50
[m6w6/libmemcached] / src / libmemcached / server.hpp
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #pragma once
17
18 #ifdef HAVE_SYS_TIME_H
19 # include <sys/time.h>
20 #endif
21
22 #include <cassert>
23
24 memcached_server_st *__server_create_with(memcached_st *memc, memcached_server_st *self,
25 const memcached_string_t &hostname, const in_port_t port,
26 uint32_t weight, const memcached_connection_t type);
27
28 memcached_return_t memcached_server_add_parsed(memcached_st *ptr, const char *hostname,
29 size_t hostname_length, in_port_t port,
30 uint32_t weight);
31
32 void __server_free(memcached_server_st *);
33
34 static inline bool memcached_is_valid_servername(const memcached_string_t &arg) {
35 return (arg.c_str != NULL or arg.size == 0) and arg.size < MEMCACHED_NI_MAXHOST;
36 }
37
38 static inline bool memcached_is_valid_filename(const memcached_string_t &arg) {
39 return arg.c_str != NULL and arg.size > 0 and arg.size < MEMCACHED_NI_MAXHOST;
40 }
41
42 void memcached_instance_free(memcached_instance_st *);
43
44 void set_last_disconnected_host(memcached_instance_st *self);
45
46 static inline void memcached_mark_server_for_timeout(memcached_instance_st *server) {
47 if (server->state != MEMCACHED_SERVER_STATE_IN_TIMEOUT) {
48 if (server->server_timeout_counter_query_id != server->root->query_id) {
49 server->server_timeout_counter++;
50 server->server_timeout_counter_query_id = server->root->query_id;
51 }
52
53 if (server->server_timeout_counter >= server->root->server_timeout_limit) {
54 struct timeval next_time;
55 if (gettimeofday(&next_time, NULL) == 0) {
56 server->next_retry = next_time.tv_sec + server->root->retry_timeout;
57 } else {
58 server->next_retry = 1; // Setting the value to 1 causes the timeout to occur immediately
59 }
60
61 server->state = MEMCACHED_SERVER_STATE_IN_TIMEOUT;
62 if (server->server_failure_counter_query_id != server->root->query_id) {
63 server->server_failure_counter++;
64 server->server_failure_counter_query_id = server->root->query_id;
65 }
66 set_last_disconnected_host(server);
67 }
68 }
69 }