mirror of
https://github.com/luxfi/zap.git
synced 2026-10-30 15:04:16 +00:00
Three orthogonal additions, one wire format. v2 package — generic ad-hoc schema dispatch (~/work/lux/zap/v2): - README + IMPOSSIBILITY + BENCH_RESULTS document the v1↔v2 split. - builder/field/iter/list/pool/registry/schema/view + codegen package emit v1-equivalent fast paths from declarative .zap sources. - _compile_fail_test/ + examples/ cover negative and positive cases. - v2_test.go exercises the generic dispatch path. - doc.go in v1 marks v1 deprecated for *new* schema authoring; existing v1 buffers stay parseable by both v1 and v2. bufpool — pooled read slabs (bufpool.go + bufpool_test.go): - Message gains optional *bufRef; Release() returns the slab to its pool when sourced from the pooled read path, GC-managed otherwise. - ParseHeader returns (data, rootOffset) without allocating *Message, used by zapv2 to avoid two allocations per parse. - bench_read_test.go + node_quic_stream_test.go cover both paths. Transport allocator + UMA/RDMA cgo (transport/*): - Transport.Caps()/Buffer interface: capability + slab-typed buffer contract. Bytes() vs DevicePtr() decoupled so UMA and GPUDirect can share the same call surface. - DPDK PMD + GPUDirect RDMA + CUDA UMA real cgo backends gated behind build tags; stub paths remain default on darwin / non-Linux. - transport_test.go covers Caps reporting + Buffer lifecycle on the default transport. LLM.md + go.mod follow the additions.
88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
// Copyright (C) 2025-2026, Lux Industries Inc. All rights reserved.
|
|
// See the file LICENSE for licensing terms.
|
|
|
|
package transport
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"sync"
|
|
)
|
|
|
|
// defaultTransport is the always-available baseline. Uses an in-process
|
|
// channel pair as the wire — adequate for tests and single-host dev
|
|
// rigs. Production deployments wire this to the existing
|
|
// github.com/luxfi/zap NodeServer/NodeClient via a small adapter (not
|
|
// included here to keep the transport package dependency-free).
|
|
type defaultTransport struct {
|
|
mu sync.Mutex
|
|
outbox chan envelope
|
|
inbox chan envelope
|
|
closed bool
|
|
}
|
|
|
|
type envelope struct {
|
|
peer string
|
|
msg []byte
|
|
}
|
|
|
|
func newDefault() Transport {
|
|
return &defaultTransport{
|
|
outbox: make(chan envelope, 64),
|
|
inbox: make(chan envelope, 64),
|
|
}
|
|
}
|
|
|
|
func (t *defaultTransport) Name() string { return "default" }
|
|
|
|
func (t *defaultTransport) Caps() Capabilities {
|
|
return Capabilities{GPUResident: false, ZeroCopy: false, MinLatencyMicros: 50}
|
|
}
|
|
|
|
func (t *defaultTransport) Send(ctx context.Context, peer string, msg []byte) error {
|
|
t.mu.Lock()
|
|
if t.closed {
|
|
t.mu.Unlock()
|
|
return errors.New("zap/transport: closed")
|
|
}
|
|
t.mu.Unlock()
|
|
// Copy because the caller may reuse the buffer after Send returns
|
|
// (the Transport interface contract). Outbox is bounded; we respect
|
|
// ctx for back-pressure.
|
|
cp := append([]byte(nil), msg...)
|
|
select {
|
|
case t.outbox <- envelope{peer: peer, msg: cp}:
|
|
return nil
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
}
|
|
}
|
|
|
|
func (t *defaultTransport) Recv(ctx context.Context) (string, []byte, error) {
|
|
select {
|
|
case env := <-t.inbox:
|
|
return env.peer, env.msg, nil
|
|
case <-ctx.Done():
|
|
return "", nil, ctx.Err()
|
|
}
|
|
}
|
|
|
|
func (t *defaultTransport) Close() error {
|
|
t.mu.Lock()
|
|
defer t.mu.Unlock()
|
|
if t.closed {
|
|
return nil
|
|
}
|
|
t.closed = true
|
|
close(t.outbox)
|
|
close(t.inbox)
|
|
return nil
|
|
}
|
|
|
|
// Outbox / Inbox are package-private hooks used by the adapter that
|
|
// bridges this transport to github.com/luxfi/zap NodeServer/NodeClient.
|
|
// Kept un-exported to discourage callers from poking at the channels
|
|
// directly — the adapter lives next to the production node code.
|
|
func (t *defaultTransport) outboxChan() <-chan envelope { return t.outbox }
|
|
func (t *defaultTransport) inboxChan() chan<- envelope { return t.inbox }
|