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
35 changes: 9 additions & 26 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
name: CI/CD with Benchmarks

env:
ZIG_VERSION: 0.15.2

on:
push:
branches: [ main, develop ]
Expand All @@ -14,20 +17,10 @@ jobs:
- uses: actions/checkout@v4

- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0

- name: Restore Zig cache
uses: actions/cache@v3
uses: mlugg/setup-zig@v2
with:
path: |
~/.cache/zig
zig-cache
key: ${{ runner.os }}-zig-${{ hashFiles('**/*.zig', '**/build.zig.zon') }}
restore-keys: |
${{ runner.os }}-zig-

version: ${{env.ZIG_VERSION}}

- name: Build project
run: zig build

Expand All @@ -46,19 +39,9 @@ jobs:
- uses: actions/checkout@v4

- name: Setup Zig
uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0

- name: Restore Zig cache
uses: actions/cache@v3
uses: mlugg/setup-zig@v2
with:
path: |
~/.cache/zig
zig-cache
key: ${{ runner.os }}-zig-${{ hashFiles('**/*.zig', '**/build.zig.zon') }}
restore-keys: |
${{ runner.os }}-zig-
version: ${{env.ZIG_VERSION}}

- name: Build benchmark tool
run: zig build
Expand Down Expand Up @@ -254,4 +237,4 @@ jobs:
}
} catch (error) {
console.log('Performance regression check failed:', error);
}
}
164 changes: 144 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,60 +20,136 @@ A blazingly fast gRPC client & server implementation in Zig, designed for maximu
## 🚀 Quick Start

```zig
// Server
const server = try GrpcServer.init(allocator, 50051, "secret-key");
try server.handlers.append(.{
.name = "SayHello",
.handler_fn = sayHello,
});
try server.start();
const std = @import("std");
const GrpcServer = @import("grpc-server").GrpcServer;

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

// Create and configure server
var server = try GrpcServer.init(allocator, 50051, "secret-key");
defer server.deinit();

// Client
var client = try GrpcClient.init(allocator, "localhost", 50051);
const response = try client.call("SayHello", "World", .none);
// Register handlers
try server.handlers.append(allocator, .{
.name = "SayHello",
.handler_fn = sayHello,
});

// Start server
try server.start();
}

fn sayHello(request: []const u8, allocator: std.mem.Allocator) ![]u8 {
_ = request;
return allocator.dupe(u8, "Hello from gRPC-zig!");
}
```

## 📚 Examples

### Basic Server

See [examples/basic_server.zig](examples/basic_server.zig) for a complete example.

```zig
const std = @import("std");
const GrpcServer = @import("server.zig").GrpcServer;
const GrpcServer = @import("grpc-server").GrpcServer;

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

var server = try GrpcServer.init(gpa.allocator(), 50051, "secret-key");
var server = try GrpcServer.init(allocator, 50051, "secret-key");
defer server.deinit();

try server.start();
}
```

### Streaming
### Basic Client

See [examples/basic_client.zig](examples/basic_client.zig) for a complete example.

```zig
var stream = streaming.MessageStream.init(allocator, 5);
try stream.push("First message", false);
try stream.push("Final message", true);
const std = @import("std");
const GrpcClient = @import("grpc-client").GrpcClient;

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

var client = try GrpcClient.init(allocator, "localhost", 50051);
defer client.deinit();

const response = try client.call("SayHello", "World", .none);
defer allocator.free(response);

std.debug.print("Response: {s}\n", .{response});
}
```

### Features

All features are demonstrated in the [examples/](examples/) directory:

- **[Authentication](examples/auth.zig)**: JWT token generation and verification
- **[Compression](examples/compression.zig)**: gzip/deflate support
- **[Streaming](examples/streaming.zig)**: Bi-directional message streaming
- **[Health Checks](examples/health.zig)**: Service health monitoring

## 🔧 Installation

1. Fetch the dependency:
### Option 1: Using zig fetch (Recommended)

1. Add the dependency to your project:

```sh
zig fetch --save "git+https://ziglana/grpc-zig/gRPC-zig#main"
zig fetch --save git+https://github.com/ziglana/gRPC-zig#main
```

2. Add to your `build.zig`:

```zig
const grpc_zig = b.dependency("grpc_zig", .{});
const grpc_zig_dep = b.dependency("grpc_zig", .{
.target = target,
.optimize = optimize,
});

// For server development
exe.root_module.addImport("grpc-server", grpc_zig_dep.module("grpc-server"));

// For client development
exe.root_module.addImport("grpc-client", grpc_zig_dep.module("grpc-client"));
```

3. Import in your code:

```zig
const GrpcServer = @import("grpc-server").GrpcServer;
const GrpcClient = @import("grpc-client").GrpcClient;
```

### Option 2: Manual setup

exe.addModule("grpc", grpc_zig.module("grpc"));
Clone the repository and add it to your `build.zig.zon`:

```zig
.{
.name = "my-project",
.version = "0.1.0",
.dependencies = .{
.grpc_zig = .{
.url = "https://github.com/ziglana/gRPC-zig/archive/refs/heads/main.tar.gz",
// Replace with actual hash after first fetch
.hash = "...",
},
},
}
```

## 🏃 Performance
Expand Down Expand Up @@ -123,6 +199,54 @@ The benchmarks automatically run in CI/CD on every pull request and provide perf

📖 **[Detailed Benchmarking Guide](docs/benchmarking.md)**

## 🧪 Testing

### Unit Tests

Run the unit test suite:

```bash
zig build test
```

The test suite covers:
- Compression algorithms (gzip, deflate, none)
- Benchmark handler functionality
- Core protocol functionality

### Integration Tests

Run integration tests with a Python client validating the Zig server:

```bash
cd integration_test
./run_tests.sh
```

Or manually:

```bash
# Build and start the test server
zig build integration_test
./zig-out/bin/grpc-test-server

# In another terminal, run Python tests
cd integration_test
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt
python3 test_client.py
```

The integration tests validate:
- HTTP/2 protocol compliance
- gRPC request/response flow
- Compression functionality
- Health checking
- Authentication integration

📖 **[Integration Test Documentation](integration_test/README.md)**

## 🤝 Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
Expand Down
Loading
Loading