jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 1 | // Copyright (c) 2007- 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 | #include <algorithm> |
| 8 | #include <iostream> |
| 9 | |
| 10 | #include "TSocketPool.h" |
| 11 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 12 | namespace facebook { namespace thrift { namespace transport { |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 13 | |
| 14 | using namespace std; |
| 15 | |
| 16 | /** |
| 17 | * TSocketPool implementation. |
| 18 | * |
| 19 | * @author Jason Sobel <jsobel@facebook.com> |
| 20 | */ |
| 21 | |
| 22 | TSocketPool::TSocketPool(const vector<string> &hosts, |
| 23 | const vector<int> &ports) : TSocket(), |
| 24 | numRetries_(1), |
| 25 | retryInterval_(60), |
| 26 | maxConsecutiveFailures_(1), |
| 27 | randomize_(true), |
| 28 | alwaysTryLast_(true) |
| 29 | { |
| 30 | if (hosts.size() != ports.size()) { |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 31 | GlobalOutput("TSocketPool::TSocketPool: hosts.size != ports.size"); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 32 | throw TTransportException(TTransportException::BAD_ARGS); |
| 33 | } |
| 34 | |
| 35 | for (unsigned int i = 0; i < hosts.size(); ++i) { |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 36 | addServer(hosts[i], ports[i]); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 37 | } |
| 38 | } |
| 39 | |
| 40 | TSocketPool::TSocketPool(const vector<pair<string, int> > servers) : TSocket(), |
| 41 | servers_(servers), |
| 42 | numRetries_(1), |
| 43 | retryInterval_(60), |
| 44 | maxConsecutiveFailures_(1), |
| 45 | randomize_(true), |
| 46 | alwaysTryLast_(true) |
| 47 | { |
| 48 | } |
| 49 | |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 50 | TSocketPool::TSocketPool(const string& host, int port) : TSocket(), |
| 51 | numRetries_(1), |
| 52 | retryInterval_(60), |
| 53 | maxConsecutiveFailures_(1), |
| 54 | randomize_(true), |
| 55 | alwaysTryLast_(true) |
| 56 | { |
| 57 | addServer(host, port); |
| 58 | } |
| 59 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 60 | TSocketPool::~TSocketPool() { |
| 61 | close(); |
| 62 | } |
| 63 | |
dweatherford | d137282 | 2007-10-09 22:57:23 +0000 | [diff] [blame] | 64 | void TSocketPool::addServer(const string& host, int port) { |
| 65 | servers_.push_back(pair<string, int>(host, port)); |
| 66 | } |
| 67 | |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 68 | void TSocketPool::setNumRetries(int numRetries) { |
| 69 | numRetries_ = numRetries; |
| 70 | } |
| 71 | |
| 72 | void TSocketPool::setRetryInterval(int retryInterval) { |
| 73 | retryInterval_ = retryInterval; |
| 74 | } |
| 75 | |
| 76 | |
| 77 | void TSocketPool::setMaxConsecutiveFailures(int maxConsecutiveFailures) { |
| 78 | maxConsecutiveFailures_ = maxConsecutiveFailures; |
| 79 | } |
| 80 | |
| 81 | void TSocketPool::setRandomize(bool randomize) { |
| 82 | randomize_ = randomize; |
| 83 | } |
| 84 | |
| 85 | void TSocketPool::setAlwaysTryLast(bool alwaysTryLast) { |
| 86 | alwaysTryLast_ = alwaysTryLast; |
| 87 | } |
| 88 | |
| 89 | /* TODO: without apc we ignore a lot of functionality from the php version */ |
| 90 | void TSocketPool::open() { |
| 91 | if (randomize_) { |
| 92 | std::random_shuffle(servers_.begin(), servers_.end()); |
| 93 | } |
| 94 | |
| 95 | for (unsigned int i = 0; i < servers_.size(); ++i) { |
| 96 | host_ = servers_[i].first; |
| 97 | port_ = servers_[i].second; |
| 98 | |
| 99 | for (int j = 0; j < numRetries_; ++j) { |
| 100 | try { |
| 101 | TSocket::open(); |
| 102 | |
| 103 | // success |
| 104 | return; |
| 105 | } catch (TException e) { |
| 106 | // connection failed |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
boz | 6ded775 | 2007-06-05 22:41:18 +0000 | [diff] [blame] | 111 | GlobalOutput("TSocketPool::open: all connections failed"); |
jsobel | e02e424 | 2007-05-08 17:51:49 +0000 | [diff] [blame] | 112 | throw TTransportException(TTransportException::NOT_OPEN); |
| 113 | } |
| 114 | |
| 115 | }}} // facebook::thrift::transport |