| 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 |  | 
|  | 12 | namespace facebook { namespace thrift { namespace transport { | 
|  | 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()) { | 
|  | 31 | perror("TSocketPool::TSocketPool: hosts.size != ports.size"); | 
|  | 32 | throw TTransportException(TTransportException::BAD_ARGS); | 
|  | 33 | } | 
|  | 34 |  | 
|  | 35 | for (unsigned int i = 0; i < hosts.size(); ++i) { | 
|  | 36 | servers_.push_back(pair<string, int>(hosts[i], ports[i])); | 
|  | 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 |  | 
|  | 50 | TSocketPool::~TSocketPool() { | 
|  | 51 | close(); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | void TSocketPool::setNumRetries(int numRetries) { | 
|  | 55 | numRetries_ = numRetries; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | void TSocketPool::setRetryInterval(int retryInterval) { | 
|  | 59 | retryInterval_ = retryInterval; | 
|  | 60 | } | 
|  | 61 |  | 
|  | 62 |  | 
|  | 63 | void TSocketPool::setMaxConsecutiveFailures(int maxConsecutiveFailures) { | 
|  | 64 | maxConsecutiveFailures_ = maxConsecutiveFailures; | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | void TSocketPool::setRandomize(bool randomize) { | 
|  | 68 | randomize_ = randomize; | 
|  | 69 | } | 
|  | 70 |  | 
|  | 71 | void TSocketPool::setAlwaysTryLast(bool alwaysTryLast) { | 
|  | 72 | alwaysTryLast_ = alwaysTryLast; | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | /* TODO: without apc we ignore a lot of functionality from the php version */ | 
|  | 76 | void TSocketPool::open() { | 
|  | 77 | if (randomize_) { | 
|  | 78 | std::random_shuffle(servers_.begin(), servers_.end()); | 
|  | 79 | } | 
|  | 80 |  | 
|  | 81 | for (unsigned int i = 0; i < servers_.size(); ++i) { | 
|  | 82 | host_ = servers_[i].first; | 
|  | 83 | port_ = servers_[i].second; | 
|  | 84 |  | 
|  | 85 | for (int j = 0; j < numRetries_; ++j) { | 
|  | 86 | try { | 
|  | 87 | TSocket::open(); | 
|  | 88 |  | 
|  | 89 | // success | 
|  | 90 | return; | 
|  | 91 | } catch (TException e) { | 
|  | 92 | // connection failed | 
|  | 93 | } | 
|  | 94 | } | 
|  | 95 | } | 
|  | 96 |  | 
|  | 97 | perror("TSocketPool::open: all connections failed"); | 
|  | 98 | throw TTransportException(TTransportException::NOT_OPEN); | 
|  | 99 | } | 
|  | 100 |  | 
|  | 101 | }}} // facebook::thrift::transport |