gRPC client
Overview
The machrpc package provides a standard Go database interface for connecting to and interacting with machbase-neo servers. Built on gRPC, it offers high-performance connectivity while maintaining compatibility with Go’s database/sql package conventions.
For a standard Go database/sql interface, please refer to the SQL driver section.
Installation
To use the machbase-neo SQL driver in your Go project, install the package:
go get github.com/machbase/neo-server/v8TLS Configuration
Since the machbase-neo SQL driver is based on gRPC, TLS certificates are required for secure communication between client and server. You need to prepare the following certificate files:
- Server Certificate (
server-cert.pem): The server’s public certificate - Client Certificate (
client-cert.pem): The client’s public certificate - Client Key (
client-key.pem): The client’s private key
Generating Certificates
Please see the API Security guide for step‑by‑step instructions to generate the TLS certificates required by the driver — server-cert.pem, client-cert.pem, and client-key.pem. The guide explains creating a CA, signing certificates.
Basic Usage
Establishing a Connection
| |
Configuration Options
The machrpc.Config struct accepts the following options:
| Field | Type | Description |
|---|---|---|
ServerAddr | string | Server address in host:port format (default: 127.0.0.1:5655) |
Tls | *TlsConfig | TLS configuration for secure communication |
The machrpc.TlsConfig struct:
| Field | Type | Description |
|---|---|---|
ServerCert | string | Path to server certificate file |
ClientCert | string | Path to client certificate file |
ClientKey | string | Path to client private key file |
Querying Data
Simple SELECT Query
| |
Parameterized Queries
Always use parameterized queries to prevent SQL injection:
| |
Executing Commands
INSERT Statement
| |
DELETE Statement
| |
High-Performance Bulk Insert (Appender)
For high-throughput data insertion, use the Appender interface. This provides optimal performance for time-series data ingestion:
// IMPORTANT: Dedicate a separate connection for the Appender
// A connection with an active Appender should not be used for other operations
apd, err := conn.Appender(ctx, "example")
if err != nil {
panic(err)
}
defer apd.Close() // Always close the appender to flush remaining data
// High-speed bulk insertion
for i := range 10_000 {
err := apd.Append("grpc", time.Now(), 1.23*float64(i))
if err != nil {
panic(err)
}
}Best Practices
1. Always Use Context
Pass context to all operations for proper timeout and cancellation handling:
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
rows, err := conn.Query(ctx, "SELECT * FROM large_table")2. Close Resources
Always close connections, statements, and result sets:
defer cli.Close()
defer conn.Close()
defer rows.Close()3. Handle Errors Appropriately
Don’t ignore errors; wrap them with context for better debugging:
if err != nil {
return fmt.Errorf("failed to query sensor data: %w", err)
}4. Use Connection Pooling in Production
Reuse connections to reduce overhead and improve performance.
| |
Complete Example
Here’s a complete example demonstrating various operations:
| |
Troubleshooting
Common Issues
Connection Refused
- Verify the server is running and accessible
- Check the server address and port
- Ensure firewall rules allow the connection
TLS Certificate Errors
- Verify certificate file paths are correct
- Ensure certificates are valid and not expired
- Check file permissions on certificate files
Authentication Failed
- Verify username and password are correct
- Check user permissions in machbase-neo
Query Timeout
- Increase context timeout for long-running queries
- Optimize query performance with appropriate indexes
- Consider pagination for large result sets