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  */
19 
20 #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
21 #define _THRIFT_TRANSPORT_TSOCKETPOOL_H_ 1
22 
23 #include <vector>
24 #include <thrift/transport/TSocket.h>
25 
26 namespace apache {
27 namespace thrift {
28 namespace transport {
29 
30 /**
31  * Class to hold server information for TSocketPool
32  *
33  */
34 class TSocketPoolServer {
35 
36 public:
37   /**
38    * Default constructor for server info
39    */
40   TSocketPoolServer();
41 
42   /**
43    * Constructor for TSocketPool server
44    */
45   TSocketPoolServer(const std::string& host, int port);
46 
47   // Host name
48   std::string host_;
49 
50   // Port to connect on
51   int port_;
52 
53   // Socket for the server
54   THRIFT_SOCKET socket_;
55 
56   // Last time connecting to this server failed
57   time_t lastFailTime_;
58 
59   // Number of consecutive times connecting to this server failed
60   int consecutiveFailures_;
61 };
62 
63 /**
64  * TCP Socket implementation of the TTransport interface.
65  *
66  */
67 class TSocketPool : public TSocket {
68 
69 public:
70   /**
71    * Socket pool constructor
72    */
73   TSocketPool();
74 
75   /**
76    * Socket pool constructor
77    *
78    * @param hosts list of host names
79    * @param ports list of port names
80    */
81   TSocketPool(const std::vector<std::string>& hosts, const std::vector<int>& ports);
82 
83   /**
84    * Socket pool constructor
85    *
86    * @param servers list of pairs of host name and port
87    */
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    */
95   TSocketPool(const std::vector<std::shared_ptr<TSocketPoolServer> >& servers);
96 
97   /**
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   /**
106    * Destroyes the socket object, closing it if necessary.
107    */
108   ~TSocketPool() override;
109 
110   /**
111    * Add a server to the pool
112    */
113   void addServer(const std::string& host, int port);
114 
115   /**
116    * Add a server to the pool
117    */
118   void addServer(std::shared_ptr<TSocketPoolServer>& server);
119 
120   /**
121    * Set list of servers in this pool
122    */
123   void setServers(const std::vector<std::shared_ptr<TSocketPoolServer> >& servers);
124 
125   /**
126    * Get list of servers in this pool
127    */
128   void getServers(std::vector<std::shared_ptr<TSocketPoolServer> >& servers);
129 
130   /**
131    * 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() override;
159 
160   /*
161    * Closes the UNIX socket
162    */
163   void close() override;
164 
165 protected:
166   void setCurrentServer(const std::shared_ptr<TSocketPoolServer>& server);
167 
168   /** List of servers to connect to */
169   std::vector<std::shared_ptr<TSocketPoolServer> > servers_;
170 
171   /** Current server */
172   std::shared_ptr<TSocketPoolServer> currentServer_;
173 
174   /** How many times to retry each host in connect */
175   int numRetries_;
176 
177   /** Retry interval in seconds, how long to not try a host if it has been
178    * marked as down.
179    */
180   time_t retryInterval_;
181 
182   /** Max consecutive failures before marking a host down. */
183   int maxConsecutiveFailures_;
184 
185   /** Try hosts in order? or Randomized? */
186   bool randomize_;
187 
188   /** Always try last host, even if marked down? */
189   bool alwaysTryLast_;
190 };
191 }
192 }
193 } // apache::thrift::transport
194 
195 #endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
196