zapclient
Package zapclient
import "github.com/luxfi/zap/zapclient"Functions
MustOpcode
func MustOpcode(name string) uint16MustOpcode is ProcedureOpcode that panics on error. Use in package-level var initialisers where a bad procedure name is a build-time bug.
ProcedureOpcode
func ProcedureOpcode(name string) (uint16, error)ProcedureOpcode returns the uint16 opcode for a procedure name. The lower 8 bits are zero (matching the existing MsgType<<8 shape in lux/zap); the upper 8 bits are FNV-1a(name) modulo 254 + 1.
Returns ErrReservedOpcode if the hash lands on 0x00 or 0xFF.
Types
AllowListVerifier
AllowListVerifier accepts only peers whose NodeID appears in Allowed[procedure]. Empty Allowed[procedure] means the procedure is not exposed.
func (a AllowListVerifier) Verify(ctx context.Context, peer PeerInfo, procedure string) errorClient
Client is a native ZAP RPC client. Created by Connect; closed by Close (or returned stop function from MustConnect).
func Connect(ctx context.Context, serviceType string, opts ...ClientOption) (*Client, error)func MustConnect(ctx context.Context, serviceType string, opts ...ClientOption) *Clientfunc (c *Client) Broadcast(ctx context.Context, procedure string, req *zap.Message) map[string]errorfunc (c *Client) Call(ctx context.Context, procedure string, req *zap.Message) (*zap.Message, error)func (c *Client) Close() errorfunc (c *Client) Peers() []Peerfunc (c *Client) Send(ctx context.Context, procedure string, req *zap.Message) errorClientOption
ClientOption is the functional-option constructor knob.
func WithBrowseInterval(d time.Duration) ClientOptionfunc WithCallTimeout(d time.Duration) ClientOptionfunc WithDiscoverTimeout(d time.Duration) ClientOptionfunc WithDiscovery(d Discovery) ClientOptionfunc WithLogger(l *slog.Logger) ClientOptionfunc WithMinPeers(n int) ClientOptionfunc WithNodeID(id string) ClientOptionfunc WithPicker(p Picker) ClientOptionfunc WithStaticPeers(addrs ...string) ClientOptionfunc WithTLS(cfg *tls.Config) ClientOptionClientOptions
ClientOptions configure Connect. Construct via WithX options.
Discovery
Discovery is the peer-enumeration contract.
Implementations MUST be goroutine-safe; the client may call Peers() from every in-flight Call and Send.
LocalTrustVerifier
LocalTrustVerifier accepts any peer presenting a valid mTLS cert from the cluster CA. The underlying TLS handshake already verified the chain; this verifier returns nil for every authenticated peer.
Use when:
- the peer is on the cluster-private network
- the listener was constructed with the cluster CA in ClientCAs
- all in-cluster services should be allowed to call all procedures (apply per-procedure RBAC in the handler if needed)
func (LocalTrustVerifier) Verify(ctx context.Context, peer PeerInfo, procedure string) errorPeer
Peer is the discovery view of one reachable instance of a service.
NodeID uniquely identifies the peer within ServiceType across discovery cycles. Address is the dial target (host:port). For in-cluster peers under mTLS, the authenticated peer identity comes from the TLS session — NodeID is a hint, not a trust anchor.
PeerInfo
PeerInfo is the authenticated peer identity surfaced to server-side handlers and PeerVerifier.
PeerVerifier
PeerVerifier is the server-side authorisation hook. Runs on every inbound message before procedure dispatch — return non-nil to reject the call.
The PeerInfo carries the authenticated peer identity (NodeID + mTLS cert chain if present). Procedure is the procedure name the caller requested. PeerVerifier should compare the (peer, procedure) pair against the service's authorisation policy.
Picker
Picker selects one peer from a Discovery snapshot for a single Call or Send. Implementations MUST be cheap (sub-microsecond) and goroutine-safe; the client invokes Pick from every concurrent op.
ProcedureHandler
ProcedureHandler runs server-side for one inbound procedure call. Return nil + nil-err to ack a fire-and-forget Send. Return a response + nil-err to reply to a Call. Return nil + non-nil err to signal failure; the client side observes the err on its Call.
RoundRobinPicker
RoundRobinPicker rotates through peers in registration order. Zero-value is ready to use; goroutine-safe.
func (p *RoundRobinPicker) Pick(peers []Peer) (Peer, error)Server
Server is the native ZAP server side: procedure-name dispatch on top of *zap.Node. Construct with NewServer, Register procedures, Start to begin accepting, Stop to release.
func NewServer(serviceType string, opts ...ServerOption) (*Server, error)func (s *Server) Dispatch(ctx context.Context, from string, msg *zap.Message) (*zap.Message, error)func (s *Server) NodeID() stringfunc (s *Server) Register(procedure string, h ProcedureHandler) errorfunc (s *Server) Start() errorfunc (s *Server) Stop()ServerOption
ServerOption is a functional-option knob.
func WithNoDiscovery() ServerOptionfunc WithServerLogger(l *slog.Logger) ServerOptionfunc WithServerMetadata(m map[string]string) ServerOptionfunc WithServerNodeID(id string) ServerOptionfunc WithServerPort(p int) ServerOptionfunc WithServerTLS(cfg *tls.Config) ServerOptionfunc WithVerifier(v PeerVerifier) ServerOptionServerOptions
ServerOptions configure NewServer.