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 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 13 | namespace facebook { 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 | |
| 39 | // Last time connecting to this server failed |
| 40 | int lastFailTime_; |
| 41 | |
| 42 | // Number of consecutive times connecting to this server failed |
| 43 | int consecutiveFailures_; |
| 44 | }; |
| 45 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 46 | /** |
| 47 | * TCP Socket implementation of the TTransport interface. |
| 48 | * |
| 49 | * @author Mark Slee <mcslee@facebook.com> |
| 50 | */ |
| 51 | class TSocketPool : public TSocket { |
| 52 | |
| 53 | public: |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 54 | |
| 55 | /** |
| 56 | * Socket pool constructor |
| 57 | */ |
| 58 | TSocketPool(); |
| 59 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 60 | /** |
| 61 | * Socket pool constructor |
| 62 | * |
| 63 | * @param hosts list of host names |
| 64 | * @param ports list of port names |
| 65 | */ |
| 66 | TSocketPool(const std::vector<std::string> &hosts, |
| 67 | const std::vector<int> &ports); |
| 68 | |
| 69 | /** |
| 70 | * Socket pool constructor |
| 71 | * |
| 72 | * @param servers list of pairs of host name and port |
| 73 | */ |
David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 74 | TSocketPool(const std::vector<std::pair<std::string, int> >& servers); |
| 75 | |
| 76 | /** |
| 77 | * Socket pool constructor |
| 78 | * |
| 79 | * @param servers list of TSocketPoolServers |
| 80 | */ |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 81 | TSocketPool(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 82 | |
| 83 | /** |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 84 | * Socket pool constructor |
| 85 | * |
| 86 | * @param host single host |
| 87 | * @param port single port |
| 88 | */ |
| 89 | TSocketPool(const std::string& host, int port); |
| 90 | |
| 91 | /** |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 92 | * Destroyes the socket object, closing it if necessary. |
| 93 | */ |
| 94 | virtual ~TSocketPool(); |
| 95 | |
| 96 | /** |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 97 | * Add a server to the pool |
| 98 | */ |
| 99 | void addServer(const std::string& host, int port); |
| 100 | |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 101 | /** |
| 102 | * Set list of servers in this pool |
| 103 | */ |
| 104 | void setServers(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); |
David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 105 | |
| 106 | /** |
| 107 | * Get list of servers in this pool |
| 108 | */ |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 109 | void getServers(std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); |
David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 110 | |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 111 | /** |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 112 | * Sets how many times to keep retrying a host in the connect function. |
| 113 | */ |
| 114 | void setNumRetries(int numRetries); |
| 115 | |
| 116 | /** |
| 117 | * Sets how long to wait until retrying a host if it was marked down |
| 118 | */ |
| 119 | void setRetryInterval(int retryInterval); |
| 120 | |
| 121 | /** |
| 122 | * Sets how many times to keep retrying a host before marking it as down. |
| 123 | */ |
| 124 | void setMaxConsecutiveFailures(int maxConsecutiveFailures); |
| 125 | |
| 126 | /** |
| 127 | * Turns randomization in connect order on or off. |
| 128 | */ |
| 129 | void setRandomize(bool randomize); |
| 130 | |
| 131 | /** |
| 132 | * Whether to always try the last server. |
| 133 | */ |
| 134 | void setAlwaysTryLast(bool alwaysTryLast); |
| 135 | |
| 136 | /** |
| 137 | * Creates and opens the UNIX socket. |
| 138 | */ |
| 139 | void open(); |
| 140 | |
| 141 | protected: |
| 142 | |
| 143 | /** List of servers to connect to */ |
David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 144 | std::vector< boost::shared_ptr<TSocketPoolServer> > servers_; |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 145 | |
| 146 | /** How many times to retry each host in connect */ |
| 147 | int numRetries_; |
| 148 | |
| 149 | /** Retry interval in seconds, how long to not try a host if it has been |
| 150 | * marked as down. |
| 151 | */ |
| 152 | int retryInterval_; |
| 153 | |
| 154 | /** Max consecutive failures before marking a host down. */ |
| 155 | int maxConsecutiveFailures_; |
| 156 | |
| 157 | /** Try hosts in order? or Randomized? */ |
| 158 | bool randomize_; |
| 159 | |
| 160 | /** Always try last host, even if marked down? */ |
| 161 | bool alwaysTryLast_; |
| 162 | }; |
| 163 | |
| 164 | }}} // facebook::thrift::transport |
| 165 | |
| 166 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_ |
| 167 | |