Skip to content

Commit

Permalink
proxy: embed routelib
Browse files Browse the repository at this point in the history
`-o proxy_config=routelib` will use the binary-distributed routelib
library
  • Loading branch information
dormando committed Oct 23, 2024
1 parent 8203c93 commit 607343b
Show file tree
Hide file tree
Showing 7 changed files with 4,368 additions and 2 deletions.
4 changes: 3 additions & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ memcached_SOURCES += proto_proxy.c proto_proxy.h vendor/mcmc/mcmc.h \
proxy_config.c proxy_ring_hash.c \
proxy_internal.c \
proxy_tls.c proxy_tls.h \
md5.c md5.h
md5.c md5.h \
vendor/routelib/routelib.h
endif

if ENABLE_EXTSTORE
Expand Down Expand Up @@ -151,6 +152,7 @@ EXTRA_DIST = doc scripts t memcached.spec memcached_dtrace.d version.m4 README.m
EXTRA_DIST += vendor/Makefile vendor/lua/doc/* vendor/lua/Makefile vendor/lua/README
EXTRA_DIST += vendor/lua/src/*.c vendor/lua/src/*.h vendor/lua/src/Makefile
EXTRA_DIST += vendor/mcmc/LICENSE vendor/mcmc/Makefile vendor/mcmc/README.md vendor/mcmc/*.c vendor/mcmc/*.h
EXTRA_DIST += vendor/routelib/*.h

if ENABLE_PROXY
SUBDIRS += vendor
Expand Down
7 changes: 6 additions & 1 deletion proxy_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// TODO (v2): move worker thread related code back out of here.

#include "proxy.h"
#include "vendor/routelib/routelib.h"

// not using queue.h becuase those require specific storage for HEAD.
// it's not possible to have the HEAD simply be in the proxy context because
Expand Down Expand Up @@ -455,7 +456,11 @@ static int proxy_load_files(proxy_ctx_t *ctx) {
memset(db->buf, 0, db->size);
db->used = 0;

res = luaL_loadfile(L, db->fname);
if (strcmp(db->fname, "routelib") == 0) {
res = luaL_loadbuffer(L, routelib_lua, routelib_lua_len, "routelib");
} else {
res = luaL_loadfile(L, db->fname);
}
if (res != LUA_OK) {
fprintf(stderr, "ERROR: Failed to load proxy_startfile: %s\n", lua_tostring(L, -1));
return -1;
Expand Down
12 changes: 12 additions & 0 deletions t/proxyroutelib.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
-- Minimal configuration.
pools{
main = {
backends = {
"127.0.0.1:12181",
}
}
}

routes{
default = route_direct{ child = "main" }
}
43 changes: 43 additions & 0 deletions t/proxyroutelib.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env perl

use strict;
use warnings;
use Test::More;
use FindBin qw($Bin);
use lib "$Bin/lib";
use Carp qw(croak);
use MemcachedTest;
use IO::Socket qw(AF_INET SOCK_STREAM);
use IO::Select;
use Data::Dumper qw/Dumper/;

if (!supports_proxy()) {
plan skip_all => 'proxy not enabled';
exit 0;
}

my $t = Memcached::ProxyTest->new(servers => [12181]);

my $p_srv = new_memcached('-o proxy_config=routelib,proxy_arg=./t/proxyroutelib.lua -t 1');
my $ps = $p_srv->sock;
$ps->autoflush(1);

$t->set_c($ps);
$t->accept_backends();

my $w = $p_srv->new_sock;
print $w "watch proxyevents\r\n";
is(<$w>, "OK\r\n");

# We don't intend to test routelib here, just ensure that the embedding works
# and this obviously routelib config starts.

subtest 'basic' => sub {
$t->c_send("mg test\r\n");
$t->be_recv(0, "mg test\r\n");
$t->be_send(0, "HD\r\n");
$t->c_recv_be();
$t->clear();
};

done_testing();
2 changes: 2 additions & 0 deletions vendor/routelib/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
generated by:
xxd -i routelib.lua | perl -p -e 's/unsigned/const/' > routelib.h
3,019 changes: 3,019 additions & 0 deletions vendor/routelib/routelib.h

Large diffs are not rendered by default.

Loading

0 comments on commit 607343b

Please sign in to comment.