Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Various hacks to make it work. No joy yet.

  • Loading branch information...
commit ef947d805a7555f00c3b7fe81f47d61851477db0 1 parent 18f4445
Cosimo Streppone authored

Showing 1 changed file with 74 additions and 26 deletions. Show diff stats Hide diff stats

  1. +74 26 lib/Cache/Memcached.pm
100 lib/Cache/Memcached.pm
@@ -46,6 +46,12 @@ has &.cb_connect_fail is rw;
46 46 has @!buck2sock is rw;
47 47
48 48 submethod BUILD {
  49 +
  50 + # TODO understand why @!servers is empty here
  51 + if ! @!servers {
  52 + @!servers = "127.0.0.1:11211";
  53 + }
  54 +
49 55 say "Setting servers: ", @!servers;
50 56 self.set_servers(@!servers);
51 57 }
@@ -337,7 +343,7 @@ sub _connect_sock { # sock, sin, timeout
337 343 }
338 344 =end pod
339 345
340   -method _connect_sock ($sock, $sin, $timeout = 0.25) {
  346 +sub _connect_sock ($sock, $sin, $timeout = 0.25) {
341 347
342 348 # make the socket non-blocking from now on,
343 349 # except if someone wants 0 timeout, meaning
@@ -350,10 +356,18 @@ method _connect_sock ($sock, $sin, $timeout = 0.25) {
350 356 my $host = 'localhost';
351 357 my $port = 11211;
352 358
353   - my $sock_obj = IO::Socket::INET.new(:$host, :$port);
  359 + my $sock_obj = IO::Socket::INET.new;
  360 + my $ret = $sock_obj.open($host, $port);
354 361
355 362 # TODO non-blocking sockets support yanked for now
356   - return $sock_obj;
  363 +
  364 + if $ret {
  365 + say "Connected to localhost:11211 (hardcoded)...\n";
  366 + return $sock_obj;
  367 + }
  368 +
  369 + return;
  370 +
357 371 }
358 372
359 373 =begin pod
@@ -454,8 +468,7 @@ method sock_to_host ($host) {
454 468 return %cache_sock{$host};
455 469 }
456 470
457   - my $now = time();
458   -
  471 + my $now = time;
459 472 my $ip;
460 473 my $port;
461 474
@@ -463,7 +476,7 @@ method sock_to_host ($host) {
463 476 $ip = $0;
464 477 $port = $1;
465 478 # Get rid of optional IPv6 brackets
466   - $ip ~~ s/<\[\]>//g if $ip.defined;
  479 + $ip ~~ s:g [ \[ | \] ] = '' if $ip.defined;
467 480 }
468 481
469 482 if %host_dead{$host} && %host_dead{$host} > $now {
@@ -477,7 +490,7 @@ method sock_to_host ($host) {
477 490 # TODO connect fail callback
478 491 #my &cb = &!cb_connect_fail;
479 492 #if &cb { &cb->() }
480   - return _dead_sock($sock, Mu, 20 + 10.rand.Int);
  493 + return self._dead_sock($sock, Mu, 20 + 10.rand.Int);
481 494 }
482 495
483 496 %sock_map{$sock} = $host;
@@ -660,15 +673,16 @@ sub _write_and_read {
660 673 # writes a line, then reads result. by default stops reading after a
661 674 # single line, but caller can override the $check_complete subref,
662 675 # which gets passed a scalarref of buffer read thus far.
663   -method _write_and_read ($sock, $line, $check_complete) {
  676 +method _write_and_read ($sock, $command, $check_complete = Mu) {
664 677
665 678 my $res;
666 679 my $ret = Mu;
667 680 my $offset = 0;
668   -
669   - &check_complete //= sub ($ret) {
670   - return $ret.rindex("\r\n") + 2 == $ret.chars;
671   - };
  681 + my $line = $command;
  682 +
  683 + #$check_complete //= sub ($ret) {
  684 + # return ($ret.rindex("\x0D\x0A") + 2) == $ret.chars;
  685 + #};
672 686
673 687 # state: 0 - writing, 1 - reading, 2 - done
674 688 my $state = 0;
@@ -682,12 +696,21 @@ method _write_and_read ($sock, $line, $check_complete) {
682 696 }
683 697
684 698 my $to_send = $line.chars;
685   -
  699 +
  700 + say "Chars to send: $to_send";
  701 +
  702 +=begin oldpart
686 703 while $to_send > 0 {
  704 +
  705 + say "Sending [$line] ...";
  706 +
687 707 $res = $sock.send($line);
  708 + say "Send result: $res";
  709 +
688 710 last unless $res.defined;
  711 +
689 712 if $res == 0 {
690   - $._close_sock($sock);
  713 + self._close_sock($sock);
691 714 return;
692 715 }
693 716 $to_send -= $res;
@@ -699,20 +722,43 @@ method _write_and_read ($sock, $line, $check_complete) {
699 722 $line = $line.substr($res); # delete the part we sent
700 723 }
701 724 }
  725 +=end oldpart
702 726
703   - $res = $sock.recv();
  727 + if $to_send > 0 {
  728 + my $sent = $sock.send($line);
  729 + if $sent == 0 {
  730 + self._close_sock($sock);
  731 + return;
  732 + }
  733 + $to_send -= $sent;
  734 + if $to_send == 0 {
  735 + $state = 1;
  736 + }
  737 + else {
  738 + $line = $line.substr($sent);
  739 + }
  740 + }
  741 +
  742 + say "Receiving from socket";
  743 +
  744 + $ret = $sock.recv();
  745 + #$ret = "";
  746 + #while (my $c = $sock.recv(1)) {
  747 + # $ret ~= $c;
  748 + #}
704 749
705   - next unless $res.defined;
706   - if $res.chars == 0 {
707   - $._close_sock($sock);
708   - return;
  750 + say "Got from socket (recv=" ~ $ret.perl ~ ")";
  751 +
  752 + if $ret ~~ m/\r\n$/ {
  753 + say "Got a terminator (\\r\\n)";
  754 + $state = 2;
709 755 }
710   - $offset += $res;
711   - $state = 2 if &check_complete.($ret);
  756 +
712 757 }
713 758
  759 + # Improperly finished
714 760 unless $state == 2 {
715   - $._dead_sock($sock); # improperly finished
  761 + self._dead_sock($sock);
716 762 return;
717 763 }
718 764
@@ -961,13 +1007,14 @@ method get ($key) {
961 1007 my $sock = $.get_sock($key);
962 1008 say "get(): socket '$sock'";
963 1009
964   - my $full_key = $!namespace ~ $key;
  1010 + my $namespace = $!namespace // "";
  1011 + my $full_key = $namespace ~ $key;
965 1012 say "get(): full key '$full_key'";
966 1013
967 1014 my $get_cmd = "get $full_key\r\n";
968 1015 say "get(): command '$get_cmd'";
969 1016
970   - my @res = $.run_command($sock, $get_cmd);
  1017 + my @res = self.run_command($sock, $get_cmd);
971 1018
972 1019 %!stats<get>++;
973 1020
@@ -1284,12 +1331,13 @@ method run_command ($sock, $cmd) {
1284 1331
1285 1332 return unless $sock;
1286 1333
1287   - my $ret;
  1334 + my $ret = "";
1288 1335 my $line = $cmd;
1289 1336
1290 1337 while (my $res = self._write_and_read($sock, $line)) {
1291   - $line = Mu;
  1338 + $line = "";
1292 1339 $ret ~= $res;
  1340 + say "Received [$res] total [$ret]";
1293 1341 last if $ret ~~ /[ OK | END | ERROR ] \r\n $/;
1294 1342 }
1295 1343

0 comments on commit ef947d8

Please sign in to comment.
Something went wrong with that request. Please try again.