Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-17 08:35:03

0001 /*
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements. See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership. The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License. You may obtain a copy of the License at
0009  *
0010  *   http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing,
0013  * software distributed under the License is distributed on an
0014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
0015  * KIND, either express or implied. See the License for the
0016  * specific language governing permissions and limitations
0017  * under the License.
0018  */
0019 
0020 #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_
0021 #define _THRIFT_TRANSPORT_TSOCKETPOOL_H_ 1
0022 
0023 #include <vector>
0024 #include <thrift/transport/TSocket.h>
0025 
0026 namespace apache {
0027 namespace thrift {
0028 namespace transport {
0029 
0030 /**
0031  * Class to hold server information for TSocketPool
0032  *
0033  */
0034 class TSocketPoolServer {
0035 
0036 public:
0037   /**
0038    * Default constructor for server info
0039    */
0040   TSocketPoolServer();
0041 
0042   /**
0043    * Constructor for TSocketPool server
0044    */
0045   TSocketPoolServer(const std::string& host, int port);
0046 
0047   // Host name
0048   std::string host_;
0049 
0050   // Port to connect on
0051   int port_;
0052 
0053   // Socket for the server
0054   THRIFT_SOCKET socket_;
0055 
0056   // Last time connecting to this server failed
0057   time_t lastFailTime_;
0058 
0059   // Number of consecutive times connecting to this server failed
0060   int consecutiveFailures_;
0061 };
0062 
0063 /**
0064  * TCP Socket implementation of the TTransport interface.
0065  *
0066  */
0067 class TSocketPool : public TSocket {
0068 
0069 public:
0070   /**
0071    * Socket pool constructor
0072    */
0073   TSocketPool();
0074 
0075   /**
0076    * Socket pool constructor
0077    *
0078    * @param hosts list of host names
0079    * @param ports list of port names
0080    */
0081   TSocketPool(const std::vector<std::string>& hosts, const std::vector<int>& ports);
0082 
0083   /**
0084    * Socket pool constructor
0085    *
0086    * @param servers list of pairs of host name and port
0087    */
0088   TSocketPool(const std::vector<std::pair<std::string, int> >& servers);
0089 
0090   /**
0091    * Socket pool constructor
0092    *
0093    * @param servers list of TSocketPoolServers
0094    */
0095   TSocketPool(const std::vector<std::shared_ptr<TSocketPoolServer> >& servers);
0096 
0097   /**
0098    * Socket pool constructor
0099    *
0100    * @param host single host
0101    * @param port single port
0102    */
0103   TSocketPool(const std::string& host, int port);
0104 
0105   /**
0106    * Destroyes the socket object, closing it if necessary.
0107    */
0108   ~TSocketPool() override;
0109 
0110   /**
0111    * Add a server to the pool
0112    */
0113   void addServer(const std::string& host, int port);
0114 
0115   /**
0116    * Add a server to the pool
0117    */
0118   void addServer(std::shared_ptr<TSocketPoolServer>& server);
0119 
0120   /**
0121    * Set list of servers in this pool
0122    */
0123   void setServers(const std::vector<std::shared_ptr<TSocketPoolServer> >& servers);
0124 
0125   /**
0126    * Get list of servers in this pool
0127    */
0128   void getServers(std::vector<std::shared_ptr<TSocketPoolServer> >& servers);
0129 
0130   /**
0131    * Sets how many times to keep retrying a host in the connect function.
0132    */
0133   void setNumRetries(int numRetries);
0134 
0135   /**
0136    * Sets how long to wait until retrying a host if it was marked down
0137    */
0138   void setRetryInterval(int retryInterval);
0139 
0140   /**
0141    * Sets how many times to keep retrying a host before marking it as down.
0142    */
0143   void setMaxConsecutiveFailures(int maxConsecutiveFailures);
0144 
0145   /**
0146    * Turns randomization in connect order on or off.
0147    */
0148   void setRandomize(bool randomize);
0149 
0150   /**
0151    * Whether to always try the last server.
0152    */
0153   void setAlwaysTryLast(bool alwaysTryLast);
0154 
0155   /**
0156    * Creates and opens the UNIX socket.
0157    */
0158   void open() override;
0159 
0160   /*
0161    * Closes the UNIX socket
0162    */
0163   void close() override;
0164 
0165 protected:
0166   void setCurrentServer(const std::shared_ptr<TSocketPoolServer>& server);
0167 
0168   /** List of servers to connect to */
0169   std::vector<std::shared_ptr<TSocketPoolServer> > servers_;
0170 
0171   /** Current server */
0172   std::shared_ptr<TSocketPoolServer> currentServer_;
0173 
0174   /** How many times to retry each host in connect */
0175   int numRetries_;
0176 
0177   /** Retry interval in seconds, how long to not try a host if it has been
0178    * marked as down.
0179    */
0180   time_t retryInterval_;
0181 
0182   /** Max consecutive failures before marking a host down. */
0183   int maxConsecutiveFailures_;
0184 
0185   /** Try hosts in order? or Randomized? */
0186   bool randomize_;
0187 
0188   /** Always try last host, even if marked down? */
0189   bool alwaysTryLast_;
0190 };
0191 }
0192 }
0193 } // apache::thrift::transport
0194 
0195 #endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_