-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
flb_upstream.c
878 lines (730 loc) · 24.6 KB
/
flb_upstream.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Fluent Bit
* ==========
* Copyright (C) 2019-2021 The Fluent Bit Authors
* Copyright (C) 2015-2018 Treasure Data Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <monkey/mk_core.h>
#include <fluent-bit/flb_info.h>
#include <fluent-bit/flb_mem.h>
#include <fluent-bit/flb_kv.h>
#include <fluent-bit/flb_slist.h>
#include <fluent-bit/flb_str.h>
#include <fluent-bit/flb_upstream.h>
#include <fluent-bit/flb_io.h>
#include <fluent-bit/tls/flb_tls.h>
#include <fluent-bit/flb_utils.h>
#include <fluent-bit/flb_thread_storage.h>
FLB_TLS_DEFINE(struct mk_list, flb_upstream_list_key);
/* Config map for Upstream networking setup */
struct flb_config_map upstream_net[] = {
{
FLB_CONFIG_MAP_BOOL, "net.keepalive", "true",
0, FLB_TRUE, offsetof(struct flb_net_setup, keepalive),
"Enable or disable Keepalive support"
},
{
FLB_CONFIG_MAP_TIME, "net.keepalive_idle_timeout", "30s",
0, FLB_TRUE, offsetof(struct flb_net_setup, keepalive_idle_timeout),
"Set maximum time allowed for an idle Keepalive connection"
},
{
FLB_CONFIG_MAP_TIME, "net.connect_timeout", "10s",
0, FLB_TRUE, offsetof(struct flb_net_setup, connect_timeout),
"Set maximum time allowed to establish a connection, this time "
"includes the TLS handshake"
},
{
FLB_CONFIG_MAP_STR, "net.source_address", NULL,
0, FLB_TRUE, offsetof(struct flb_net_setup, source_address),
"Specify network address to bind for data traffic"
},
{
FLB_CONFIG_MAP_INT, "net.keepalive_max_recycle", "2000",
0, FLB_TRUE, offsetof(struct flb_net_setup, keepalive_max_recycle),
"Set maximum number of times a keepalive connection can be used "
"before it is retired."
},
/* EOF */
{0}
};
int flb_upstream_needs_proxy(const char *host, const char *proxy,
const char *no_proxy);
/* Enable thread-safe mode for upstream connection */
void flb_upstream_thread_safe(struct flb_upstream *u)
{
u->thread_safe = FLB_TRUE;
pthread_mutex_init(&u->mutex_lists, NULL);
/*
* Upon upstream creation, automatically the upstream is linked into
* the main Fluent Bit context (struct flb_config *)->upstreams. We
* have to avoid any access to this context outside of the worker
* thread.
*/
mk_list_del(&u->_head);
}
struct mk_list *flb_upstream_get_config_map(struct flb_config *config)
{
struct mk_list *config_map;
config_map = flb_config_map_create(config, upstream_net);
return config_map;
}
void flb_upstream_queue_init(struct flb_upstream_queue *uq)
{
mk_list_init(&uq->av_queue);
mk_list_init(&uq->busy_queue);
mk_list_init(&uq->destroy_queue);
}
struct flb_upstream_queue *flb_upstream_queue_get(struct flb_upstream *u)
{
struct mk_list *head;
struct mk_list *list;
struct flb_upstream *th_u;
struct flb_upstream_queue *uq;
/*
* Get the upstream queue, this might be different if the upstream is running
* in single-thread or multi-thread mode.
*/
if (u->thread_safe == FLB_TRUE) {
list = flb_upstream_list_get();
if (!list) {
/*
* Here is the issue: a plugin enabled in multiworker mode in the
* initialization callback might wanted to use an upstream
* connection, but the init callback does not run in threaded mode
* so we hit this problem.
*
* As a fallback mechanism: just cross our fingers and return the
* principal upstream queue.
*/
return (struct flb_upstream_queue *) &u->queue;
}
mk_list_foreach(head, list) {
th_u = mk_list_entry(head, struct flb_upstream, _head);
if (th_u->parent_upstream == u) {
break;
}
th_u = NULL;
}
if (!th_u) {
return NULL;
}
uq = &th_u->queue;
}
else {
uq = &u->queue;
}
return uq;
}
void flb_upstream_list_set(struct mk_list *list)
{
FLB_TLS_SET(flb_upstream_list_key, list);
}
struct mk_list *flb_upstream_list_get()
{
return FLB_TLS_GET(flb_upstream_list_key);
}
/* Initialize any upstream environment context */
void flb_upstream_init()
{
/* Initialize the upstream queue thread local storage */
FLB_TLS_INIT(flb_upstream_list_key);
}
/* Creates a new upstream context */
struct flb_upstream *flb_upstream_create(struct flb_config *config,
const char *host, int port, int flags,
struct flb_tls *tls)
{
int ret;
char *proxy_protocol = NULL;
char *proxy_host = NULL;
char *proxy_port = NULL;
char *proxy_username = NULL;
char *proxy_password = NULL;
struct flb_upstream *u;
u = flb_calloc(1, sizeof(struct flb_upstream));
if (!u) {
flb_errno();
return NULL;
}
/* Set default networking setup values */
flb_net_setup_init(&u->net);
/* Set upstream to the http_proxy if it is specified. */
if (flb_upstream_needs_proxy(host, config->http_proxy, config->no_proxy) == FLB_TRUE) {
flb_debug("[upstream] config->http_proxy: %s", config->http_proxy);
ret = flb_utils_proxy_url_split(config->http_proxy, &proxy_protocol,
&proxy_username, &proxy_password,
&proxy_host, &proxy_port);
if (ret == -1) {
flb_errno();
flb_free(u);
return NULL;
}
u->tcp_host = flb_strdup(proxy_host);
u->tcp_port = atoi(proxy_port);
u->proxied_host = flb_strdup(host);
u->proxied_port = port;
if (proxy_username && proxy_password) {
u->proxy_username = flb_strdup(proxy_username);
u->proxy_password = flb_strdup(proxy_password);
}
flb_free(proxy_protocol);
flb_free(proxy_host);
flb_free(proxy_port);
flb_free(proxy_username);
flb_free(proxy_password);
}
else {
u->tcp_host = flb_strdup(host);
u->tcp_port = port;
}
if (!u->tcp_host) {
flb_free(u);
return NULL;
}
u->flags = flags;
u->flags |= FLB_IO_ASYNC;
u->thread_safe = FLB_FALSE;
/* Initialize queues */
flb_upstream_queue_init(&u->queue);
#ifdef FLB_HAVE_TLS
u->tls = tls;
#endif
mk_list_add(&u->_head, &config->upstreams);
return u;
}
/*
* Checks whehter a destinate URL should be proxied.
*/
int flb_upstream_needs_proxy(const char *host, const char *proxy,
const char *no_proxy)
{
int ret;
struct mk_list no_proxy_list;
struct mk_list *head;
struct flb_slist_entry *e = NULL;
/* No HTTP_PROXY, should not set up proxy for the upstream `host`. */
if (proxy == NULL) {
return FLB_FALSE;
}
/* No NO_PROXY with HTTP_PROXY set, should set up proxy for the upstream `host`. */
if (no_proxy == NULL) {
return FLB_TRUE;
}
/* NO_PROXY=`*`, it matches all hosts. */
if (strcmp(no_proxy, "*") == 0) {
return FLB_FALSE;
}
/* check the URL list in the NO_PROXY */
ret = flb_slist_create(&no_proxy_list);
if (ret != 0) {
return FLB_TRUE;
}
ret = flb_slist_split_string(&no_proxy_list, no_proxy, ',', -1);
if (ret <= 0) {
return FLB_TRUE;
}
ret = FLB_TRUE;
mk_list_foreach(head, &no_proxy_list) {
e = mk_list_entry(head, struct flb_slist_entry, _head);
if (strcmp(host, e->str) == 0) {
ret = FLB_FALSE;
break;
}
}
/* clean up the resources. */
flb_slist_destroy(&no_proxy_list);
return ret;
}
/* Create an upstream context using a valid URL (protocol, host and port) */
struct flb_upstream *flb_upstream_create_url(struct flb_config *config,
const char *url, int flags,
struct flb_tls *tls)
{
int ret;
int tmp_port = 0;
char *prot = NULL;
char *host = NULL;
char *port = NULL;
char *uri = NULL;
struct flb_upstream *u = NULL;
/* Parse and split URL */
ret = flb_utils_url_split(url, &prot, &host, &port, &uri);
if (ret == -1) {
flb_error("[upstream] invalid URL: %s", url);
return NULL;
}
if (!prot) {
flb_error("[upstream] unknown protocol type from URL: %s", url);
goto out;
}
/* Manage some default ports */
if (!port) {
if (strcasecmp(prot, "http") == 0) {
tmp_port = 80;
}
else if (strcasecmp(prot, "https") == 0) {
tmp_port = 443;
if ((flags & FLB_IO_TLS) == 0) {
flags |= FLB_IO_TLS;
}
}
}
else {
tmp_port = atoi(port);
}
if (tmp_port <= 0) {
flb_error("[upstream] unknown TCP port in URL: %s", url);
goto out;
}
u = flb_upstream_create(config, host, tmp_port, flags, tls);
if (!u) {
flb_error("[upstream] error creating context from URL: %s", url);
}
out:
if (prot) {
flb_free(prot);
}
if (host) {
flb_free(host);
}
if (port) {
flb_free(port);
}
if (uri) {
flb_free(uri);
}
return u;
}
/*
* This function moves the 'upstream connection' into the queue to be
* destroyed. Note that the caller is responsible to validate and check
* required mutex if this is being used in multi-worker mode.
*/
static int prepare_destroy_conn(struct flb_upstream_conn *u_conn)
{
struct flb_upstream *u = u_conn->u;
struct flb_upstream_queue *uq;
uq = flb_upstream_queue_get(u);
flb_trace("[upstream] destroy connection #%i to %s:%i",
u_conn->fd, u->tcp_host, u->tcp_port);
if (u->flags & FLB_IO_ASYNC) {
mk_event_del(u_conn->evl, &u_conn->event);
}
if (u_conn->fd > 0) {
flb_socket_close(u_conn->fd);
u_conn->fd = -1;
u_conn->event.fd = -1;
}
/* remove connection from the queue */
mk_list_del(&u_conn->_head);
/* Add node to destroy queue */
mk_list_add(&u_conn->_head, &uq->destroy_queue);
/*
* note: the connection context is destroyed by the engine once all events
* have been processed.
*/
return 0;
}
/* 'safe' version of prepare_destroy_conn. It set locks if necessary */
static inline int prepare_destroy_conn_safe(struct flb_upstream_conn *u_conn)
{
int ret;
struct flb_upstream *u = u_conn->u;
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_lock(&u->mutex_lists);
}
ret = prepare_destroy_conn(u_conn);
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_unlock(&u->mutex_lists);
}
return ret;
}
static int destroy_conn(struct flb_upstream_conn *u_conn)
{
#ifdef FLB_HAVE_TLS
if (u_conn->tls_session) {
flb_tls_session_destroy(u_conn->tls, u_conn);
}
#endif
mk_list_del(&u_conn->_head);
flb_free(u_conn);
return 0;
}
static struct flb_upstream_conn *create_conn(struct flb_upstream *u)
{
int ret;
time_t now;
struct mk_event_loop *evl;
struct flb_coro *coro = flb_coro_get();
struct flb_upstream_conn *conn;
struct flb_upstream_queue *uq;
now = time(NULL);
conn = flb_calloc(1, sizeof(struct flb_upstream_conn));
if (!conn) {
flb_errno();
return NULL;
}
conn->u = u;
conn->fd = -1;
conn->net_error = -1;
/* retrieve the event loop */
evl = flb_engine_evl_get();
conn->evl = evl;
if (u->net.connect_timeout > 0) {
conn->ts_connect_timeout = now + u->net.connect_timeout;
}
else {
conn->ts_connect_timeout = -1;
}
#ifdef FLB_HAVE_TLS
conn->tls = NULL;
conn->tls_session = NULL;
#endif
conn->ts_created = time(NULL);
conn->ts_assigned = time(NULL);
conn->ts_available = 0;
conn->ka_count = 0;
conn->coro = coro;
if (u->net.keepalive == FLB_TRUE) {
flb_upstream_conn_recycle(conn, FLB_TRUE);
}
else {
flb_upstream_conn_recycle(conn, FLB_FALSE);
}
MK_EVENT_ZERO(&conn->event);
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_lock(&u->mutex_lists);
}
/* Link new connection to the busy queue */
uq = flb_upstream_queue_get(u);
mk_list_add(&conn->_head, &uq->busy_queue);
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_unlock(&u->mutex_lists);
}
/* Start connection */
ret = flb_io_net_connect(conn, coro);
if (ret == -1) {
flb_debug("[upstream] connection #%i failed to %s:%i",
conn->fd, u->tcp_host, u->tcp_port);
prepare_destroy_conn_safe(conn);
return NULL;
}
if (conn->u->flags & FLB_IO_TCP_KA) {
flb_debug("[upstream] KA connection #%i to %s:%i is connected",
conn->fd, u->tcp_host, u->tcp_port);
}
/* Invalidate timeout for connection */
conn->ts_connect_timeout = -1;
return conn;
}
int flb_upstream_destroy(struct flb_upstream *u)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_upstream_conn *u_conn;
struct flb_upstream_queue *uq;
uq = flb_upstream_queue_get(u);
if (!uq) {
uq = &u->queue;
}
mk_list_foreach_safe(head, tmp, &uq->av_queue) {
u_conn = mk_list_entry(head, struct flb_upstream_conn, _head);
prepare_destroy_conn(u_conn);
}
mk_list_foreach_safe(head, tmp, &uq->busy_queue) {
u_conn = mk_list_entry(head, struct flb_upstream_conn, _head);
prepare_destroy_conn(u_conn);
}
mk_list_foreach_safe(head, tmp, &uq->destroy_queue) {
u_conn = mk_list_entry(head, struct flb_upstream_conn, _head);
destroy_conn(u_conn);
}
flb_free(u->tcp_host);
flb_free(u->proxied_host);
flb_free(u->proxy_username);
flb_free(u->proxy_password);
mk_list_del(&u->_head);
flb_free(u);
return 0;
}
/* Enable or disable 'recycle' flag for the connection */
int flb_upstream_conn_recycle(struct flb_upstream_conn *conn, int val)
{
if (val == FLB_TRUE || val == FLB_FALSE) {
conn->recycle = val;
}
return -1;
}
struct flb_upstream_conn *flb_upstream_conn_get(struct flb_upstream *u)
{
int err;
struct mk_list *tmp;
struct mk_list *head;
struct flb_upstream_conn *conn = NULL;
struct flb_upstream_queue *uq;
uq = flb_upstream_queue_get(u);
flb_trace("[upstream] get new connection for %s:%i, net setup:\n"
"net.connect_timeout = %i seconds\n"
"net.source_address = %s\n"
"net.keepalive = %s\n"
"net.keepalive_idle_timeout = %i seconds",
u->tcp_host, u->tcp_port,
u->net.connect_timeout,
u->net.source_address ? u->net.source_address: "any",
u->net.keepalive ? "enabled": "disabled",
u->net.keepalive_idle_timeout);
/* On non Keepalive mode, always create a new TCP connection */
if (u->net.keepalive == FLB_FALSE) {
return create_conn(u);
}
/*
* If we are in keepalive mode, iterate list of available connections,
* take a little of time to do some cleanup and assign a connection. If no
* entries exists, just create a new one.
*/
mk_list_foreach_safe(head, tmp, &uq->av_queue) {
conn = mk_list_entry(head, struct flb_upstream_conn, _head);
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_lock(&u->mutex_lists);
}
/* This connection works, let's move it to the busy queue */
mk_list_del(&conn->_head);
mk_list_add(&conn->_head, &uq->busy_queue);
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_unlock(&u->mutex_lists);
}
/* Reset errno */
conn->net_error = -1;
err = flb_socket_error(conn->fd);
if (!FLB_EINPROGRESS(err) && err != 0) {
flb_debug("[upstream] KA connection #%i is in a failed state "
"to: %s:%i, cleaning up",
conn->fd, u->tcp_host, u->tcp_port);
prepare_destroy_conn_safe(conn);
conn = NULL;
continue;
}
/* Connect timeout */
conn->ts_assigned = time(NULL);
flb_debug("[upstream] KA connection #%i to %s:%i has been assigned (recycled)",
conn->fd, u->tcp_host, u->tcp_port);
/*
* Note: since we are in a keepalive connection, the socket is already being
* monitored for possible disconnections while idle. Upon re-use by the caller
* when it try to send some data, the I/O interface (flb_io.c) will put the
* proper event mask and reuse, there is no need to remove the socket from
* the event loop and re-add it again.
*
* So just return the connection context.
*/
return conn;
}
/* No keepalive connection available, create a new one */
if (!conn) {
conn = create_conn(u);
}
return conn;
}
/*
* An 'idle' and keepalive might be disconnected, if so, this callback will perform
* the proper connection cleanup.
*/
static int cb_upstream_conn_ka_dropped(void *data)
{
struct flb_upstream_conn *conn;
conn = (struct flb_upstream_conn *) data;
flb_debug("[upstream] KA connection #%i to %s:%i has been disconnected "
"by the remote service",
conn->fd, conn->u->tcp_host, conn->u->tcp_port);
return prepare_destroy_conn_safe(conn);
}
int flb_upstream_conn_release(struct flb_upstream_conn *conn)
{
int ret;
struct flb_upstream *u = conn->u;
struct flb_upstream_queue *uq;
uq = flb_upstream_queue_get(u);
/* If this is a valid KA connection just recycle */
if (conn->u->net.keepalive == FLB_TRUE && conn->recycle == FLB_TRUE && conn->fd > -1) {
/*
* This connection is still useful, move it to the 'available' queue and
* initialize variables.
*/
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_lock(&u->mutex_lists);
}
mk_list_del(&conn->_head);
mk_list_add(&conn->_head, &uq->av_queue);
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_unlock(&u->mutex_lists);
}
conn->ts_available = time(NULL);
/*
* The socket at this point is not longer monitored, so if we want to be
* notified if the 'available keepalive connection' gets disconnected by
* the remote endpoint we need to add it again.
*/
conn->event.handler = cb_upstream_conn_ka_dropped;
ret = mk_event_add(conn->evl, conn->fd,
FLB_ENGINE_EV_CUSTOM,
MK_EVENT_CLOSE, &conn->event);
if (ret == -1) {
/* We failed the registration, for safety just destroy the connection */
flb_debug("[upstream] KA connection #%i to %s:%i could not be "
"registered, closing.",
conn->fd, conn->u->tcp_host, conn->u->tcp_port);
return prepare_destroy_conn_safe(conn);
}
flb_debug("[upstream] KA connection #%i to %s:%i is now available",
conn->fd, conn->u->tcp_host, conn->u->tcp_port);
conn->ka_count++;
/* if we have exceeded our max number of uses of this connection, destroy it */
if (conn->u->net.keepalive_max_recycle > 0 &&
conn->ka_count > conn->u->net.keepalive_max_recycle) {
flb_debug("[upstream] KA count %i exceeded configured limit "
"of %i: closing.",
conn->ka_count, conn->u->net.keepalive_max_recycle);
return prepare_destroy_conn(conn);
}
return 0;
}
/* No keepalive connections must be destroyed */
return prepare_destroy_conn_safe(conn);
}
int flb_upstream_conn_timeouts(struct mk_list *list)
{
time_t now;
int drop;
struct mk_list *head;
struct mk_list *u_head;
struct mk_list *tmp;
struct flb_upstream *u;
struct flb_upstream_conn *u_conn;
struct flb_upstream_queue *uq;
now = time(NULL);
/* Iterate all upstream contexts */
mk_list_foreach(head, list) {
u = mk_list_entry(head, struct flb_upstream, _head);
uq = flb_upstream_queue_get(u);
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_lock(&u->mutex_lists);
}
/* Iterate every busy connection */
mk_list_foreach_safe(u_head, tmp, &uq->busy_queue) {
u_conn = mk_list_entry(u_head, struct flb_upstream_conn, _head);
drop = FLB_FALSE;
/* Connect timeouts */
if (u->net.connect_timeout > 0 &&
u_conn->ts_connect_timeout > 0 &&
u_conn->ts_connect_timeout <= now) {
drop = FLB_TRUE;
flb_error("[upstream] connection #%i to %s:%i timed out after "
"%i seconds",
u_conn->fd,
u->tcp_host, u->tcp_port, u->net.connect_timeout);
}
if (drop == FLB_TRUE) {
/*
* Shutdown the connection, this is the safest way to indicate
* that the socket cannot longer work and any co-routine on
* waiting for I/O will receive the notification and trigger
* the error to it caller.
*/
shutdown(u_conn->fd, SHUT_RDWR);
u_conn->net_error = ETIMEDOUT;
prepare_destroy_conn(u_conn);
}
}
/* Check every available Keepalive connection */
mk_list_foreach_safe(u_head, tmp, &uq->av_queue) {
u_conn = mk_list_entry(u_head, struct flb_upstream_conn, _head);
if ((now - u_conn->ts_available) >= u->net.keepalive_idle_timeout) {
shutdown(u_conn->fd, SHUT_RDWR);
prepare_destroy_conn(u_conn);
flb_debug("[upstream] drop keepalive connection #%i to %s:%i "
"(keepalive idle timeout)",
u_conn->fd, u->tcp_host, u->tcp_port);
}
}
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_unlock(&u->mutex_lists);
}
}
return 0;
}
int flb_upstream_conn_pending_destroy(struct flb_upstream *u)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_upstream_conn *u_conn;
struct flb_upstream_queue *uq;
uq = flb_upstream_queue_get(u);
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_lock(&u->mutex_lists);
}
/* Real destroy of connections context */
mk_list_foreach_safe(head, tmp, &uq->destroy_queue) {
u_conn = mk_list_entry(head, struct flb_upstream_conn, _head);
destroy_conn(u_conn);
}
if (u->thread_safe == FLB_TRUE) {
pthread_mutex_lock(&u->mutex_lists);
}
return 0;
}
int flb_upstream_conn_active_destroy(struct flb_upstream *u)
{
struct mk_list *tmp;
struct mk_list *head;
struct flb_upstream_conn *u_conn;
struct flb_upstream_queue *uq;
uq = flb_upstream_queue_get(u);
/* Real destroy of connections context */
mk_list_foreach_safe(head, tmp, &uq->av_queue) {
u_conn = mk_list_entry(head, struct flb_upstream_conn, _head);
destroy_conn(u_conn);
}
return 0;
}
int flb_upstream_conn_active_destroy_list(struct mk_list *list)
{
struct mk_list *head;
struct flb_upstream *u;
/* Iterate all upstream contexts */
mk_list_foreach(head, list) {
u = mk_list_entry(head, struct flb_upstream, _head);
flb_upstream_conn_active_destroy(u);
}
return 0;
}
int flb_upstream_conn_pending_destroy_list(struct mk_list *list)
{
struct mk_list *head;
struct flb_upstream *u;
/* Iterate all upstream contexts */
mk_list_foreach(head, list) {
u = mk_list_entry(head, struct flb_upstream, _head);
flb_upstream_conn_pending_destroy(u);
}
return 0;
}
int flb_upstream_is_async(struct flb_upstream *u)
{
if (u->flags & FLB_IO_ASYNC) {
return FLB_TRUE;
}
return FLB_FALSE;
}