Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: 0.15.2
version: 0.16.0

- name: Check code formatting
run: zig fmt --check .
Expand Down
18 changes: 7 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# memcached.zig

A memcached client library for Zig, built on [zio](https://github.com/lalinsky/zio) for async I/O. Uses the modern [meta protocol](https://docs.memcached.org/protocols/meta/) for efficient communication.
A memcached client library for Zig, built on `std.Io` for async I/O. Uses the modern [meta protocol](https://docs.memcached.org/protocols/meta/) for efficient communication.

## Features

- Async I/O via zio coroutines
- Async I/O via std.Io
- Connection pooling per server
- Multi-server support with consistent hashing (rendezvous)
- Meta protocol (mg, ms, md, ma commands)
Expand All @@ -15,17 +15,13 @@ A memcached client library for Zig, built on [zio](https://github.com/lalinsky/z

```zig
const std = @import("std");
const zio = @import("zio");
const memcached = @import("memcached");

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
pub fn main(init: std.process.Init) !void {
const allocator = init.gpa;
const io = init.io;

var rt = try zio.Runtime.init(gpa.allocator(), .{});
defer rt.deinit();

var client = try memcached.connect(gpa.allocator(), "localhost:11211");
var client = try memcached.connect(allocator, io, "localhost:11211");
defer client.deinit();

// Set a value
Expand All @@ -47,7 +43,7 @@ pub fn main() !void {
## Multi-server

```zig
var client = try memcached.Client.init(gpa.allocator(), .{
var client = try memcached.Client.init(allocator, io, .{
.servers = &.{
"server1:11211",
"server2:11211",
Expand Down
7 changes: 0 additions & 7 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});

const zio = b.dependency("zio", .{
.target = target,
.optimize = optimize,
});

// Library module
const mod = b.addModule("memcached", .{
.root_source_file = b.path("src/root.zig"),
.target = target,
.optimize = optimize,
});
mod.addImport("zio", zio.module("zio"));

// Examples
const examples_step = b.step("examples", "Build all examples");
Expand All @@ -29,7 +23,6 @@ pub fn build(b: *std.Build) void {
}),
});
example.root_module.addImport("memcached", mod);
example.root_module.addImport("zio", zio.module("zio"));

const install = b.addInstallArtifact(example, .{});
examples_step.dependOn(&install.step);
Expand Down
9 changes: 2 additions & 7 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@
.name = .memcached,
.version = "0.0.1",
.fingerprint = 0x2e7f2a7825dfe6f7,
.minimum_zig_version = "0.15.0",
.minimum_zig_version = "0.16.0",
.paths = .{
"build.zig",
"build.zig.zon",
"src",
},
.dependencies = .{
.zio = .{
.url = "git+https://github.com/lalinsky/zio?ref=v0.8.2#3ac234eae165177cd75742f922851dc5d373fd12",
.hash = "zio-0.8.2-xHbVVC5jGAAYUMZd_BW5eT-HJb_lf-LCGj4PaLOeEkA2",
},
},
.dependencies = .{},
}
13 changes: 4 additions & 9 deletions examples/basic.zig
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
const std = @import("std");
const zio = @import("zio");
const memcached = @import("memcached");

pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const allocator = gpa.allocator();

var rt = try zio.Runtime.init(allocator, .{});
defer rt.deinit();
pub fn main(init: std.process.Init) !void {
const allocator = init.gpa;
const io = init.io;

// Connect
var client = try memcached.connect(allocator, "localhost:11211");
var client = try memcached.connect(allocator, io, "localhost:11211");
defer client.deinit();

// Version
Expand Down
52 changes: 27 additions & 25 deletions src/Client.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const std = @import("std");
const zio = @import("zio");
const Allocator = std.mem.Allocator;
const Connection = @import("Connection.zig");
const Pool = @import("Pool.zig");
Expand All @@ -12,23 +11,24 @@ const log = std.log.scoped(.memcached);
const Client = @This();

gpa: Allocator,
io: std.Io,
servers: []Server,
hasher: Hasher,
round_robin: std.atomic.Value(usize),
retry_attempts: usize,
retry_interval: zio.Duration,
retry_interval: std.Io.Duration,

pub const Options = struct {
servers: []const []const u8 = &.{},
hasher: Hasher = .none,
max_idle: usize = 2,
read_buffer_size: usize = 4096,
write_buffer_size: usize = 4096,
connect_timeout: zio.Timeout = .none,
read_timeout: zio.Timeout = .none,
write_timeout: zio.Timeout = .none,
connect_timeout: std.Io.Timeout = .none,
read_timeout: std.Io.Timeout = .none,
write_timeout: std.Io.Timeout = .none,
retry_attempts: usize = 2,
retry_interval: zio.Duration = .zero,
retry_interval: std.Io.Duration = .{ .nanoseconds = 0 },
};

// Re-export types for convenience
Expand All @@ -37,7 +37,7 @@ pub const GetOpts = Connection.GetOpts;
pub const SetOpts = Connection.SetOpts;
pub const Error = Connection.Error;

pub fn init(gpa: Allocator, options: Options) !Client {
pub fn init(gpa: Allocator, io: std.Io, options: Options) !Client {
if (options.servers.len == 0) return error.NoServers;

const servers = try gpa.alloc(Server, options.servers.len);
Expand All @@ -54,11 +54,12 @@ pub fn init(gpa: Allocator, options: Options) !Client {

for (options.servers, 0..) |server_str, i| {
const host, const port = parseServer(server_str) orelse return error.InvalidServer;
servers[i] = Server.init(gpa, host, port, pool_opts);
servers[i] = Server.init(gpa, io, host, port, pool_opts);
}

return .{
.gpa = gpa,
.io = io,
.servers = servers,
.hasher = options.hasher,
.round_robin = std.atomic.Value(usize).init(0),
Expand Down Expand Up @@ -107,7 +108,7 @@ fn withServer(self: *Client, server: *Server, comptime func: anytype, args: anyt
attempts,
self.retry_attempts,
});
try zio.sleep(self.retry_interval);
try self.io.sleep(self.retry_interval, .awake);
continue;
}
return err;
Expand All @@ -128,7 +129,7 @@ fn withServer(self: *Client, server: *Server, comptime func: anytype, args: anyt
attempts,
self.retry_attempts,
});
try zio.sleep(self.retry_interval);
try self.io.sleep(self.retry_interval, .awake);
continue;
}
return err;
Expand Down Expand Up @@ -221,8 +222,10 @@ test "parseServer ipv6" {
try std.testing.expectEqual(11211, result.?[1]);
}

const testing = @import("testing.zig");

test "Client get/set" {
var client = try Client.init(std.testing.allocator, .{
var client = try Client.init(std.testing.allocator, std.testing.io, .{
.servers = &.{"127.0.0.1:21211"},
});
defer client.deinit();
Expand All @@ -237,7 +240,7 @@ test "Client get/set" {
}

test "Client get non-existent returns null" {
var client = try Client.init(std.testing.allocator, .{
var client = try Client.init(std.testing.allocator, std.testing.io, .{
.servers = &.{"127.0.0.1:21211"},
});
defer client.deinit();
Expand All @@ -249,7 +252,7 @@ test "Client get non-existent returns null" {
}

test "Client incr/decr" {
var client = try Client.init(std.testing.allocator, .{
var client = try Client.init(std.testing.allocator, std.testing.io, .{
.servers = &.{"127.0.0.1:21211"},
});
defer client.deinit();
Expand All @@ -264,7 +267,7 @@ test "Client incr/decr" {
}

test "Client delete" {
var client = try Client.init(std.testing.allocator, .{
var client = try Client.init(std.testing.allocator, std.testing.io, .{
.servers = &.{"127.0.0.1:21211"},
});
defer client.deinit();
Expand All @@ -278,7 +281,7 @@ test "Client delete" {
}

test "Client connection reused after NotStored error" {
var client = try Client.init(std.testing.allocator, .{
var client = try Client.init(std.testing.allocator, std.testing.io, .{
.servers = &.{"127.0.0.1:21211"},
.max_idle = 1,
});
Expand All @@ -303,7 +306,7 @@ test "Client connection reused after NotStored error" {
}

test "Client connection reused after Exists error (CAS conflict)" {
var client = try Client.init(std.testing.allocator, .{
var client = try Client.init(std.testing.allocator, std.testing.io, .{
.servers = &.{"127.0.0.1:21211"},
.max_idle = 1,
});
Expand Down Expand Up @@ -337,7 +340,7 @@ test "key distribution across servers" {
};

// Create distributed client
var client = try Client.init(std.testing.allocator, .{
var client = try Client.init(std.testing.allocator, std.testing.io, .{
.servers = servers,
.hasher = .rendezvous,
});
Expand All @@ -358,7 +361,7 @@ test "key distribution across servers" {
const host, const port = parseServer(server_str).?;

var conn: Connection = undefined;
try conn.connect(std.testing.allocator, host, port, .{});
try conn.connect(std.testing.allocator, std.testing.io, host, port, .{});
defer conn.close();

var buf: [1024]u8 = undefined;
Expand Down Expand Up @@ -387,9 +390,8 @@ test "key distribution across servers" {
}

test "retry after server restart" {
const testing = @import("testing.zig");

var client = try Client.init(std.testing.allocator, .{
const io = std.testing.io;
var client = try Client.init(std.testing.allocator, io, .{
.servers = &.{"127.0.0.1:21211"},
.retry_attempts = 5,
.retry_interval = .fromMilliseconds(500),
Expand All @@ -405,14 +407,14 @@ test "retry after server restart" {
try std.testing.expectEqualStrings("before_restart", info1.?.value);

// Stop immediately (no grace period)
try testing.runDockerCompose(std.testing.allocator, &.{ "stop", "-t", "0", "memcached-1" });
try testing.runDockerCompose(std.testing.allocator, io, &.{ "stop", "-t", "0", "memcached-1" });

// Start in background - will take time to be ready
var start_thread = try std.Thread.spawn(.{}, struct {
fn run() void {
testing.runDockerCompose(std.testing.allocator, &.{ "start", "memcached-1" }) catch {};
fn run(_io: std.Io) void {
testing.runDockerCompose(std.testing.allocator, _io, &.{ "start", "memcached-1" }) catch {};
}
}.run, .{});
}.run, .{io});

// Try immediately with stale connection - should fail and retry
try client.set("retry_test_key", "after_restart", .{});
Expand Down
Loading
Loading