blob: 749116a167ebe493f1cf418fcdac6e7f256ac5a2 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 */
jsobele02e4242007-05-08 17:51:49 +000019
20#ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
21#define _THRIFT_TRANSPORT_TSOCKETPOOL_H_ 1
22
23#include <vector>
24#include "TSocket.h"
25
T Jake Lucianib5e62212009-01-31 22:36:20 +000026namespace apache { namespace thrift { namespace transport {
jsobele02e4242007-05-08 17:51:49 +000027
David Reiss6d0cccd2008-02-28 21:20:12 +000028 /**
29 * Class to hold server information for TSocketPool
30 *
David Reiss6d0cccd2008-02-28 21:20:12 +000031 */
32class 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 Reiss1997f102008-04-29 00:29:41 +000051 // Socket for the server
52 int socket_;
53
David Reiss6d0cccd2008-02-28 21:20:12 +000054 // 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
jsobele02e4242007-05-08 17:51:49 +000061/**
62 * TCP Socket implementation of the TTransport interface.
63 *
jsobele02e4242007-05-08 17:51:49 +000064 */
65class TSocketPool : public TSocket {
66
67 public:
David Reiss8f3bce42008-03-18 18:21:52 +000068
69 /**
70 * Socket pool constructor
71 */
72 TSocketPool();
73
jsobele02e4242007-05-08 17:51:49 +000074 /**
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 Reiss907ad762008-03-02 00:25:58 +000088 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 Reiss8f3bce42008-03-18 18:21:52 +000095 TSocketPool(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
jsobele02e4242007-05-08 17:51:49 +000096
97 /**
dweatherfordd1372822007-10-09 22:57:23 +000098 * 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 /**
jsobele02e4242007-05-08 17:51:49 +0000106 * Destroyes the socket object, closing it if necessary.
107 */
108 virtual ~TSocketPool();
109
110 /**
dweatherfordd1372822007-10-09 22:57:23 +0000111 * Add a server to the pool
112 */
113 void addServer(const std::string& host, int port);
114
David Reiss8f3bce42008-03-18 18:21:52 +0000115 /**
David Reiss8106ba62010-03-09 05:19:50 +0000116 * Add a server to the pool
117 */
118 void addServer(boost::shared_ptr<TSocketPoolServer> &server);
119
120 /**
David Reiss8f3bce42008-03-18 18:21:52 +0000121 * Set list of servers in this pool
122 */
123 void setServers(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
David Reiss907ad762008-03-02 00:25:58 +0000124
125 /**
126 * Get list of servers in this pool
127 */
David Reiss8f3bce42008-03-18 18:21:52 +0000128 void getServers(std::vector< boost::shared_ptr<TSocketPoolServer> >& servers);
David Reiss907ad762008-03-02 00:25:58 +0000129
dweatherfordd1372822007-10-09 22:57:23 +0000130 /**
jsobele02e4242007-05-08 17:51:49 +0000131 * Sets how many times to keep retrying a host in the connect function.
132 */
133 void setNumRetries(int numRetries);
134
135 /**
136 * Sets how long to wait until retrying a host if it was marked down
137 */
138 void setRetryInterval(int retryInterval);
139
140 /**
141 * Sets how many times to keep retrying a host before marking it as down.
142 */
143 void setMaxConsecutiveFailures(int maxConsecutiveFailures);
144
145 /**
146 * Turns randomization in connect order on or off.
147 */
148 void setRandomize(bool randomize);
149
150 /**
151 * Whether to always try the last server.
152 */
153 void setAlwaysTryLast(bool alwaysTryLast);
154
155 /**
156 * Creates and opens the UNIX socket.
157 */
158 void open();
159
David Reiss1997f102008-04-29 00:29:41 +0000160 /*
161 * Closes the UNIX socket
162 */
163 void close();
164
jsobele02e4242007-05-08 17:51:49 +0000165 protected:
166
David Reiss1997f102008-04-29 00:29:41 +0000167 void setCurrentServer(const boost::shared_ptr<TSocketPoolServer> &server);
168
jsobele02e4242007-05-08 17:51:49 +0000169 /** List of servers to connect to */
David Reiss8f3bce42008-03-18 18:21:52 +0000170 std::vector< boost::shared_ptr<TSocketPoolServer> > servers_;
jsobele02e4242007-05-08 17:51:49 +0000171
David Reiss1997f102008-04-29 00:29:41 +0000172 /** Current server */
173 boost::shared_ptr<TSocketPoolServer> currentServer_;
174
jsobele02e4242007-05-08 17:51:49 +0000175 /** How many times to retry each host in connect */
176 int numRetries_;
177
178 /** Retry interval in seconds, how long to not try a host if it has been
179 * marked as down.
180 */
181 int retryInterval_;
182
183 /** Max consecutive failures before marking a host down. */
184 int maxConsecutiveFailures_;
185
186 /** Try hosts in order? or Randomized? */
187 bool randomize_;
188
189 /** Always try last host, even if marked down? */
190 bool alwaysTryLast_;
191};
192
T Jake Lucianib5e62212009-01-31 22:36:20 +0000193}}} // apache::thrift::transport
jsobele02e4242007-05-08 17:51:49 +0000194
195#endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
196