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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
/*
* Copyright (C) Igor Sysoev
* Copyright (C) NGINX, Inc.
*/
#include <nxt_main.h>
#include <nxt_runtime.h>
static void nxt_stream_connection_peer(nxt_task_t *task,
nxt_upstream_peer_t *up);
static void nxt_stream_connection_close(nxt_task_t *task, void *obj,
void *data);
void
nxt_stream_connection_init(nxt_task_t *task, void *obj, void *data)
{
nxt_conn_t *c;
nxt_runtime_t *rt;
nxt_upstream_peer_t *up;
c = obj;
nxt_debug(task, "stream connection init");
up = nxt_mp_zget(c->mem_pool, sizeof(nxt_upstream_peer_t));
if (nxt_slow_path(up == NULL)) {
goto fail;
}
up->data = c;
rt = task->thread->runtime;
if (rt->upstream.length != 0) {
up->addr = rt->upstream;
} else {
nxt_str_set(&up->addr, "127.0.0.1:8080");
}
up->ready_handler = nxt_stream_connection_peer;
up->mem_pool = c->mem_pool;
nxt_upstream_round_robin_peer(task, up);
return;
fail:
/* TODO: close connection */
return;
}
static void
nxt_stream_connection_peer(nxt_task_t *task, nxt_upstream_peer_t *up)
{
nxt_conn_t *c;
nxt_conn_proxy_t *p;
c = up->data;
up->sockaddr->type = SOCK_STREAM;
nxt_log_debug(c->socket.log, "stream connection peer %*s",
(size_t) up->sockaddr->length,
nxt_sockaddr_start(up->sockaddr));
p = nxt_conn_proxy_create(c);
if (nxt_slow_path(p == NULL)) {
goto fail;
}
p->client->socket.data = p;
p->peer->socket.data = p;
p->client_buffer_size = 1024;
p->peer_buffer_size = 4096;
//p->client_wait_timeout = 9000;
p->connect_timeout = 7000;
p->reconnect_timeout = 500;
//p->peer_wait_timeout = 5000;
p->client_write_timeout = 3000;
p->peer_write_timeout = 3000;
p->completion_handler = nxt_stream_connection_close;
//p->retries = 10;
p->peer->remote = up->sockaddr;
if (0) {
nxt_event_engine_t *engine;
nxt_event_write_rate_t *rate;
rate = nxt_mp_get(c->mem_pool, sizeof(nxt_event_write_rate_t));
if (nxt_slow_path(rate == NULL)) {
goto fail;
}
c->rate = rate;
rate->limit = 1024;
rate->limit_after = 0;
rate->average = rate->limit;
engine = nxt_thread_event_engine();
rate->last = engine->timers.now;
}
nxt_conn_proxy(task, p);
return;
fail:
/* TODO: close connection */
return;
}
static void
nxt_stream_connection_close(nxt_task_t *task, void *obj, void *data)
{
nxt_event_conn_proxy_t *p;
p = obj;
nxt_log_debug(p->client->socket.log, "stream connection close");
nxt_mp_destroy(p->client->mem_pool);
}
|