jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 1 | // Copyright (c) 2006- Facebook |
| 2 | // Distributed under the Thrift Software License |
| 3 | // |
| 4 | // See accompanying file LICENSE or visit the Thrift site at: |
| 5 | // http://developers.facebook.com/thrift/ |
| 6 | |
| 7 | #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_ |
| 8 | #define _THRIFT_TRANSPORT_TSOCKETPOOL_H_ 1 |
| 9 | |
| 10 | #include <vector> |
| 11 | #include "TSocket.h" |
| 12 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 13 | namespace apache { namespace thrift { namespace transport { |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 14 | |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 15 | /** |
| 16 | * Class to hold server information for TSocketPool |
| 17 | * |
| 18 | * @author Akhil Wable <akhil@facebook.com> |
| 19 | */ |
| 20 | class TSocketPoolServer { |
| 21 | |
| 22 | public: |
| 23 | /** |
| 24 | * Default constructor for server info |
| 25 | */ |
| 26 | TSocketPoolServer(); |
| 27 | |
| 28 | /** |
| 29 | * Constructor for TSocketPool server |
| 30 | */ |
| 31 | TSocketPoolServer(const std::string &host, int port); |
| 32 | |
| 33 | // Host name |
| 34 | std::string host_; |
| 35 | |
| 36 | // Port to connect on |
| 37 | int port_; |
| 38 | |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 39 | // Socket for the server |
| 40 | int socket_; |
| 41 | |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 42 | // Last time connecting to this server failed |
| 43 | int lastFailTime_; |
| 44 | |
| 45 | // Number of consecutive times connecting to this server failed |
| 46 | int consecutiveFailures_; |
| 47 | }; |
| 48 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 49 | /** |
| 50 | * TCP Socket implementation of the TTransport interface. |
| 51 | * |
| 52 | * @author Mark Slee <mcslee@facebook.com> |
| 53 | */ |
| 54 | class TSocketPool : public TSocket { |
| 55 | |
| 56 | public: |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Socket pool constructor |
| 60 | */ |
| 61 | TSocketPool(); |
| 62 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Socket pool constructor |
| 65 | * |
| 66 | * @param hosts list of host names |
| 67 | * @param ports list of port names |
| 68 | */ |
| 69 | TSocketPool(const std::vector<std::string> &hosts, |
| 70 | const std::vector<int> &ports); |
| 71 | |
| 72 | /** |
| 73 | * Socket pool constructor |
| 74 | * |
| 75 | * @param servers list of pairs of host name and port |
| 76 | */ |
David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 77 | TSocketPool(const std::vector<std::pair<std::string, int> >& servers); |
| 78 | |
| 79 | /** |
| 80 | * Socket pool constructor |
| 81 | * |
| 82 | * @param servers list of TSocketPoolServers |
| 83 | */ |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 84 | TSocketPool(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 85 | |
| 86 | /** |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 87 | * Socket pool constructor |
| 88 | * |
| 89 | * @param host single host |
| 90 | * @param port single port |
| 91 | */ |
| 92 | TSocketPool(const std::string& host, int port); |
| 93 | |
| 94 | /** |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 95 | * Destroyes the socket object, closing it if necessary. |
| 96 | */ |
| 97 | virtual ~TSocketPool(); |
| 98 | |
| 99 | /** |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 100 | * Add a server to the pool |
| 101 | */ |
| 102 | void addServer(const std::string& host, int port); |
| 103 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 104 | /** |
| 105 | * Set list of servers in this pool |
| 106 | */ |
| 107 | void setServers(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); |
David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Get list of servers in this pool |
| 111 | */ |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 112 | void getServers(std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); |
David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 113 | |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 114 | /** |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 115 | * Sets how many times to keep retrying a host in the connect function. |
| 116 | */ |
| 117 | void setNumRetries(int numRetries); |
| 118 | |
| 119 | /** |
| 120 | * Sets how long to wait until retrying a host if it was marked down |
| 121 | */ |
| 122 | void setRetryInterval(int retryInterval); |
| 123 | |
| 124 | /** |
| 125 | * Sets how many times to keep retrying a host before marking it as down. |
| 126 | */ |
| 127 | void setMaxConsecutiveFailures(int maxConsecutiveFailures); |
| 128 | |
| 129 | /** |
| 130 | * Turns randomization in connect order on or off. |
| 131 | */ |
| 132 | void setRandomize(bool randomize); |
| 133 | |
| 134 | /** |
| 135 | * Whether to always try the last server. |
| 136 | */ |
| 137 | void setAlwaysTryLast(bool alwaysTryLast); |
| 138 | |
| 139 | /** |
| 140 | * Creates and opens the UNIX socket. |
| 141 | */ |
| 142 | void open(); |
| 143 | |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 144 | /* |
| 145 | * Closes the UNIX socket |
| 146 | */ |
| 147 | void close(); |
| 148 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 149 | protected: |
| 150 | |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 151 | void setCurrentServer(const boost::shared_ptr<TSocketPoolServer> &server); |
| 152 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 153 | /** List of servers to connect to */ |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 154 | std::vector< boost::shared_ptr<TSocketPoolServer> > servers_; |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 155 | |
David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 156 | /** Current server */ |
| 157 | boost::shared_ptr<TSocketPoolServer> currentServer_; |
| 158 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 159 | /** How many times to retry each host in connect */ |
| 160 | int numRetries_; |
| 161 | |
| 162 | /** Retry interval in seconds, how long to not try a host if it has been |
| 163 | * marked as down. |
| 164 | */ |
| 165 | int retryInterval_; |
| 166 | |
| 167 | /** Max consecutive failures before marking a host down. */ |
| 168 | int maxConsecutiveFailures_; |
| 169 | |
| 170 | /** Try hosts in order? or Randomized? */ |
| 171 | bool randomize_; |
| 172 | |
| 173 | /** Always try last host, even if marked down? */ |
| 174 | bool alwaysTryLast_; |
| 175 | }; |
| 176 | |
T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 177 | }}} // apache::thrift::transport |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 178 | |
| 179 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_ |
| 180 | |