增加连接层的性能统计

This commit is contained in:
Lu Qiuwen
2018-11-02 13:52:30 +08:00
parent 2e13728bfc
commit b3b65369d8
6 changed files with 152 additions and 92 deletions

View File

@@ -149,7 +149,7 @@ void tfe_stream_suspend(const struct tfe_stream * stream, enum tfe_conn_dir by)
ret = bufferevent_disable(_stream->conn_downstream->bev, EV_READ | EV_WRITE);
assert(ret == 0);
(void)ret;
(void) ret;
}
void tfe_stream_resume(const struct tfe_stream * stream)
@@ -313,6 +313,8 @@ static void __conn_private_destory(struct tfe_conn_private * conn)
if (conn->fd > 0) evutil_closesocket(conn->fd);
free(conn);
TFE_PROXY_STAT_INCREASE(STAT_FD_INSTANT_CLOSE, 1);
}
static void __conn_private_destory_with_ssl(struct event_base * ev_base,
@@ -324,12 +326,12 @@ static void __conn_private_destory_with_ssl(struct event_base * ev_base,
return __conn_private_destory(conn);
}
static void __stream_bev_downstream_statcb(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg)
static void __stream_bev_downstream_statcb(struct evbuffer * buffer, const struct evbuffer_cb_info * info, void * arg)
{
struct tfe_stream_private * _stream = (struct tfe_stream_private *) arg;
}
static void __stream_bev_upstream_statcb(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg)
static void __stream_bev_upstream_statcb(struct evbuffer * buffer, const struct evbuffer_cb_info * info, void * arg)
{
struct tfe_stream_private * _stream = (struct tfe_stream_private *) arg;
}
@@ -465,6 +467,8 @@ static void __stream_bev_readcb(struct bufferevent * bev, void * arg)
}
struct evbuffer * outbuf = bufferevent_get_output(peer_conn->bev);
assert(inbuf != NULL && outbuf != NULL);
enum tfe_stream_action action_tmp = ACTION_FORWARD_DATA;
enum tfe_stream_action action_final = ACTION_FORWARD_DATA;
@@ -573,7 +577,6 @@ static void __stream_bev_writecb(struct bufferevent * bev, void * arg)
ref_this_conn = &_stream->conn_upstream;
ref_this_ssl_stream = &_stream->ssl_upstream;
ref_peer_conn = &_stream->conn_downstream;
__str_dir = "up";
}
if (__bev_dir(_stream, bev) == CONN_DIR_DOWNSTREAM)
@@ -581,20 +584,20 @@ static void __stream_bev_writecb(struct bufferevent * bev, void * arg)
ref_this_conn = &_stream->conn_downstream;
ref_this_ssl_stream = &_stream->ssl_downstream;
ref_peer_conn = &_stream->conn_upstream;
__str_dir = "down";
}
struct evbuffer * __output_buffer = bufferevent_get_output(bev);
assert(__output_buffer != NULL);
if (*ref_peer_conn == NULL && (*ref_this_conn)->on_writing == 0 && evbuffer_get_length(__output_buffer) == 0)
if (*ref_peer_conn == NULL /* Peer connection is closed */
&& (*ref_this_conn)->on_writing == 0 /* No body is prepare to write data, eg. No body call stream_write */
&& evbuffer_get_length(__output_buffer) == 0) /* Nothing is in send queue */
{
TFE_PROXY_STAT_INCREASE(STAT_FD_CLOSE_BY_EVENT_WRITE, 1);
__conn_private_destory_with_ssl(ev_base, *ref_this_conn, *ref_this_ssl_stream);
*ref_this_conn = NULL;
*ref_this_ssl_stream = NULL;
// fprintf(stderr, "---- writecb ----, close this connection, "
// "stream = %p, dir = %s\n", _stream, __str_dir);
}
if (*ref_peer_conn == NULL && *ref_this_conn == NULL)
@@ -602,8 +605,6 @@ static void __stream_bev_writecb(struct bufferevent * bev, void * arg)
call_plugin_close(_stream);
tfe_stream_destory(_stream);
}
return;
}
/*
@@ -627,7 +628,6 @@ static void __stream_bev_eventcb(struct bufferevent * bev, short events, void *
ref_peer_conn = &_stream->conn_downstream;
ref_this_ssl_stream = &_stream->ssl_upstream;
ref_peer_ssl_stream = &_stream->ssl_downstream;
__str_dir = "up";
}
if (__bev_dir(_stream, bev) == CONN_DIR_DOWNSTREAM)
@@ -636,7 +636,6 @@ static void __stream_bev_eventcb(struct bufferevent * bev, short events, void *
ref_peer_conn = &_stream->conn_upstream;
ref_this_ssl_stream = &_stream->ssl_downstream;
ref_peer_ssl_stream = &_stream->ssl_upstream;
__str_dir = "down";
}
if (events & BEV_EVENT_ERROR || events & BEV_EVENT_EOF)
@@ -646,6 +645,9 @@ static void __stream_bev_eventcb(struct bufferevent * bev, short events, void *
__stream_bev_readcb(bev, arg);
}
if (events & BEV_EVENT_ERROR) { TFE_PROXY_STAT_INCREASE(STAT_FD_CLOSE_BY_EVENT_ERROR, 1); }
if (events & BEV_EVENT_EOF) { TFE_PROXY_STAT_INCREASE(STAT_FD_CLOSE_BY_EVENT_EOF, 1); }
goto __close_connection;
}
@@ -667,9 +669,6 @@ __close_connection:
if (*ref_this_conn != NULL)
{
// fprintf(stderr, "---- eventcb ----, close this connection, "
// "stream = %p, event = %x, dir = %s\n", _stream, events, __str_dir);
__conn_private_destory_with_ssl(ev_base, *ref_this_conn, *ref_this_ssl_stream);
*ref_this_conn = NULL;
*ref_this_ssl_stream = NULL;
@@ -692,6 +691,7 @@ static tfe_conn_private * __conn_private_create_by_fd(struct tfe_stream_private
struct tfe_conn_private * __conn_private = ALLOC(struct tfe_conn_private, 1);
struct event_base * __ev_base = stream->thread_ref->evbase;
__conn_private->_stream_ref = stream;
__conn_private->bev = bufferevent_socket_new(__ev_base, fd, BEV_OPT_DEFER_CALLBACKS);
__conn_private->fd = fd;
@@ -747,7 +747,7 @@ void ssl_downstream_create_on_success(future_result_t * result, void * user)
void ssl_downstream_create_on_fail(enum e_future_error err, const char * what, void * user)
{
struct tfe_stream_private * _stream = (struct tfe_stream_private *)user;
struct tfe_stream_private * _stream = (struct tfe_stream_private *) user;
assert(_stream != NULL && _stream->session_type == STREAM_PROTO_SSL);
TFE_STREAM_LOG_ERROR(_stream, "Failed to create SSL downstream, close the connection : %s. ", what);
@@ -787,7 +787,7 @@ void ssl_upstream_create_on_success(future_result_t * result, void * user)
void ssl_upstream_create_on_fail(enum e_future_error err, const char * what, void * user)
{
struct tfe_stream_private * _stream = (struct tfe_stream_private *)user;
struct tfe_stream_private * _stream = (struct tfe_stream_private *) user;
assert(_stream != NULL && _stream->session_type == STREAM_PROTO_SSL);
TFE_STREAM_LOG_ERROR(_stream, "Failed to create SSL upstream, pass-through the connection : %s. ", what);
@@ -807,6 +807,8 @@ void ssl_upstream_create_on_fail(enum e_future_error err, const char * what, voi
struct tfe_stream * tfe_stream_create(struct tfe_proxy * pxy, struct tfe_thread_ctx * thread_ctx)
{
struct tfe_stream_private * _stream = ALLOC(struct tfe_stream_private, 1);
TFE_PROXY_STAT_INCREASE(STAT_STREAM_CREATE, 1);
_stream->thread_ref = thread_ctx;
_stream->proxy_ref = pxy;
_stream->stream_logger = pxy->logger;
@@ -830,6 +832,7 @@ void tfe_stream_destory(struct tfe_stream_private * stream)
struct tfe_thread_ctx * thread = stream->thread_ref;
struct event_base * ev_base = thread->evbase;
TFE_PROXY_STAT_INCREASE(STAT_STREAM_DESTROY, 1);
__stream_access_log_write(stream);
if (stream->str_stream_addr)
@@ -1041,7 +1044,8 @@ int tfe_stream_init_by_fds(struct tfe_stream * stream, evutil_socket_t fd_downst
if (unlikely(_stream->head.addr == NULL))
{
TFE_LOG_ERROR(_stream->stream_logger, "Failed to create address from fd %d, %d, terminate fds.",
fd_downstream, fd_upstream); goto __errout;
fd_downstream, fd_upstream);
goto __errout;
}
_stream->str_stream_addr = tfe_stream_addr_to_str(_stream->head.addr);
@@ -1050,11 +1054,22 @@ int tfe_stream_init_by_fds(struct tfe_stream * stream, evutil_socket_t fd_downst
_stream->conn_downstream = __conn_private_create_by_fd(_stream, fd_downstream);
_stream->conn_upstream = __conn_private_create_by_fd(_stream, fd_upstream);
/* Defer FD has been transfer to conn_downstream/conn_upstream */
_stream->defer_fd_downstream = 0;
_stream->defer_fd_upstream = 0;
if (unlikely(_stream->conn_downstream == NULL || _stream->conn_upstream == NULL))
{
goto __errout;
}
assert(_stream->conn_downstream != NULL);
assert(_stream->conn_upstream != NULL);
__conn_private_enable(_stream->conn_downstream);
__conn_private_enable(_stream->conn_upstream);
TFE_PROXY_STAT_INCREASE(STAT_STREAM_TCP_PLAIN, 1);
}
if (_stream->session_type == STREAM_PROTO_SSL)
@@ -1067,6 +1082,8 @@ int tfe_stream_init_by_fds(struct tfe_stream * stream, evutil_socket_t fd_downst
/* Defer setup conn_downstream & conn_upstream in async callbacks. */
ssl_async_upstream_create(_stream->future_upstream_create,
_stream->ssl_mgr, fd_upstream, fd_downstream, ev_base);
TFE_PROXY_STAT_INCREASE(STAT_STREAM_TCP_SSL, 1);
}
return 0;