zap: infer transport network from addr (unix socket / in-proc fast path)

Dial + Listen now infer the network from the address instead of hardcoding
tcp: a socket path (/tmp/vm.sock, @abstract, or any '/' path) → unix-domain
socket (same-host IPC, no TCP/UDP stack, no ephemeral-port exhaustion); a
host:port → tcp (genuinely remote). Strict backward-compat: existing
127.0.0.1:port addrs still dial tcp. Enables the rpcchainvm same-host fast
path — a co-located plugin VM connects back over a unix socket with zero
plugin-side code change once rebuilt against this api.

Co-authored-by: Hanzo Dev <[email protected]>
This commit is contained in:
2026-07-11 23:29:19 -07:00
co-authored by Hanzo Dev
parent 8bdf9cbf1b
commit eb596a6fb1
+25 -3
View File
@@ -11,6 +11,7 @@ import (
"fmt"
"io"
"net"
"strings"
"sync"
"sync/atomic"
"time"
@@ -66,14 +67,18 @@ type response struct {
err error
}
// Dial connects to a ZAP server
// Dial connects to a ZAP server. The network transport is inferred from addr:
// a filesystem path ("/tmp/vm.sock", "@abstract", or anything containing no
// ':' host:port) dials a unix-domain socket — no TCP/UDP stack, the in-proc /
// same-host fast path; a "host:port" addr dials TCP for a genuinely remote
// server. Backward compatible: existing "127.0.0.1:port" addrs still dial tcp.
func Dial(ctx context.Context, addr string, config *Config) (*Conn, error) {
if config == nil {
config = DefaultConfig()
}
dialer := &net.Dialer{}
conn, err := dialer.DialContext(ctx, "tcp", addr)
conn, err := dialer.DialContext(ctx, transportNetwork(addr), addr)
if err != nil {
return nil, fmt.Errorf("zap: dial failed: %w", err)
}
@@ -81,6 +86,23 @@ func Dial(ctx context.Context, addr string, config *Config) (*Conn, error) {
return newConn(conn, config), nil
}
// transportNetwork infers the net.Dial/net.Listen network from an address:
// unix-domain socket for a filesystem/abstract path (same-host, no TCP stack),
// tcp for a host:port. A ':' with no '/' means host:port ⇒ tcp; anything that
// looks like a path ('/', leading '@' abstract socket, or no ':') ⇒ unix.
func transportNetwork(addr string) string {
if strings.HasPrefix(addr, "@") || strings.HasPrefix(addr, "/") {
return "unix"
}
if strings.Contains(addr, ":") && !strings.Contains(addr, "/") {
return "tcp"
}
if strings.Contains(addr, "/") {
return "unix"
}
return "tcp"
}
// NewConn wraps an existing net.Conn as a ZAP connection
func NewConn(conn net.Conn, config *Config) *Conn {
if config == nil {
@@ -247,7 +269,7 @@ func Listen(addr string, config *Config) (*Listener, error) {
config = DefaultConfig()
}
listener, err := net.Listen("tcp", addr)
listener, err := net.Listen(transportNetwork(addr), addr)
if err != nil {
return nil, fmt.Errorf("zap: listen failed: %w", err)
}