diff --git a/internal/output/writers.go b/internal/output/writers.go index a6d2f4c..dffcc40 100644 --- a/internal/output/writers.go +++ b/internal/output/writers.go @@ -259,6 +259,7 @@ type UnixSocketWriter struct { errorCallback ErrorCallback consecutiveFailures int failuresMu sync.Mutex + networkType string // "unix" for STREAM, "unixgram" for DGRAM } // NewUnixSocketWriter creates a new UNIX socket writer with reconnection logic @@ -300,7 +301,8 @@ func NewUnixSocketWriterWithConfig(socketPath string, dialTimeout, writeTimeout go w.processQueue() // Try initial connection silently (socket may not exist yet - that's okay) - conn, err := net.DialTimeout("unix", socketPath, w.dialTimeout) + // Use unixgram (DGRAM) for connectionless UDP-like socket communication + conn, err := net.DialTimeout("unixgram", socketPath, w.dialTimeout) if err == nil { w.conn = conn } @@ -399,7 +401,8 @@ func (w *UnixSocketWriter) writeWithReconnect(data []byte) error { if w.conn != nil { return nil } - conn, err := net.DialTimeout("unix", w.socketPath, w.dialTimeout) + // Use unixgram (DGRAM) for connectionless UDP-like socket communication + conn, err := net.DialTimeout("unixgram", w.socketPath, w.dialTimeout) if err != nil { return fmt.Errorf("failed to connect to socket %s: %w", w.socketPath, err) } diff --git a/internal/output/writers_test.go b/internal/output/writers_test.go index 9264680..a601392 100644 --- a/internal/output/writers_test.go +++ b/internal/output/writers_test.go @@ -657,31 +657,29 @@ func TestUnixSocketWriter_CallbackResetOnSuccess(t *testing.T) { tmpDir := t.TempDir() socketPath := filepath.Join(tmpDir, "test.sock") - // Create a real socket - listener, err := net.Listen("unix", socketPath) + // Create a real DGRAM (unixgram) socket + addr, err := net.ResolveUnixAddr("unixgram", socketPath) if err != nil { - t.Fatalf("Failed to create socket: %v", err) + t.Fatalf("Failed to resolve unixgram address: %v", err) + } + listener, err := net.ListenUnixgram("unixgram", addr) + if err != nil { + t.Fatalf("Failed to create unixgram socket: %v", err) } defer listener.Close() - // Start a goroutine to accept and read connections + // Start a goroutine to read datagrams done := make(chan struct{}) go func() { + buf := make([]byte, 65536) for { - conn, err := listener.Accept() - if err != nil { - select { - case <-done: - return - default: - } - continue + select { + case <-done: + return + default: } - // Read and discard data - buf := make([]byte, 1024) - conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) - conn.Read(buf) - conn.Close() + listener.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) + _, _, _ = listener.ReadFrom(buf) } }() defer close(done)