| 1 | package App::derived::Plugin::Memcached; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use parent qw/App::derived::Plugin/; | ||
| 6 | use Class::Accessor::Lite ( | ||
| 7 | ro => [qw/host port timeout/], | ||
| 8 | ); | ||
| 9 | use IO::Socket::INET; | ||
| 10 | use POSIX qw(EINTR EAGAIN EWOULDBLOCK :sys_wait_h); | ||
| 11 | use Socket qw(IPPROTO_TCP TCP_NODELAY); | ||
| 12 | use Log::Minimal; | ||
| 13 | |||
| 14 | our $MAX_REQUEST_SIZE = 131072; | ||
| 15 | our $CRLF = "\x0d\x0a"; | ||
| 16 | our $DELIMITER = "\x20"; | ||
| 17 | |||
| 18 | sub init { | ||
| 19 | my $self = shift; | ||
| 20 | |||
| 21 | $self->{host} = '0' unless $self->{host}; | ||
| 22 | $self->{port} = 12306 unless $self->{port}; | ||
| 23 | $self->{timeout} = 10 unless $self->{timeout}; | ||
| 24 | |||
| 25 | my $localaddr = $self->{host} .':'. $self->{port}; | ||
| 26 | my $sock = IO::Socket::INET->new( | ||
| 27 | Listen => SOMAXCONN, | ||
| 28 | LocalAddr => $localaddr, | ||
| 29 | Proto => 'tcp', | ||
| 30 | (($^O eq 'MSWin32') ? () : (ReuseAddr => 1)), | ||
| 31 | ) or die "failed to listen to port $localaddr: $!"; | ||
| 32 | infof('Memcached server starts listen on %s',$localaddr); | ||
| 33 | $self->add_worker( | ||
| 34 | 'memcached_server_'.$localaddr, | ||
| 35 | sub { | ||
| 36 | $0 = "$0 memcached_server"; | ||
| 37 | $self->server($sock); | ||
| 38 | } | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | |||
| 42 | sub server { | ||
| 43 | my $self = shift; | ||
| 44 | my $sock = shift; | ||
| 45 | |||
| 46 | local $SIG{CHLD} = sub { | ||
| 47 | 1 until (-1 == waitpid(-1, WNOHANG)); | ||
| 48 | }; | ||
| 49 | |||
| 50 | while(1) { | ||
| 51 | local $SIG{PIPE} = 'IGNORE'; | ||
| 52 | if ( my $conn = $sock->accept ) { | ||
| 53 | debugf("[server] new connection from %s:%s", $conn->peerhost, $conn->peerport); | ||
| 54 | $conn->blocking(0) | ||
| 55 | or die "failed to set socket to nonblocking mode:$!"; | ||
| 56 | $conn->setsockopt(IPPROTO_TCP, TCP_NODELAY, 1) | ||
| 57 | or die "setsockopt(TCP_NODELAY) failed:$!"; | ||
| 58 | my $pid = fork(); | ||
| 59 | die "cannot fork: $!" unless defined $pid; | ||
| 60 | if ( $pid == 0 ) { | ||
| 61 | $self->handle_connection($conn); | ||
| 62 | debugf("[server] close connection: %s:%s", $conn->peerhost, $conn->peerport); | ||
| 63 | $conn->close; | ||
| 64 | exit; | ||
| 65 | } | ||
| 66 | $conn->close; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | } | ||
| 71 | |||
| 72 | sub handle_connection { | ||
| 73 | my ($self, $conn) = @_; | ||
| 74 | |||
| 75 | my $buf = ''; | ||
| 76 | my $req = +{}; | ||
| 77 | |||
| 78 | while (1) { | ||
| 79 | my $rlen = read_timeout( | ||
| 80 | $conn, \$buf, $MAX_REQUEST_SIZE - length($buf), length($buf), $self->{timeout}, | ||
| 81 | ) or last; | ||
| 82 | if ( parse_read_buffer($buf, $req ) ) { | ||
| 83 | $buf = ''; | ||
| 84 | if ( $req->{cmd} eq 'get' ) { | ||
| 85 | my @keys = split /\x20+/, $req->{keys}; | ||
| 86 | my $result; | ||
| 87 | debugf("[server] request get => %s from %s:%s", $req->{keys}, $conn->peerhost, $conn->peerport); | ||
| 88 | for my $key ( @keys ) { | ||
| 89 | my $mode = ''; | ||
| 90 | if ( $key =~ m!:full$! ) { | ||
| 91 | $mode = 'full'; | ||
| 92 | $key =~ s!:full$!!; | ||
| 93 | } | ||
| 94 | elsif ( $key =~ m!:latest$! ) { | ||
| 95 | $mode = 'latest'; | ||
| 96 | $key =~ s!:latest$!!; | ||
| 97 | } | ||
| 98 | if ( $self->exists_service($key) ) { | ||
| 99 | my $ref = $self->service_stats($key); | ||
| 100 | if ( $mode eq 'full' ) { | ||
| 101 | my $val = $self->json->encode($ref); | ||
| 102 | $result .= join $DELIMITER, "VALUE", $key, 0, length($val); | ||
| 103 | $result .= $CRLF . $val . $CRLF; | ||
| 104 | } | ||
| 105 | elsif ( $mode eq 'latest' ) { | ||
| 106 | if ( defined $ref->{latest} ) { | ||
| 107 | my $val = $ref->{latest}; | ||
| 108 | $result .= join $DELIMITER, "VALUE", $key, 0, length($val); | ||
| 109 | $result .= $CRLF . $val . $CRLF; | ||
| 110 | } | ||
| 111 | } | ||
| 112 | else { | ||
| 113 | if ( defined $ref->{persec} ) { | ||
| 114 | my $val = $ref->{persec}; | ||
| 115 | $result .= join $DELIMITER, "VALUE", $key, 0, length($val); | ||
| 116 | $result .= $CRLF . $val . $CRLF; | ||
| 117 | } | ||
| 118 | } | ||
| 119 | } | ||
| 120 | } | ||
| 121 | $result .= "END" . $CRLF; | ||
| 122 | write_all( $conn, $result, $self->{timeout} ); | ||
| 123 | } | ||
| 124 | elsif ( $req->{cmd} eq 'version' ) { | ||
| 125 | write_all( $conn, "VERSION $App::derived::VERSION$CRLF", $self->{timeout} ); | ||
| 126 | } | ||
| 127 | elsif ( $req->{cmd} eq 'quit' ) { | ||
| 128 | #do nothing | ||
| 129 | last; | ||
| 130 | } | ||
| 131 | else { | ||
| 132 | write_all( $conn, "ERROR".$CRLF, $self->{timeout} ); | ||
| 133 | } | ||
| 134 | } | ||
| 135 | } | ||
| 136 | return; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub parse_read_buffer { | ||
| 140 | my ($buf, $ret) = @_; | ||
| 141 | if ( $buf =~ /$CRLF$/o ) { | ||
| 142 | my ($req_line) = split /$CRLF/, $buf; | ||
| 143 | ($ret->{cmd}, $ret->{keys}) = split /$DELIMITER/o, $req_line, 2; | ||
| 144 | $ret->{keys} ||= ''; | ||
| 145 | return 1; | ||
| 146 | } | ||
| 147 | return; | ||
| 148 | } | ||
| 149 | |||
| 150 | # returns (positive) number of bytes read, or undef if the socket is to be closed | ||
| 151 | sub read_timeout { | ||
| 152 | my ($sock, $buf, $len, $off, $timeout) = @_; | ||
| 153 | do_io(undef, $sock, $buf, $len, $off, $timeout); | ||
| 154 | } | ||
| 155 | |||
| 156 | # returns (positive) number of bytes written, or undef if the socket is to be closed | ||
| 157 | sub write_timeout { | ||
| 158 | my ($sock, $buf, $len, $off, $timeout) = @_; | ||
| 159 | do_io(1, $sock, $buf, $len, $off, $timeout); | ||
| 160 | } | ||
| 161 | |||
| 162 | # writes all data in buf and returns number of bytes written or undef if failed | ||
| 163 | sub write_all { | ||
| 164 | my ($sock, $buf, $timeout) = @_; | ||
| 165 | my $off = 0; | ||
| 166 | while (my $len = length($buf) - $off) { | ||
| 167 | my $ret = write_timeout($sock, $buf, $len, $off, $timeout) | ||
| 168 | or return; | ||
| 169 | $off += $ret; | ||
| 170 | } | ||
| 171 | return length $buf; | ||
| 172 | } | ||
| 173 | |||
| 174 | # returns value returned by $cb, or undef on timeout or network error | ||
| 175 | sub do_io { | ||
| 176 | my ($is_write, $sock, $buf, $len, $off, $timeout) = @_; | ||
| 177 | my $ret; | ||
| 178 | DO_READWRITE: | ||
| 179 | # try to do the IO | ||
| 180 | if ($is_write) { | ||
| 181 | $ret = syswrite $sock, $buf, $len, $off | ||
| 182 | and return $ret; | ||
| 183 | } else { | ||
| 184 | $ret = sysread $sock, $$buf, $len, $off | ||
| 185 | and return $ret; | ||
| 186 | } | ||
| 187 | unless ((! defined($ret) | ||
| 188 | && ($! == EINTR || $! == EAGAIN || $! == EWOULDBLOCK))) { | ||
| 189 | return; | ||
| 190 | } | ||
| 191 | # wait for data | ||
| 192 | DO_SELECT: | ||
| 193 | while (1) { | ||
| 194 | my ($rfd, $wfd); | ||
| 195 | my $efd = ''; | ||
| 196 | vec($efd, fileno($sock), 1) = 1; | ||
| 197 | if ($is_write) { | ||
| 198 | ($rfd, $wfd) = ('', $efd); | ||
| 199 | } else { | ||
| 200 | ($rfd, $wfd) = ($efd, ''); | ||
| 201 | } | ||
| 202 | my $start_at = time; | ||
| 203 | my $nfound = select($rfd, $wfd, $efd, $timeout); | ||
| 204 | $timeout -= (time - $start_at); | ||
| 205 | last if $nfound; | ||
| 206 | return if $timeout <= 0; | ||
| 207 | } | ||
| 208 | goto DO_READWRITE; | ||
| 209 | } | ||
| 210 | |||
| 211 | 1; | ||
| 212 | |||
| 213 | |||
| 214 | __END__ |