| Crates.io | go-server-rust-sdk |
| lib.rs | go-server-rust-sdk |
| version | 1.6.0 |
| created_at | 2025-07-23 06:47:30.649952+00 |
| updated_at | 2025-07-24 12:11:37.340502+00 |
| description | Rust SDK for go-server distributed task scheduler and worker system |
| homepage | https://github.com/go-enols/go-server |
| repository | https://github.com/go-enols/go-server |
| max_upload_size | |
| id | 1764412 |
| size | 129,947 |
A Rust implementation of the Go Server SDK for distributed task processing.
Add this to your Cargo.toml:
[dependencies]
go-server-rust-sdk = "0.1.0"
tokio = { version = "1.0", features = ["full"] }
serde_json = "1.0"
use go_server_rust_sdk::worker::call;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Call a remote method
let result = call(
"http://localhost:8080",
"add",
json!({"a": 1, "b": 2})
).await?;
println!("Result: {}", result);
Ok(())
}
use go_server_rust_sdk::worker::{Worker, Config};
use serde_json::{json, Value};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let config = Config {
scheduler_url: "ws://localhost:8080/api/worker/connect/123456".to_string(),
worker_group: "math".to_string(),
max_retry: 5,
ping_interval: 5,
};
let mut worker = Worker::new(config);
// Register a method
worker.register_method("add", |params: Value| {
let a = params["a"].as_f64().unwrap_or(0.0);
let b = params["b"].as_f64().unwrap_or(0.0);
Ok(json!(a + b))
}, vec!["Add two numbers".to_string()]);
// Start the worker
worker.start().await?;
Ok(())
}
use go_server_rust_sdk::scheduler::Client;
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new("http://localhost:8080".to_string());
// Synchronous execution
let result = client.execute_sync("add", json!({"a": 5, "b": 3})).await?;
println!("5 + 3 = {}", result);
// Asynchronous execution
let task_id = client.execute("multiply", json!({"a": 4, "b": 7})).await?;
let result = client.get_result(&task_id).await?;
if let Some(result) = result {
println!("4 * 7 = {}", result);
}
// Encrypted execution
let result = client.execute_sync_encrypted(
"add",
json!({"a": 10, "b": 15}),
"my-secret-key"
).await?;
println!("10 + 15 = {} (encrypted)", result);
Ok(())
}
The Client struct provides methods for executing tasks on the scheduler:
execute(method, params) - Execute a task asynchronouslyexecute_sync(method, params) - Execute a task synchronously with pollingexecute_encrypted(method, params, key) - Execute an encrypted task asynchronouslyexecute_sync_encrypted(method, params, key) - Execute an encrypted task synchronouslyget_result(task_id) - Get the result of a taskget_result_encrypted(task_id, key) - Get the encrypted result of a taskThe RetryClient wraps the base client with automatic retry functionality:
execute_with_retry(method, params) - Execute with retry logicexecute_sync_with_retry(method, params) - Synchronous execution with retryexecute_encrypted_with_retry(method, params, key) - Encrypted execution with retryexecute_sync_encrypted_with_retry(method, params, key) - Encrypted synchronous execution with retryThe Worker struct allows you to create distributed task workers:
new(config) - Create a new workerregister_method(name, handler, docs) - Register a method handlerstart() - Start the worker (blocks until stopped)stop() - Stop the workercall(scheduler_url, method, params) - Simple function to call a remote methodcall_encrypted(scheduler_url, method, params, key) - Simple function to call an encrypted remote methodSee the examples/ directory for complete examples:
client.rs - Simple client exampleworker.rs - Worker with multiple methodsscheduler.rs - Advanced scheduler client usageThe SDK uses a comprehensive error system with the SdkError enum that covers:
The SDK supports AES-GCM encryption for secure task execution. When using encrypted methods, both the task parameters and results are encrypted using the provided key.
This Rust SDK is fully compatible with the original Go SDK and can interoperate with Go-based workers and clients.
MIT License - see LICENSE file for details.