Unix Domain Socket

UDS Server

The UDS Server has builtin abilities to

  • clean up UDS file before listening
  • set UDS file permission
  • graceful shutdown
  • defend against DDoS attacks by limiting max number of concurrent connections
  • close idle connections
  • generate connection id
  • etc.
import (
    "github.com/burningxflame/gx/uds"
    "github.com/burningxflame/gx/sync/sem"
)

// Create an UDS Server
srv := &uds.Server{
    // The UDS address to listen
    UdsAddr: "/some/path",
    // File permission of the UdsAddr.
    // If specified, set the file permission of UdsAddr to the specified value.
    Perm: 0600,
    // Connection handler is where you communicate with a client, i.e. receive/send data from/to a client.
    // Connection handler should return ASAP when ctx.Done channel is closed, which usually means an exit signal is sent.
    ConnHandler: func(ctx context.Context, conn net.Conn) error { ... },
    // If graceful shutdown takes longer than ShutdownTimeout, exit instantly.
    // Default to no timeout.
    ShutdownTimeout: time.Second*3,
    // Used to limit max number of concurrent connections.
    // Default to no limit.
    ConnLimiter: sem.New(n),
    // If no data is sent from a connection in the specified duration, close the connection.
    // Default to no timeout.
    IdleTimeout: time.Minute,
    // If true, the Context argument of ConnHandler contains the connection id.
    // Call GetConnId(ctx) to get connection id.
    CtxConnId: false,
    // Used to tag log messages
    Tag: "someTag",
    // A TagLogger used to log messages
    Log: ...,
}

// Start the Server
err := srv.Serve(ctx)

HTTP Server over UDS

The HTTP Server over UDS has builtin abilities to

  • clean up UDS file before listening
  • set UDS file permission
  • graceful shutdown
  • defend against DDoS attacks by limiting max number of concurrent requests
  • etc.
import (
    "net/http"

    uh "github.com/burningxflame/gx/uds/http"
    "github.com/burningxflame/gx/sync/sem"
)

// Create an HTTP Server over UDS
srv := &uh.Server{
    // http.Server in std lib
    Std: http.Server{
      Handler: someHandler,
      ...
    },
    // The UDS address to listen
    UdsAddr: "/some/path",
    // File permission of the UdsAddr.
    // If specified, set the file permission of UdsAddr to the specified value.
    Perm: 0600,
    // Used to limit max number of concurrent requests.
    // Default to no limit.
    Limiter: sem.New(n),
    // If graceful shutdown takes longer than ShutdownTimeout, exit instantly.
    ShutdownTimeout: time.Second*3,
    // Used to tag log messages
    Tag: "someTag",
    // A TagLogger used to log messages
    Log: ...,
}

// Start the Server
err := srv.Serve(ctx)

HTTP Client over UDS

import uh "github.com/burningxflame/gx/uds/http"

// Create an HTTP Client over UDS.
// The return value c is of type std *http.Client.
c := uh.NewClient(udsAddr)
// Use the Client as usual
resp, err := c.Post(...)

Meshless Client

import "github.com/burningxflame/gx/uds/meshless"

// Connect to and handshake with Meshless Node Agent
conn, err := meshless.Client(dstSvcId, meshless.Conf{
    // Default to /meshlet/client/meshless
    UdsAddr: "some/path"
    // Connection timeout. Default to no timeout.
    DialTimeout: ...
    // If handshake does not finish in the specified duration, close the connection.
    // Default to no timeout.
    HandshakeTimeout: ...
})

// Proceed to communication with the destination service
// i.e. use the conn as usual

Meshless HTTP Client

import "github.com/burningxflame/gx/uds/meshless"

// Connect to and handshake with Meshless Node Agent, and create an HTTP Client.
// Same params as Meshless Client.
// The return value c is of type std *http.Client.
c := meshless.NewHttpClient(dstSvcId, conf)
// Use the Client as usual
resp, err := c.Post(...)