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