Mercurial > hg > ngx_http_upstream_keepalive
comparison t/stale.t @ 21:9a4ee6fe1c6d
Keepalive: avoid closing upstream connection on stale events.
Under some conditions it's possible that stale events occur, i.e. read
event handler called for just saved upstream connection without any data
available for read. We shouldn't close upstream connection in such
situation.
Reported by: Martin Fjordvald
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Tue, 14 Sep 2010 04:01:14 +0400 |
parents | |
children | 2ee28064a04a |
comparison
equal
deleted
inserted
replaced
20:8d8eaaf07663 | 21:9a4ee6fe1c6d |
---|---|
1 #!/usr/bin/perl | |
2 | |
3 # (C) Maxim Dounin | |
4 | |
5 # Test for stale events handling in upstream keepalive. | |
6 | |
7 ############################################################################### | |
8 | |
9 use warnings; | |
10 use strict; | |
11 | |
12 use Test::More; | |
13 use Test::Nginx; | |
14 | |
15 ############################################################################### | |
16 | |
17 select STDERR; $| = 1; | |
18 select STDOUT; $| = 1; | |
19 | |
20 eval { require Cache::Memcached; }; | |
21 plan(skip_all => 'Cache::Memcached not installed') if $@; | |
22 | |
23 my $t = Test::Nginx->new()->has('rewrite')->has_daemon('memcached')->plan(1) | |
24 ->write_file_expand('nginx.conf', <<'EOF'); | |
25 | |
26 %%TEST_GLOBALS%% | |
27 | |
28 daemon off; | |
29 | |
30 worker_processes 2; | |
31 | |
32 events { | |
33 } | |
34 | |
35 http { | |
36 %%TEST_GLOBALS_HTTP%% | |
37 | |
38 upstream memd { | |
39 server 127.0.0.1:8081; | |
40 keepalive 1; | |
41 } | |
42 | |
43 server { | |
44 listen 127.0.0.1:8080 sndbuf=32k; | |
45 server_name localhost; | |
46 | |
47 location / { | |
48 set $memcached_key $uri; | |
49 memcached_pass memd; | |
50 } | |
51 } | |
52 } | |
53 | |
54 EOF | |
55 | |
56 my $memhelp = `memcached -h`; | |
57 my @memopts1 = (); | |
58 | |
59 if ($memhelp =~ /repcached/) { | |
60 # repcached patches adds additional listen socket memcached | |
61 # that should be different too | |
62 | |
63 push @memopts1, '-X', '8091'; | |
64 } | |
65 if ($memhelp =~ /-U/) { | |
66 # UDP ports no longer off by default in memcached 1.2.7+ | |
67 | |
68 push @memopts1, '-U', '0'; | |
69 } | |
70 | |
71 $t->run_daemon('memcached', '-l', '127.0.0.1', '-p', '8081', @memopts1); | |
72 | |
73 $t->run(); | |
74 | |
75 $t->waitforsocket('127.0.0.1:8081') | |
76 or die "Unable to start memcached"; | |
77 | |
78 ############################################################################### | |
79 | |
80 my $memd1 = Cache::Memcached->new(servers => [ '127.0.0.1:8081' ]); | |
81 | |
82 # It's possible that stale events occur, i.e. read event handler called | |
83 # for just saved upstream connection without any data available for | |
84 # read. We shouldn't close upstream connection in such situation. | |
85 # | |
86 # This happens due to reading from upstream connection on downstream write | |
87 # events. More likely to happen with multiple workers due to use of posted | |
88 # events. | |
89 # | |
90 # Stale event may only happen if reading response from upstream requires | |
91 # entering event loop, i.e. response should be big enough. On the other | |
92 # hand, it is less likely to occur with full client's connection output | |
93 # buffer. | |
94 # | |
95 # We use here 2 workers, 20k response and set output buffer on clients | |
96 # connection to 32k. This allows more or less reliably reproduce stale | |
97 # events at least on FreeBSD testbed here. | |
98 | |
99 $memd1->set('/big', 'X' x 20480); | |
100 | |
101 my $total = $memd1->stats()->{total}->{total_connections}; | |
102 | |
103 for (1 .. 100) { | |
104 http_get('/big'); | |
105 } | |
106 | |
107 cmp_ok($memd1->stats()->{total}->{total_connections}, '<=', $total + 2, | |
108 'only one connection per worker used'); | |
109 | |
110 ############################################################################### |