| 1 | package Nginx::Redis; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | no warnings 'uninitialized'; | ||
| 6 | use bytes; | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | our @EXPORT = qw(ngx_redis ngx_redis_client ngx_redis_request); | ||
| 11 | our $VERSION = '1.1.12.4'; | ||
| 12 | |||
| 13 | use Nginx; | ||
| 14 | use Redis::Parser::XS; | ||
| 15 | |||
| 16 | sub CRLF { "\x0d\x0a" } | ||
| 17 | sub ngx_redis_client ($$$;&); | ||
| 18 | sub ngx_redis ($$&); | ||
| 19 | |||
| 20 | our $AUTH; | ||
| 21 | our $TIMEOUT = 5; | ||
| 22 | my %CLIENTS; | ||
| 23 | |||
| 24 | |||
| 25 | sub ngx_redis_client ($$$;&) { | ||
| 26 | my ($ip, $port, $timeout, $error_cb) = @_; | ||
| 27 | |||
| 28 | my ($error, # push needs it | ||
| 29 | $c, @output, @active, @queue); | ||
| 30 | my $buf = ''; | ||
| 31 | |||
| 32 | |||
| 33 | my $push = sub { | ||
| 34 | |||
| 35 | return 0 # don't do anything if | ||
| 36 | if @active != 0 || # sending something right now or | ||
| 37 | @queue == 0 || # nothing to send or | ||
| 38 | !defined $c; # not connected yet | ||
| 39 | |||
| 40 | @active = @queue; | ||
| 41 | @queue = (); | ||
| 42 | |||
| 43 | &$error (), | ||
| 44 | return 0 # handle error and return if | ||
| 45 | if $c == 0; # connection marked as failed | ||
| 46 | |||
| 47 | $buf = # | ||
| 48 | join ('', # redis packet: | ||
| 49 | map { # | ||
| 50 | '*' . scalar (@{$_->[0]}) . CRLF . # *1 CRLF | ||
| 51 | join ('', # | ||
| 52 | map { # | ||
| 53 | '$' . length ($_) . CRLF . # $3 CRLF | ||
| 54 | $_ . CRLF # foo CRLF | ||
| 55 | } @{$_->[0]} # ... | ||
| 56 | ) # | ||
| 57 | } @active | ||
| 58 | ); | ||
| 59 | |||
| 60 | # tell caller we need to start | ||
| 61 | return 1; # writing flow (ngx_write/NGX_WRITE) | ||
| 62 | }; | ||
| 63 | |||
| 64 | |||
| 65 | $error = sub { | ||
| 66 | |||
| 67 | $c = 0; # marking connection as failed | ||
| 68 | |||
| 69 | for my $i (0 .. $#active) { | ||
| 70 | my $cb = $active [$i]->[1]; | ||
| 71 | &$cb ( $output [$i] ); | ||
| 72 | } | ||
| 73 | |||
| 74 | ngx_log_error $!, "redis error" | ||
| 75 | if @active > 0; | ||
| 76 | |||
| 77 | @output = (); | ||
| 78 | @active = (); | ||
| 79 | |||
| 80 | foreach (@queue) { | ||
| 81 | my $cb = $_->[1]; | ||
| 82 | &$cb (); | ||
| 83 | } | ||
| 84 | |||
| 85 | @queue = (); | ||
| 86 | |||
| 87 | &$error_cb () | ||
| 88 | if $error_cb; | ||
| 89 | }; | ||
| 90 | |||
| 91 | |||
| 92 | my $enqueue = sub { | ||
| 93 | push @queue, \@_; | ||
| 94 | |||
| 95 | &$push () && | ||
| 96 | ngx_write ($c); | ||
| 97 | }; | ||
| 98 | |||
| 99 | |||
| 100 | ngx_connector $ip, $port, $timeout, sub { | ||
| 101 | |||
| 102 | &$error (), | ||
| 103 | return NGX_CLOSE | ||
| 104 | if $!; | ||
| 105 | |||
| 106 | $c = shift; | ||
| 107 | |||
| 108 | |||
| 109 | ngx_writer $c, $buf, $timeout, sub { | ||
| 110 | |||
| 111 | &$error (), | ||
| 112 | return NGX_CLOSE | ||
| 113 | if $!; | ||
| 114 | |||
| 115 | $buf = ''; | ||
| 116 | |||
| 117 | return NGX_READ; | ||
| 118 | }; | ||
| 119 | |||
| 120 | |||
| 121 | ngx_reader $c, $buf, 0, 0, $timeout, sub { | ||
| 122 | |||
| 123 | &$error (), | ||
| 124 | return NGX_CLOSE | ||
| 125 | if $!; | ||
| 126 | |||
| 127 | my $len = parse_redis $buf, \@output; | ||
| 128 | |||
| 129 | &$error (), | ||
| 130 | return NGX_CLOSE | ||
| 131 | if !defined $len; | ||
| 132 | |||
| 133 | $buf = substr $buf, $len; | ||
| 134 | |||
| 135 | return NGX_READ | ||
| 136 | if @output != @active; | ||
| 137 | |||
| 138 | $buf = ''; | ||
| 139 | |||
| 140 | for my $i (0 .. $#active) { | ||
| 141 | my $cb = $active [$i]->[1]; | ||
| 142 | &$cb ($output [$i]); | ||
| 143 | } | ||
| 144 | |||
| 145 | @output = (); | ||
| 146 | @active = (); | ||
| 147 | |||
| 148 | &$push () && | ||
| 149 | return NGX_WRITE; | ||
| 150 | |||
| 151 | return NGX_READ; | ||
| 152 | }; | ||
| 153 | |||
| 154 | |||
| 155 | &$push () && | ||
| 156 | return NGX_WRITE; | ||
| 157 | |||
| 158 | return NGX_NOOP; | ||
| 159 | }; | ||
| 160 | |||
| 161 | |||
| 162 | return $enqueue; | ||
| 163 | } | ||
| 164 | |||
| 165 | |||
| 166 | sub ngx_redis_request { | ||
| 167 | my $enqueue = shift; | ||
| 168 | |||
| 169 | &$enqueue; | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | sub ngx_redis ($$&) { | ||
| 174 | my $dest = shift; | ||
| 175 | my $enqueue = $CLIENTS{$dest}; | ||
| 176 | |||
| 177 | &$enqueue, # enqueue request | ||
| 178 | return # and return | ||
| 179 | if defined $enqueue; # since we already connected | ||
| 180 | |||
| 181 | my ($ip, $port, $auth, $timeout) = split (':', $dest); | ||
| 182 | |||
| 183 | $ip = '127.0.0.1' unless $ip; | ||
| 184 | $port = 6379 unless $port; | ||
| 185 | $auth = $AUTH unless defined $auth; | ||
| 186 | $timeout = $TIMEOUT unless $timeout; | ||
| 187 | |||
| 188 | $enqueue = ngx_redis_client $ip, $port, $timeout, sub { | ||
| 189 | delete $CLIENTS{$dest}; # deleting on error to force reconnect | ||
| 190 | }; | ||
| 191 | |||
| 192 | $CLIENTS{$dest} = $enqueue; | ||
| 193 | |||
| 194 | if ($auth) { | ||
| 195 | &$enqueue (['AUTH', $auth ], sub { | ||
| 196 | my ($reply) = @_; | ||
| 197 | |||
| 198 | ngx_log_error 0, "Cannot authorize on redis server at $ip:$port" | ||
| 199 | if $reply->[0] ne '+'; | ||
| 200 | }); | ||
| 201 | } | ||
| 202 | |||
| 203 | &$enqueue; | ||
| 204 | } | ||
| 205 | |||
| 206 | |||
| 207 | 1; | ||
| 208 | __END__ |