Skip to content

Commit

Permalink
Allow the binding protocol to be specified.
Browse files Browse the repository at this point in the history
Instead of always binding as autonegotiate, allow the user to specify
which protocol the server will run.
  • Loading branch information
dustin authored and Trond Norbye committed May 13, 2009
1 parent fcc2c98 commit a155b04
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 3 deletions.
6 changes: 6 additions & 0 deletions doc/memcached.1
Expand Up @@ -118,6 +118,12 @@ Try to use large memory pages (if available). Increasing the memory page size
could reduce the number of TLB misses and improve the performance. In order to
get large pages from the OS, memcached will allocate the total item-cache in
one large chunk. Only available if supported on your OS.
.TP
.B \-B <proto>
Specify the binding protocol to use. By default, the server will
autonegotiate client connections. By using this option, you can
specify the protocol clients must speak. Possible options are "auto"
(the default, autonegotiation behavior), "ascii" and "binary".
.br
.SH LICENSE
The memcached daemon is copyright Danga Interactive and is distributed under
Expand Down
20 changes: 19 additions & 1 deletion memcached.c
Expand Up @@ -192,6 +192,7 @@ static void settings_init(void) {
settings.detail_enabled = 0;
settings.reqs_per_event = 20;
settings.backlog = 1024;
settings.binding_protocol = negotiating_prot;
}

/*
Expand Down Expand Up @@ -363,7 +364,7 @@ conn *conn_new(const int sfd, enum conn_states init_state,
}

c->transport = transport;
c->protocol = negotiating_prot;
c->protocol = settings.binding_protocol;

/* unix socket mode doesn't need this, so zeroed out. but why
* is this done for every command? presumably for UDP
Expand Down Expand Up @@ -2136,6 +2137,8 @@ static void process_stat_settings(ADD_STAT add_stats, void *c) {
APPEND_STAT("reqs_per_event", "%d", settings.reqs_per_event);
APPEND_STAT("cas_enabled", "%s", settings.use_cas ? "yes" : "no");
APPEND_STAT("tcp_backlog", "%d", settings.backlog);
APPEND_STAT("binding_protocol", "%s",
prot_text(settings.binding_protocol));
}

static void process_stat(conn *c, token_t *tokens, const size_t ntokens) {
Expand Down Expand Up @@ -3721,6 +3724,7 @@ static void usage(void) {
" to prevent starvation. default 20\n");
printf("-C Disable use of CAS\n");
printf("-b Set the backlog queue limit (default 1024)\n");
printf("-B Binding protocol - one of ascii, binary, or auto (default)\n");
return;
}

Expand Down Expand Up @@ -3930,6 +3934,7 @@ int main (int argc, char **argv) {
"R:" /* max requests per event */
"C" /* Disable use of CAS */
"b:" /* backlog queue limit */
"B:" /* Binding protocol */
))) {
switch (c) {
case 'a':
Expand Down Expand Up @@ -4031,6 +4036,19 @@ int main (int argc, char **argv) {
case 'b' :
settings.backlog = atoi(optarg);
break;
case 'B':
if (strcmp(optarg, "auto") == 0) {
settings.binding_protocol = negotiating_prot;
} else if (strcmp(optarg, "binary") == 0) {
settings.binding_protocol = binary_prot;
} else if (strcmp(optarg, "ascii") == 0) {
settings.binding_protocol = ascii_prot;
} else {
fprintf(stderr, "Invalid value for binding protocol: %s\n"
" -- should be one of auto, binary, or ascii\n", optarg);
exit(EX_USAGE);
}
break;
default:
fprintf(stderr, "Illegal argument \"%c\"\n", c);
return 1;
Expand Down
1 change: 1 addition & 0 deletions memcached.h
Expand Up @@ -256,6 +256,7 @@ struct settings {
int reqs_per_event; /* Maximum number of io to process on each
io-event. */
bool use_cas;
enum protocol binding_protocol;
int backlog;
};

Expand Down
23 changes: 22 additions & 1 deletion t/00-startup.t
@@ -1,7 +1,7 @@
#!/usr/bin/perl

use strict;
use Test::More tests => 8;
use Test::More tests => 14;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;
Expand Down Expand Up @@ -35,3 +35,24 @@ eval {
is('8675', $stats->{'tcp_backlog'});
};
is($@, '', "-b works");

foreach my $val ('auto', 'ascii') {
eval {
my $server = new_memcached("-B $val");
my $stats = mem_stats($server->sock, 'settings');
ok($stats->{'binding_protocol'} =~ /$val/, "$val works");
};
is($@, '', "$val works");
}

# For the binary test, we just verify it starts since we don't have an easy bin client.
eval {
my $server = new_memcached("-B binary");
};
is($@, '', "binary works");

# Should blow up with something invalid.
eval {
my $server = new_memcached("-B http");
};
ok($@, "Died with illegal -B arg.");
2 changes: 1 addition & 1 deletion t/binary.t
Expand Up @@ -2,7 +2,7 @@

use strict;
use warnings;
use Test::More tests => 857;
use Test::More tests => 860;
use FindBin qw($Bin);
use lib "$Bin/lib";
use MemcachedTest;
Expand Down

0 comments on commit a155b04

Please sign in to comment.