Skip to content

feat: Add Unix Domain Socket support for API mode #99

@inureyes

Description

@inureyes

Problem / Background

Currently, the API mode only supports TCP connections via the `--port` option. Local applications that need to consume all-smi metrics must:

  • Manage port allocation to avoid conflicts with other services
  • Handle network stack overhead even for local communication
  • Rely on network-based access control

Unix Domain Sockets (UDS) provide a better alternative for local IPC scenarios with improved security, performance, and simplified service discovery.

Proposed Solution

Add Unix Domain Socket support to the API mode, enabling local applications to read metrics without network exposure.

Motivation

  • Local applications need to consume all-smi metrics without port management overhead
  • UDS provides better security (file permission-based access control)
  • No port conflicts with other services
  • Better performance for local communication (bypasses TCP/IP stack)
  • Simplified service discovery with fixed socket path

CLI Interface

```bash

TCP only (existing)

all-smi api --port 9090

UDS only (new)

all-smi api --socket /var/run/all-smi.sock

UDS with default path (new)

all-smi api --socket

-> /var/run/all-smi.sock (Linux)

-> /tmp/all-smi.sock (macOS)

TCP + UDS simultaneously (optional, for advanced use cases)

all-smi api --port 9090 --socket /var/run/all-smi.sock
```

Technical Details

  1. Axum UnixListener: Use `tokio::net::UnixListener` with Axum for UDS support
  2. Platform support:
    • Linux: Full support
    • macOS: Full support
    • Windows: Disabled (`#[cfg(unix)]` conditional compilation)
  3. Socket file management:
    • Remove stale socket file on startup if exists
    • Clean up socket file on graceful shutdown
    • Handle SIGTERM/SIGINT for cleanup
  4. Default paths:
    • Linux: `/var/run/all-smi.sock` (fallback to `/tmp/all-smi.sock` if no permission)
    • macOS: `/tmp/all-smi.sock`

Usage Examples

```python

Python client

import requests_unixsocket
session = requests_unixsocket.Session()
r = session.get('http+unix://%2Ftmp%2Fall-smi.sock/metrics')
```

```bash

curl

curl --unix-socket /tmp/all-smi.sock http://localhost/metrics
```

Acceptance Criteria

  • `--socket` option added to API mode
  • `--socket` without path uses platform-appropriate default
  • `--port` and `--socket` can be used together
  • Socket file is cleaned up on shutdown
  • Stale socket file is removed on startup
  • Option is hidden/disabled on Windows
  • Documentation updated

Additional Context

This feature enables better integration with local monitoring agents, container orchestrators, and system services that prefer socket-based communication over TCP.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions