netns

package
v0.7.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 2, 2025 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewNamespacedJSONRPCClient

func NewNamespacedJSONRPCClient(url string, namespace string, opts *JSONRPCClientOptions) (jsonrpc.RPCClient, error)

NewNamespacedJSONRPCClient returns a JSON-RPC client that performs network operations within the context of a given network namespace. It constructs a custom HTTP transport using a single-threaded dialer wrapped in RunInNamespace, allowing requests to be issued from inside the specified namespace.

func RunInNamespace

func RunInNamespace[T any](nsName string, fn func() (T, error)) (T, error)

RunInNamespace executes the given function within the context of the specified Linux network namespace. It locks the current OS thread, switches to the target namespace using setns, invokes the function, and then restores the original namespace. This allows thread-local operations like dialing sockets to be scoped to a namespace without affecting the rest of the program.

This is safe for use in single-threaded, short-lived operations; not safe for concurrent use.

func WaitForNamespace

func WaitForNamespace(log *slog.Logger, namespace string, timeout time.Duration) (*netns.NsHandle, error)

Types

type JSONRPCClientOptions

type JSONRPCClientOptions struct {
	DialTimeout   time.Duration
	DialKeepAlive time.Duration
	HTTPTimeout   time.Duration
}

type SingleThreadTransport

type SingleThreadTransport struct {
	DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
	TLSConfig   *tls.Config
}

SingleThreadTransport is a minimal, non-concurrent HTTP transport that uses a manually specified DialContext to establish TCP connections. It is designed for environments like network namespaces where custom dialing logic is required. It does not support connection reuse or pipelining and is intended for low-level, single-request usage patterns where full-featured HTTP transport is unnecessary.

func (*SingleThreadTransport) RoundTrip

func (t *SingleThreadTransport) RoundTrip(req *http.Request) (*http.Response, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL