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: |
| 54 | /** |
| 55 | * Socket pool constructor |
| 56 | * |
| 57 | * @param hosts list of host names |
| 58 | * @param ports list of port names |
| 59 | */ |
| 60 | TSocketPool(const std::vector<std::string> &hosts, |
| 61 | const std::vector<int> &ports); |
| 62 | |
| 63 | /** |
| 64 | * Socket pool constructor |
| 65 | * |
| 66 | * @param servers list of pairs of host name and port |
| 67 | */ |
| 68 | TSocketPool(const std::vector<std::pair<std::string, int> > servers); |
| 69 | |
| 70 | /** |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 71 | * Socket pool constructor |
| 72 | * |
| 73 | * @param host single host |
| 74 | * @param port single port |
| 75 | */ |
| 76 | TSocketPool(const std::string& host, int port); |
| 77 | |
| 78 | /** |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 79 | * Destroyes the socket object, closing it if necessary. |
| 80 | */ |
| 81 | virtual ~TSocketPool(); |
| 82 | |
| 83 | /** |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 84 | * Add a server to the pool |
| 85 | */ |
| 86 | void addServer(const std::string& host, int port); |
| 87 | |
| 88 | /** |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 89 | * Sets how many times to keep retrying a host in the connect function. |
| 90 | */ |
| 91 | void setNumRetries(int numRetries); |
| 92 | |
| 93 | /** |
| 94 | * Sets how long to wait until retrying a host if it was marked down |
| 95 | */ |
| 96 | void setRetryInterval(int retryInterval); |
| 97 | |
| 98 | /** |
| 99 | * Sets how many times to keep retrying a host before marking it as down. |
| 100 | */ |
| 101 | void setMaxConsecutiveFailures(int maxConsecutiveFailures); |
| 102 | |
| 103 | /** |
| 104 | * Turns randomization in connect order on or off. |
| 105 | */ |
| 106 | void setRandomize(bool randomize); |
| 107 | |
| 108 | /** |
| 109 | * Whether to always try the last server. |
| 110 | */ |
| 111 | void setAlwaysTryLast(bool alwaysTryLast); |
| 112 | |
| 113 | /** |
| 114 | * Creates and opens the UNIX socket. |
| 115 | */ |
| 116 | void open(); |
| 117 | |
| 118 | protected: |
| 119 | |
| 120 | /** List of servers to connect to */ |
David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame^] | 121 | std::vector<TSocketPoolServer> servers_; |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 122 | |
| 123 | /** How many times to retry each host in connect */ |
| 124 | int numRetries_; |
| 125 | |
| 126 | /** Retry interval in seconds, how long to not try a host if it has been |
| 127 | * marked as down. |
| 128 | */ |
| 129 | int retryInterval_; |
| 130 | |
| 131 | /** Max consecutive failures before marking a host down. */ |
| 132 | int maxConsecutiveFailures_; |
| 133 | |
| 134 | /** Try hosts in order? or Randomized? */ |
| 135 | bool randomize_; |
| 136 | |
| 137 | /** Always try last host, even if marked down? */ |
| 138 | bool alwaysTryLast_; |
| 139 | }; |
| 140 | |
| 141 | }}} // facebook::thrift::transport |
| 142 | |
| 143 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_ |
| 144 | |