summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_conn_proxy.c
blob: dec2368485764b200e7448316d29062f1d88142a (plain) (blame)
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
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998

/*
 * Copyright (C) Igor Sysoev
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>


static void nxt_conn_proxy_client_buffer_alloc(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_proxy_peer_connect(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_proxy_peer_read(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_proxy_client_read_ready(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_proxy_peer_read_ready(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_proxy_read_process(nxt_task_t *task, nxt_conn_proxy_t *p,
    nxt_conn_t *source, nxt_conn_t *sink);
static void nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b);
static void nxt_conn_proxy_read(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_proxy_client_write_ready(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_proxy_peer_write_ready(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_proxy_write_process(nxt_task_t *task, nxt_conn_proxy_t *p,
    nxt_conn_t *sink, nxt_conn_t *source);
static void nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b);
static void nxt_conn_proxy_close(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_proxy_error(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_proxy_read_timeout(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_proxy_write_timeout(nxt_task_t *task, void *obj,
    void *data);
static nxt_msec_t nxt_conn_proxy_timeout_value(nxt_conn_t *c, uintptr_t data);
static void nxt_conn_proxy_refused(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_proxy_reconnect_handler(nxt_task_t *task, void *obj,
    void *data);
static void nxt_conn_proxy_shutdown(nxt_task_t *task, nxt_conn_proxy_t *p,
    nxt_conn_t *source, nxt_conn_t *sink);
static void nxt_conn_proxy_read_error(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_proxy_write_error(nxt_task_t *task, void *obj, void *data);
static void nxt_conn_proxy_complete(nxt_task_t *task, nxt_conn_proxy_t *p);
static void nxt_conn_proxy_completion(nxt_task_t *task, void *obj, void *data);


static const nxt_conn_state_t  nxt_conn_proxy_client_wait_state;
static const nxt_conn_state_t  nxt_conn_proxy_client_first_read_state;
static const nxt_conn_state_t  nxt_conn_proxy_peer_connect_state;
static const nxt_conn_state_t  nxt_conn_proxy_peer_wait_state;
static const nxt_conn_state_t  nxt_conn_proxy_client_read_state;
static const nxt_conn_state_t  nxt_conn_proxy_peer_read_state;
static const nxt_conn_state_t  nxt_conn_proxy_client_write_state;
static const nxt_conn_state_t  nxt_conn_proxy_peer_write_state;


nxt_conn_proxy_t *
nxt_conn_proxy_create(nxt_conn_t *client)
{
    nxt_conn_t        *peer;
    nxt_thread_t      *thr;
    nxt_conn_proxy_t  *p;

    p = nxt_mem_zalloc(client->mem_pool, sizeof(nxt_conn_proxy_t));
    if (nxt_slow_path(p == NULL)) {
        return NULL;
    }

    peer = nxt_conn_create(client->mem_pool, client->socket.task);
    if (nxt_slow_path(peer == NULL)) {
        return NULL;
    }

    thr = nxt_thread();

    client->read_work_queue = &thr->engine->read_work_queue;
    client->write_work_queue = &thr->engine->write_work_queue;
    client->socket.read_work_queue = &thr->engine->read_work_queue;
    client->socket.write_work_queue = &thr->engine->write_work_queue;
    peer->socket.read_work_queue = &thr->engine->read_work_queue;
    peer->socket.write_work_queue = &thr->engine->write_work_queue;

    peer->socket.data = client->socket.data;

    peer->read_work_queue = client->read_work_queue;
    peer->write_work_queue = client->write_work_queue;
    peer->read_timer.work_queue = client->read_work_queue;
    peer->write_timer.work_queue = client->write_work_queue;

    p->client = client;
    p->peer = peer;

    return p;
}


void
nxt_conn_proxy(nxt_task_t *task, nxt_conn_proxy_t *p)
{
    nxt_conn_t  *peer;

    /*
     * Peer read event: not connected, disabled.
     * Peer write event: not connected, disabled.
     */

    if (p->client_wait_timeout == 0) {
        /*
         * Peer write event: waiting for connection
         * to be established with connect_timeout.
         */
        peer = p->peer;
        peer->write_state = &nxt_conn_proxy_peer_connect_state;

        nxt_conn_connect(task->thread->engine, peer);
    }

    /*
     * Client read event: waiting for client data with
     * client_wait_timeout before buffer allocation.
     */
    p->client->read_state = &nxt_conn_proxy_client_wait_state;

    nxt_conn_wait(p->client);
}


static const nxt_conn_state_t  nxt_conn_proxy_client_wait_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_client_buffer_alloc,
    .close_handler = nxt_conn_proxy_close,
    .error_handler = nxt_conn_proxy_error,

    .timer_handler = nxt_conn_proxy_read_timeout,
    .timer_value = nxt_conn_proxy_timeout_value,
    .timer_data = offsetof(nxt_conn_proxy_t, client_wait_timeout),
};


static void
nxt_conn_proxy_client_buffer_alloc(nxt_task_t *task, void *obj, void *data)
{
    nxt_buf_t         *b;
    nxt_conn_t        *client;
    nxt_conn_proxy_t  *p;

    client = obj;
    p = data;

    nxt_debug(task, "conn proxy client first read fd:%d", client->socket.fd);

    b = nxt_buf_mem_alloc(client->mem_pool, p->client_buffer_size,
                          NXT_MEM_BUF_CUTBACK | NXT_MEM_BUF_USABLE);

    if (nxt_slow_path(b == NULL)) {
        /* An error completion. */
        nxt_conn_proxy_complete(task, p);
        return;
    }

    p->client_buffer = b;
    client->read = b;

    if (p->peer->socket.fd != -1) {
        /*
         * Client read event: waiting, no timeout.
         * Client write event: blocked.
         * Peer read event: disabled.
         * Peer write event: waiting for connection to be established
         * or blocked after the connection has established.
         */
        client->read_state = &nxt_conn_proxy_client_read_state;

    } else {
        /*
         * Client read event: waiting for data with client_wait_timeout
         * before connecting to a peer.
         * Client write event: blocked.
         * Peer read event: not connected, disabled.
         * Peer write event: not connected, disabled.
         */
        client->read_state = &nxt_conn_proxy_client_first_read_state;
    }

    nxt_conn_read(task->thread->engine, client);
}


static const nxt_conn_state_t  nxt_conn_proxy_client_first_read_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_peer_connect,
    .close_handler = nxt_conn_proxy_close,
    .error_handler = nxt_conn_proxy_error,

    .timer_handler = nxt_conn_proxy_read_timeout,
    .timer_value = nxt_conn_proxy_timeout_value,
    .timer_data = offsetof(nxt_conn_proxy_t, client_wait_timeout),
    .timer_autoreset = 1,
};


static void
nxt_conn_proxy_peer_connect(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *client;
    nxt_conn_proxy_t  *p;

    client = obj;
    p = data;

    /*
     * Client read event: waiting, no timeout.
     * Client write event: blocked.
     * Peer read event: disabled.
     * Peer write event: waiting for connection to be established
     * with connect_timeout.
     */
    client->read_state = &nxt_conn_proxy_client_read_state;

    p->peer->write_state = &nxt_conn_proxy_peer_connect_state;

    nxt_conn_connect(task->thread->engine, p->peer);
}


static const nxt_conn_state_t  nxt_conn_proxy_peer_connect_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_connected,
    .close_handler = nxt_conn_proxy_refused,
    .error_handler = nxt_conn_proxy_error,

    .timer_handler = nxt_conn_proxy_write_timeout,
    .timer_value = nxt_conn_proxy_timeout_value,
    .timer_data = offsetof(nxt_conn_proxy_t, connect_timeout),
    .timer_autoreset = 1,
};


static void
nxt_conn_proxy_connected(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *client, *peer;
    nxt_conn_proxy_t  *p;

    peer = obj;
    p = data;

    nxt_debug(task, "conn proxy connected fd:%d", peer->socket.fd);

    p->connected = 1;

    nxt_conn_tcp_nodelay_on(task, peer);
    nxt_conn_tcp_nodelay_on(task, p->client);

    /* Peer read event: waiting with peer_wait_timeout.  */

    peer->read_state = &nxt_conn_proxy_peer_wait_state;
    peer->write_state = &nxt_conn_proxy_peer_write_state;

    nxt_conn_wait(peer);

    if (p->client_buffer != NULL) {
        client = p->client;

        client->read_state = &nxt_conn_proxy_client_read_state;
        client->write_state = &nxt_conn_proxy_client_write_state;
        /*
         * Send a client read data to the connected peer.
         * Client write event: blocked.
         */
        nxt_conn_proxy_read_process(task, p, client, peer);
    }
}


static const nxt_conn_state_t  nxt_conn_proxy_peer_wait_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_peer_read,
    .close_handler = nxt_conn_proxy_close,
    .error_handler = nxt_conn_proxy_error,

    .timer_handler = nxt_conn_proxy_read_timeout,
    .timer_value = nxt_conn_proxy_timeout_value,
    .timer_data = offsetof(nxt_conn_proxy_t, peer_wait_timeout),
};


static void
nxt_conn_proxy_peer_read(nxt_task_t *task, void *obj, void *data)
{
    nxt_buf_t         *b;
    nxt_conn_t        *peer;
    nxt_conn_proxy_t  *p;

    peer = obj;
    p = data;

    nxt_debug(task, "conn proxy peer read fd:%d", peer->socket.fd);

    b = nxt_buf_mem_alloc(peer->mem_pool, p->peer_buffer_size,
                          NXT_MEM_BUF_CUTBACK | NXT_MEM_BUF_USABLE);

    if (nxt_slow_path(b == NULL)) {
        /* An error completion. */
        nxt_conn_proxy_complete(task, p);
        return;
    }

    p->peer_buffer = b;
    peer->read = b;

    p->client->write_state = &nxt_conn_proxy_client_write_state;
    peer->read_state = &nxt_conn_proxy_peer_read_state;
    peer->write_state = &nxt_conn_proxy_peer_write_state;

    /*
     * Client read event: waiting, no timeout.
     * Client write event: blocked.
     * Peer read event: waiting with possible peer_wait_timeout.
     * Peer write event: blocked.
     */
    nxt_conn_read(task->thread->engine, peer);
}


static const nxt_conn_state_t  nxt_conn_proxy_client_read_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_client_read_ready,
    .close_handler = nxt_conn_proxy_close,
    .error_handler = nxt_conn_proxy_read_error,
};


static void
nxt_conn_proxy_client_read_ready(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *client;
    nxt_conn_proxy_t  *p;

    client = obj;
    p = data;

    nxt_debug(task, "conn proxy client read ready fd:%d", client->socket.fd);

    nxt_conn_proxy_read_process(task, p, client, p->peer);
}


static const nxt_conn_state_t  nxt_conn_proxy_peer_read_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_peer_read_ready,
    .close_handler = nxt_conn_proxy_close,
    .error_handler = nxt_conn_proxy_read_error,
};


static void
nxt_conn_proxy_peer_read_ready(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *peer;
    nxt_conn_proxy_t  *p;

    peer = obj;
    p = data;

    nxt_debug(task, "conn proxy peer read ready fd:%d", peer->socket.fd);

    nxt_conn_proxy_read_process(task, p, peer, p->client);
}


static void
nxt_conn_proxy_read_process(nxt_task_t *task, nxt_conn_proxy_t *p,
    nxt_conn_t *source, nxt_conn_t *sink)
{
    nxt_buf_t  *rb, *wb;

    if (sink->socket.error != 0) {
        nxt_debug(task, "conn proxy sink fd:%d error:%d",
                  sink->socket.fd, sink->socket.error);

        nxt_conn_proxy_write_error(task, sink, sink->socket.data);
        return;
    }

    while (source->read != NULL) {

        rb = source->read;

        if (rb->mem.pos != rb->mem.free) {

            /* Add a read part to a write chain. */

            wb = nxt_buf_mem_alloc(source->mem_pool, 0, 0);
            if (wb == NULL) {
                /* An error completion. */
                nxt_conn_proxy_complete(task, p);
                return;
            }

            wb->mem.pos = rb->mem.pos;
            wb->mem.free = rb->mem.free;
            wb->mem.start = rb->mem.pos;
            wb->mem.end = rb->mem.free;

            rb->mem.pos = rb->mem.free;
            rb->mem.start = rb->mem.free;

            nxt_conn_proxy_write_add(sink, wb);
        }

        if (rb->mem.start != rb->mem.end) {
            nxt_work_queue_add(source->read_work_queue, nxt_conn_proxy_read,
                               task, source, source->socket.data);
            break;
        }

        source->read = rb->next;
        nxt_buf_free(source->mem_pool, rb);
    }

    if (p->connected) {
        nxt_conn_write(task->thread->engine, sink);
    }
}


static void
nxt_conn_proxy_write_add(nxt_conn_t *c, nxt_buf_t *b)
{
    nxt_buf_t  *first, *second, *prev;

    first = c->write;

    if (first == NULL) {
        c->write = b;
        return;
    }

    /*
     * A event conn proxy maintains a buffer per each direction.
     * The buffer is divided by read and write parts.  These parts are
     * linked in buffer chains.  There can be no more than two buffers
     * in write chain at any time, because an added buffer is coalesced
     * with the last buffer if possible.
     */

    second = first->next;

    if (second == NULL) {

        if (first->mem.end != b->mem.start) {
            first->next = b;
            return;
        }

        /*
         * The first buffer is just before the added buffer, so
         * expand the first buffer to the end of the added buffer.
         */
        prev = first;

    } else {
        if (second->mem.end != b->mem.start) {
            nxt_thread_log_alert("event conn proxy write: second buffer end:%p "
                                 "is not equal to added buffer start:%p",
                                 second->mem.end, b->mem.start);
            return;
        }

        /*
         * "second->mem.end == b->mem.start" must be always true here,
         * that is the second buffer is just before the added buffer,
         * so expand the second buffer to the end of added buffer.
         */
        prev = second;
    }

    prev->mem.free = b->mem.end;
    prev->mem.end = b->mem.end;

    nxt_buf_free(c->mem_pool, b);
}


static void
nxt_conn_proxy_read(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *source, *sink;
    nxt_conn_proxy_t  *p;

    source = obj;
    p = data;

    nxt_debug(task, "conn proxy read fd:%d", source->socket.fd);

    if (!source->socket.closed) {
        sink = (source == p->client) ? p->peer : p->client;

        if (sink->socket.error == 0) {
            nxt_conn_read(task->thread->engine, source);
        }
    }
}


static const nxt_conn_state_t  nxt_conn_proxy_client_write_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_client_write_ready,
    .error_handler = nxt_conn_proxy_write_error,

    .timer_handler = nxt_conn_proxy_write_timeout,
    .timer_value = nxt_conn_proxy_timeout_value,
    .timer_data = offsetof(nxt_conn_proxy_t, client_write_timeout),
    .timer_autoreset = 1,
};


static void
nxt_conn_proxy_client_write_ready(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *client;
    nxt_conn_proxy_t  *p;

    client = obj;
    p = data;

    nxt_debug(task, "conn proxy client write ready fd:%d", client->socket.fd);

    nxt_conn_proxy_write_process(task, p, client, p->peer);
}


static const nxt_conn_state_t  nxt_conn_proxy_peer_write_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_peer_write_ready,
    .error_handler = nxt_conn_proxy_write_error,

    .timer_handler = nxt_conn_proxy_write_timeout,
    .timer_value = nxt_conn_proxy_timeout_value,
    .timer_data = offsetof(nxt_conn_proxy_t, peer_write_timeout),
    .timer_autoreset = 1,
};


static void
nxt_conn_proxy_peer_write_ready(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *peer;
    nxt_conn_proxy_t  *p;

    peer = obj;
    p = data;

    nxt_debug(task, "conn proxy peer write ready fd:%d", peer->socket.fd);

    nxt_conn_proxy_write_process(task, p, peer, p->client);
}


static void
nxt_conn_proxy_write_process(nxt_task_t *task, nxt_conn_proxy_t *p,
    nxt_conn_t *sink, nxt_conn_t *source)
{
    nxt_buf_t  *rb, *wb;

    while (sink->write != NULL) {

        wb = sink->write;

        if (nxt_buf_is_sync(wb)) {

            /* A sync buffer marks the end of stream. */

            sink->write = NULL;
            nxt_buf_free(sink->mem_pool, wb);
            nxt_conn_proxy_shutdown(task, p, source, sink);
            return;
        }

        if (wb->mem.start != wb->mem.pos) {

            /* Add a written part to a read chain. */

            rb = nxt_buf_mem_alloc(sink->mem_pool, 0, 0);
            if (rb == NULL) {
                /* An error completion. */
                nxt_conn_proxy_complete(task, p);
                return;
            }

            rb->mem.pos = wb->mem.start;
            rb->mem.free = wb->mem.start;
            rb->mem.start = wb->mem.start;
            rb->mem.end = wb->mem.pos;

            wb->mem.start = wb->mem.pos;

            nxt_conn_proxy_read_add(source, rb);
        }

        if (wb->mem.pos != wb->mem.free) {
            nxt_conn_write(task->thread->engine, sink);

            break;
        }

        sink->write = wb->next;
        nxt_buf_free(sink->mem_pool, wb);
    }

    nxt_work_queue_add(source->read_work_queue, nxt_conn_proxy_read,
                       task, source, source->socket.data);
}


static void
nxt_conn_proxy_read_add(nxt_conn_t *c, nxt_buf_t *b)
{
    nxt_buf_t  *first, *second;

    first = c->read;

    if (first == NULL) {
        c->read = b;
        return;
    }

    /*
     * A event conn proxy maintains a buffer per each direction.
     * The buffer is divided by read and write parts.  These parts are
     * linked in buffer chains.  There can be no more than two buffers
     * in read chain at any time, because an added buffer is coalesced
     * with the last buffer if possible.  The first and the second
     * buffers are also coalesced if possible.
     */

    second = first->next;

    if (second == NULL) {

        if (first->mem.start == b->mem.end) {
            /*
             * The added buffer is just before the first buffer, so expand
             * the first buffer to the beginning of the added buffer.
             */
            first->mem.pos = b->mem.start;
            first->mem.free = b->mem.start;
            first->mem.start = b->mem.start;

        } else if (first->mem.end == b->mem.start) {
            /*
             * The added buffer is just after the first buffer, so
             * expand the first buffer to the end of the added buffer.
             */
            first->mem.end = b->mem.end;

        } else {
            first->next = b;
            return;
        }

    } else {
        if (second->mem.end != b->mem.start) {
            nxt_thread_log_alert("event conn proxy read: second buffer end:%p "
                                 "is not equal to added buffer start:%p",
                                 second->mem.end, b->mem.start);
            return;
        }

        /*
         * The added buffer is just after the second buffer, so
         * expand the second buffer to the end of the added buffer.
         */
        second->mem.end = b->mem.end;

        if (first->mem.start == second->mem.end) {
            /*
             * The second buffer is just before the first buffer, so expand
             * the first buffer to the beginning of the second buffer.
             */
            first->mem.pos = second->mem.start;
            first->mem.free = second->mem.start;
            first->mem.start = second->mem.start;
            first->next = NULL;

            nxt_buf_free(c->mem_pool, second);
        }
    }

    nxt_buf_free(c->mem_pool, b);
}


static void
nxt_conn_proxy_close(nxt_task_t *task, void *obj, void *data)
{
    nxt_buf_t         *b;
    nxt_conn_t        *source, *sink;
    nxt_conn_proxy_t  *p;

    source = obj;
    p = data;

    nxt_debug(task, "conn proxy close fd:%d", source->socket.fd);

    sink = (source == p->client) ? p->peer : p->client;

    if (sink->write == NULL) {
        nxt_conn_proxy_shutdown(task, p, source, sink);
        return;
    }

    b = nxt_buf_sync_alloc(source->mem_pool, 0);
    if (b == NULL) {
        /* An error completion. */
        nxt_conn_proxy_complete(task, p);
        return;
    }

    nxt_buf_chain_add(&sink->write, b);
}


static void
nxt_conn_proxy_error(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *c;
    nxt_conn_proxy_t  *p;

    c = obj;
    p = data;

    nxt_debug(task, "conn proxy error fd:%d", c->socket.fd);

    nxt_conn_proxy_close(task, c, p);
}


static void
nxt_conn_proxy_read_timeout(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t   *c;
    nxt_timer_t  *timer;

    timer = obj;

    c = nxt_read_timer_conn(timer);
    c->socket.timedout = 1;
    c->socket.closed = 1;

    nxt_debug(task, "conn proxy read timeout fd:%d", c->socket.fd);

    nxt_conn_proxy_close(task, c, c->socket.data);
}


static void
nxt_conn_proxy_write_timeout(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t   *c;
    nxt_timer_t  *timer;

    timer = obj;

    c = nxt_write_timer_conn(timer);
    c->socket.timedout = 1;
    c->socket.closed = 1;

    nxt_debug(task, "conn proxy write timeout fd:%d", c->socket.fd);

    nxt_conn_proxy_close(task, c, c->socket.data);
}


static nxt_msec_t
nxt_conn_proxy_timeout_value(nxt_conn_t *c, uintptr_t data)
{
    nxt_msec_t        *timer;
    nxt_conn_proxy_t  *p;

    p = c->socket.data;

    timer = (nxt_msec_t *) ((char *) p + data);

    return *timer;
}


static void
nxt_conn_proxy_refused(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *peer;
    nxt_conn_proxy_t  *p;

    peer = obj;
    p = data;

    nxt_debug(task, "conn proxy refused fd:%d", peer->socket.fd);

    if (p->retries == 0) {
        /* An error completion. */
        nxt_conn_proxy_complete(task, p);
        return;
    }

    p->retries--;

    nxt_socket_close(task, peer->socket.fd);
    peer->socket.fd = -1;
    peer->socket.error = 0;

    p->delayed = 1;

    peer->write_timer.handler = nxt_conn_proxy_reconnect_handler;
    nxt_timer_add(task->thread->engine, &peer->write_timer,
                  p->reconnect_timeout);
}


static void
nxt_conn_proxy_reconnect_handler(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *peer;
    nxt_timer_t       *timer;
    nxt_conn_proxy_t  *p;

    timer = obj;

    nxt_debug(task, "conn proxy reconnect timer");

    peer = nxt_write_timer_conn(timer);
    p = peer->socket.data;

    if (p->client->socket.closed) {
        nxt_conn_proxy_complete(task, p);
        return;
    }

    p->delayed = 0;

    peer->write_state = &nxt_conn_proxy_peer_connect_state;
    /*
     * Peer read event: disabled.
     * Peer write event: waiting for connection with connect_timeout.
     */
    nxt_conn_connect(task->thread->engine, peer);
}


static void
nxt_conn_proxy_shutdown(nxt_task_t *task, nxt_conn_proxy_t *p,
    nxt_conn_t *source, nxt_conn_t *sink)
{
    nxt_buf_t  *b;

    nxt_debug(source->socket.task,
              "conn proxy shutdown source fd:%d cl:%d err:%d",
              source->socket.fd, source->socket.closed, source->socket.error);

    nxt_debug(sink->socket.task,
              "conn proxy shutdown sink fd:%d cl:%d err:%d",
              sink->socket.fd, sink->socket.closed, sink->socket.error);

    if (!p->connected || p->delayed) {
        nxt_conn_proxy_complete(task, p);
        return;
    }

    if (sink->socket.error == 0 && !sink->socket.closed) {
        sink->socket.shutdown = 1;
        nxt_socket_shutdown(task, sink->socket.fd, SHUT_WR);
    }

    if (sink->socket.error != 0
        || (sink->socket.closed && source->write == NULL))
    {
        /* The opposite direction also has been already closed. */
        nxt_conn_proxy_complete(task, p);
        return;
    }

    nxt_debug(source->socket.task, "free source buffer");

    /* Free the direction's buffer. */
    b = (source == p->client) ? p->client_buffer : p->peer_buffer;
    nxt_mem_free(source->mem_pool, b);
}


static void
nxt_conn_proxy_read_error(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *c;
    nxt_conn_proxy_t  *p;

    c = obj;
    p = data;

    nxt_debug(task, "conn proxy read error fd:%d", c->socket.fd);

    nxt_conn_proxy_close(task, c, p);
}


static void
nxt_conn_proxy_write_error(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_t        *source, *sink;
    nxt_conn_proxy_t  *p;

    sink = obj;
    p = data;

    nxt_debug(task, "conn proxy write error fd:%d", sink->socket.fd);

    /* Clear data for the direction sink. */
    sink->write = NULL;

    /* Block the direction source. */
    source = (sink == p->client) ? p->peer : p->client;
    nxt_fd_event_block_read(task->thread->engine, &source->socket);

    if (source->write == NULL) {
        /*
         * There is no data for the opposite direction and
         * the next read from the sink will most probably fail.
         */
        nxt_conn_proxy_complete(task, p);
    }
}


static const nxt_conn_state_t  nxt_conn_proxy_close_state
    nxt_aligned(64) =
{
    .ready_handler = nxt_conn_proxy_completion,
};


static void
nxt_conn_proxy_complete(nxt_task_t *task, nxt_conn_proxy_t *p)
{
    nxt_event_engine_t  *engine;

    engine = task->thread->engine;

    nxt_debug(p->client->socket.task, "conn proxy complete %d:%d",
              p->client->socket.fd, p->peer->socket.fd);

    if (p->delayed) {
        p->delayed = 0;
        nxt_queue_remove(&p->peer->link);
    }

    if (p->client->socket.fd != -1) {
        p->retain = 1;
        p->client->write_state = &nxt_conn_proxy_close_state;
        nxt_conn_close(engine, p->client);
    }

    if (p->peer->socket.fd != -1) {
        p->retain++;
        p->peer->write_state = &nxt_conn_proxy_close_state;
        nxt_conn_close(engine, p->peer);
    }
}


static void
nxt_conn_proxy_completion(nxt_task_t *task, void *obj, void *data)
{
    nxt_conn_proxy_t  *p;

    p = data;

    nxt_debug(p->client->socket.task, "conn proxy completion %d:%d:%d",
              p->retain, p->client->socket.fd, p->peer->socket.fd);

    p->retain--;

    if (p->retain == 0) {
        nxt_mem_free(p->client->mem_pool, p->client_buffer);
        nxt_mem_free(p->client->mem_pool, p->peer_buffer);

        p->completion_handler(task, p, NULL);
    }
}