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 | |
| 13 | namespace facebook { namespace thrift { namespace transport { |
| 14 | |
| 15 | /** |
| 16 | * TCP Socket implementation of the TTransport interface. |
| 17 | * |
| 18 | * @author Mark Slee <mcslee@facebook.com> |
| 19 | */ |
| 20 | class TSocketPool : public TSocket { |
| 21 | |
| 22 | public: |
| 23 | /** |
| 24 | * Socket pool constructor |
| 25 | * |
| 26 | * @param hosts list of host names |
| 27 | * @param ports list of port names |
| 28 | */ |
| 29 | TSocketPool(const std::vector<std::string> &hosts, |
| 30 | const std::vector<int> &ports); |
| 31 | |
| 32 | /** |
| 33 | * Socket pool constructor |
| 34 | * |
| 35 | * @param servers list of pairs of host name and port |
| 36 | */ |
| 37 | TSocketPool(const std::vector<std::pair<std::string, int> > servers); |
| 38 | |
| 39 | /** |
| 40 | * Destroyes the socket object, closing it if necessary. |
| 41 | */ |
| 42 | virtual ~TSocketPool(); |
| 43 | |
| 44 | /** |
| 45 | * Sets how many times to keep retrying a host in the connect function. |
| 46 | */ |
| 47 | void setNumRetries(int numRetries); |
| 48 | |
| 49 | /** |
| 50 | * Sets how long to wait until retrying a host if it was marked down |
| 51 | */ |
| 52 | void setRetryInterval(int retryInterval); |
| 53 | |
| 54 | /** |
| 55 | * Sets how many times to keep retrying a host before marking it as down. |
| 56 | */ |
| 57 | void setMaxConsecutiveFailures(int maxConsecutiveFailures); |
| 58 | |
| 59 | /** |
| 60 | * Turns randomization in connect order on or off. |
| 61 | */ |
| 62 | void setRandomize(bool randomize); |
| 63 | |
| 64 | /** |
| 65 | * Whether to always try the last server. |
| 66 | */ |
| 67 | void setAlwaysTryLast(bool alwaysTryLast); |
| 68 | |
| 69 | /** |
| 70 | * Creates and opens the UNIX socket. |
| 71 | */ |
| 72 | void open(); |
| 73 | |
| 74 | protected: |
| 75 | |
| 76 | /** List of servers to connect to */ |
| 77 | std::vector<std::pair<std::string, int> > servers_; |
| 78 | |
| 79 | /** How many times to retry each host in connect */ |
| 80 | int numRetries_; |
| 81 | |
| 82 | /** Retry interval in seconds, how long to not try a host if it has been |
| 83 | * marked as down. |
| 84 | */ |
| 85 | int retryInterval_; |
| 86 | |
| 87 | /** Max consecutive failures before marking a host down. */ |
| 88 | int maxConsecutiveFailures_; |
| 89 | |
| 90 | /** Try hosts in order? or Randomized? */ |
| 91 | bool randomize_; |
| 92 | |
| 93 | /** Always try last host, even if marked down? */ |
| 94 | bool alwaysTryLast_; |
| 95 | }; |
| 96 | |
| 97 | }}} // facebook::thrift::transport |
| 98 | |
| 99 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_ |
| 100 | |