forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathconn_pool.cc
68 lines (59 loc) · 2.52 KB
/
conn_pool.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "common/tcp/conn_pool.h"
#include <memory>
#include "envoy/event/dispatcher.h"
#include "envoy/event/timer.h"
#include "envoy/upstream/upstream.h"
#include "common/stats/timespan_impl.h"
#include "common/upstream/upstream_impl.h"
namespace Envoy {
namespace Tcp {
ActiveTcpClient::ActiveTcpClient(ConnPoolImpl& parent, const Upstream::HostConstSharedPtr& host,
uint64_t concurrent_stream_limit)
: Envoy::ConnectionPool::ActiveClient(parent, host->cluster().maxRequestsPerConnection(),
concurrent_stream_limit),
parent_(parent) {
Upstream::Host::CreateConnectionData data = host->createConnection(
parent_.dispatcher(), parent_.socketOptions(), parent_.transportSocketOptions());
real_host_description_ = data.host_description_;
connection_ = std::move(data.connection_);
connection_->addConnectionCallbacks(*this);
connection_->detectEarlyCloseWhenReadDisabled(false);
connection_->addReadFilter(std::make_shared<ConnReadFilter>(*this));
connection_->connect();
}
ActiveTcpClient::~ActiveTcpClient() {
// Handle the case where deferred delete results in the ActiveClient being destroyed before
// TcpConnectionData. Make sure the TcpConnectionData will not refer to this ActiveTcpClient
// and handle clean up normally done in clearCallbacks()
if (tcp_connection_data_) {
ASSERT(state_ == ActiveClient::State::CLOSED);
tcp_connection_data_->release();
parent_.onStreamClosed(*this, true);
parent_.checkForDrained();
}
parent_.onConnDestroyed();
}
void ActiveTcpClient::clearCallbacks() {
if (state_ == Envoy::ConnectionPool::ActiveClient::State::BUSY ||
state_ == Envoy::ConnectionPool::ActiveClient::State::DRAINING) {
parent_.onConnReleased(*this);
}
callbacks_ = nullptr;
tcp_connection_data_ = nullptr;
parent_.onStreamClosed(*this, true);
parent_.checkForDrained();
}
void ActiveTcpClient::onEvent(Network::ConnectionEvent event) {
Envoy::ConnectionPool::ActiveClient::onEvent(event);
// Do not pass the Connected event to any session which registered during onEvent above.
// Consumers of connection pool connections assume they are receiving already connected
// connections.
if (callbacks_ && event != Network::ConnectionEvent::Connected) {
callbacks_->onEvent(event);
// After receiving a disconnect event, the owner of callbacks_ will likely self-destruct.
// Clear the pointer to avoid using it again.
callbacks_ = nullptr;
}
}
} // namespace Tcp
} // namespace Envoy