| 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 |  | 
| T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 13 | namespace apache { namespace thrift { namespace transport { | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 14 |  | 
| David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 15 |  /** | 
 | 16 |   * Class to hold server information for TSocketPool | 
 | 17 |   * | 
| David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 18 |   */ | 
 | 19 | class TSocketPoolServer { | 
 | 20 |  | 
 | 21 |   public: | 
 | 22 |   /** | 
 | 23 |    * Default constructor for server info | 
 | 24 |    */ | 
 | 25 |   TSocketPoolServer(); | 
 | 26 |  | 
 | 27 |   /** | 
 | 28 |    * Constructor for TSocketPool server | 
 | 29 |    */ | 
 | 30 |   TSocketPoolServer(const std::string &host, int port); | 
 | 31 |  | 
 | 32 |   // Host name | 
 | 33 |   std::string host_; | 
 | 34 |  | 
 | 35 |   // Port to connect on | 
 | 36 |   int port_; | 
 | 37 |  | 
| David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 38 |   // Socket for the server | 
 | 39 |   int socket_; | 
 | 40 |  | 
| David Reiss | 6d0cccd | 2008-02-28 21:20:12 +0000 | [diff] [blame] | 41 |   // Last time connecting to this server failed | 
 | 42 |   int lastFailTime_; | 
 | 43 |  | 
 | 44 |   // Number of consecutive times connecting to this server failed | 
 | 45 |   int consecutiveFailures_; | 
 | 46 | }; | 
 | 47 |  | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 48 | /** | 
 | 49 |  * TCP Socket implementation of the TTransport interface. | 
 | 50 |  * | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 51 |  */ | 
 | 52 | class TSocketPool : public TSocket { | 
 | 53 |  | 
 | 54 |  public: | 
| David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 55 |  | 
 | 56 |    /** | 
 | 57 |     * Socket pool constructor | 
 | 58 |     */ | 
 | 59 |    TSocketPool(); | 
 | 60 |  | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 61 |    /** | 
 | 62 |     * Socket pool constructor | 
 | 63 |     * | 
 | 64 |     * @param hosts list of host names | 
 | 65 |     * @param ports list of port names | 
 | 66 |     */ | 
 | 67 |    TSocketPool(const std::vector<std::string> &hosts, | 
 | 68 |                const std::vector<int> &ports); | 
 | 69 |  | 
 | 70 |    /** | 
 | 71 |     * Socket pool constructor | 
 | 72 |     * | 
 | 73 |     * @param servers list of pairs of host name and port | 
 | 74 |     */ | 
| David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 75 |    TSocketPool(const std::vector<std::pair<std::string, int> >& servers); | 
 | 76 |  | 
 | 77 |    /** | 
 | 78 |     * Socket pool constructor | 
 | 79 |     * | 
 | 80 |     * @param servers list of TSocketPoolServers | 
 | 81 |     */ | 
| David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 82 |   TSocketPool(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 83 |  | 
 | 84 |    /** | 
| dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 85 |     * Socket pool constructor | 
 | 86 |     * | 
 | 87 |     * @param host single host | 
 | 88 |     * @param port single port | 
 | 89 |     */ | 
 | 90 |    TSocketPool(const std::string& host, int port); | 
 | 91 |  | 
 | 92 |    /** | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 93 |     * Destroyes the socket object, closing it if necessary. | 
 | 94 |     */ | 
 | 95 |    virtual ~TSocketPool(); | 
 | 96 |  | 
 | 97 |    /** | 
| dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 98 |     * Add a server to the pool | 
 | 99 |     */ | 
 | 100 |    void addServer(const std::string& host, int port); | 
 | 101 |  | 
| David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 102 |    /** | 
 | 103 |     * Set list of servers in this pool | 
 | 104 |     */ | 
 | 105 |   void setServers(const std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); | 
| David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 106 |  | 
 | 107 |    /** | 
 | 108 |     * Get list of servers in this pool | 
 | 109 |     */ | 
| David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 110 |   void getServers(std::vector< boost::shared_ptr<TSocketPoolServer> >& servers); | 
| David Reiss | 907ad76 | 2008-03-02 00:25:58 +0000 | [diff] [blame] | 111 |  | 
| dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 112 |    /** | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 113 |     * Sets how many times to keep retrying a host in the connect function. | 
 | 114 |     */ | 
 | 115 |    void setNumRetries(int numRetries); | 
 | 116 |  | 
 | 117 |    /** | 
 | 118 |     * Sets how long to wait until retrying a host if it was marked down | 
 | 119 |     */ | 
 | 120 |    void setRetryInterval(int retryInterval); | 
 | 121 |  | 
 | 122 |    /** | 
 | 123 |     * Sets how many times to keep retrying a host before marking it as down. | 
 | 124 |     */ | 
 | 125 |    void setMaxConsecutiveFailures(int maxConsecutiveFailures); | 
 | 126 |  | 
 | 127 |    /** | 
 | 128 |     * Turns randomization in connect order on or off. | 
 | 129 |     */ | 
 | 130 |    void setRandomize(bool randomize); | 
 | 131 |  | 
 | 132 |    /** | 
 | 133 |     * Whether to always try the last server. | 
 | 134 |     */ | 
 | 135 |    void setAlwaysTryLast(bool alwaysTryLast); | 
 | 136 |  | 
 | 137 |    /** | 
 | 138 |     * Creates and opens the UNIX socket. | 
 | 139 |     */ | 
 | 140 |    void open(); | 
 | 141 |  | 
| David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 142 |    /* | 
 | 143 |     * Closes the UNIX socket | 
 | 144 |     */ | 
 | 145 |    void close(); | 
 | 146 |  | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 147 |  protected: | 
 | 148 |  | 
| David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 149 |   void setCurrentServer(const boost::shared_ptr<TSocketPoolServer> &server); | 
 | 150 |  | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 151 |    /** List of servers to connect to */ | 
| David Reiss | 8f3bce4 | 2008-03-18 18:21:52 +0000 | [diff] [blame] | 152 |   std::vector< boost::shared_ptr<TSocketPoolServer> > servers_; | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 153 |  | 
| David Reiss | 1997f10 | 2008-04-29 00:29:41 +0000 | [diff] [blame] | 154 |   /** Current server */ | 
 | 155 |   boost::shared_ptr<TSocketPoolServer> currentServer_; | 
 | 156 |  | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 157 |    /** How many times to retry each host in connect */ | 
 | 158 |    int numRetries_; | 
 | 159 |  | 
 | 160 |    /** Retry interval in seconds, how long to not try a host if it has been | 
 | 161 |     * marked as down. | 
 | 162 |     */ | 
 | 163 |    int retryInterval_; | 
 | 164 |  | 
 | 165 |    /** Max consecutive failures before marking a host down. */ | 
 | 166 |    int maxConsecutiveFailures_; | 
 | 167 |  | 
 | 168 |    /** Try hosts in order? or Randomized? */ | 
 | 169 |    bool randomize_; | 
 | 170 |  | 
 | 171 |    /** Always try last host, even if marked down? */ | 
 | 172 |    bool alwaysTryLast_; | 
 | 173 | }; | 
 | 174 |  | 
| T Jake Luciani | b5e6221 | 2009-01-31 22:36:20 +0000 | [diff] [blame] | 175 | }}} // apache::thrift::transport | 
| jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 176 |  | 
 | 177 | #endif // #ifndef _THRIFT_TRANSPORT_TSOCKETPOOL_H_ | 
 | 178 |  |