From 25d0473000f63f3ba1c027724672e451a86a0641 Mon Sep 17 00:00:00 2001 From: Max Altgelt Date: Mon, 30 Nov 2026 13:23:14 +0100 Subject: [PATCH 1/5] refactor: use options for New --- client.go | 6 +++--- client_test.go | 6 +++--- connectionwriter.go | 24 ++++++++++++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/client.go b/client.go index 831eaab..57a4a56 100644 --- a/client.go +++ b/client.go @@ -7,7 +7,6 @@ package simplesyslog import ( - "crypto/tls" "errors" "fmt" "os" @@ -60,8 +59,9 @@ type Client struct { // - NewClient(ConnectionTCP, ":514") // - NewClient(ConnectionTLS, "172.0.0.1:514") // - NewClient(ConnectionHTTP, "https://example.com:8080/syslog") -func NewClient(connectionType ConnectionType, address string, tlsconfig *tls.Config) (*Client, error) { - connWriter, err := NewConnectionWriter(connectionType, address, tlsconfig) +// - NewClient(ConnectionTLS, "172.0.0.1:514", WithTLSConfig(config)) +func NewClient(connectionType ConnectionType, address string, opts ...Option) (*Client, error) { + connWriter, err := NewConnectionWriter(connectionType, address, opts...) if err != nil { return nil, fmt.Errorf("could not create connection writer: %w", err) } diff --git a/client_test.go b/client_test.go index 90b0ba5..af8e1ee 100644 --- a/client_test.go +++ b/client_test.go @@ -81,7 +81,7 @@ func testNewClientUDP(t *testing.T) { /* * Send the message 'foo bar baz' to the syslog server */ - client, err := NewClient(ConnectionUDP, host, nil) + client, err := NewClient(ConnectionUDP, host) if err != nil { sendErr <- fmt.Errorf("could not initialize server: %s", err) return @@ -140,7 +140,7 @@ func testNewClientTCP(t *testing.T, useTLS bool) { if useTLS { connectionType = ConnectionTLS } - client, err := NewClient(connectionType, host, &tls.Config{InsecureSkipVerify: true}) + client, err := NewClient(connectionType, host, WithTLSConfig(&tls.Config{InsecureSkipVerify: true})) if err != nil { sendErr <- fmt.Errorf("could not initialize server: %s", err) return @@ -239,7 +239,7 @@ func testNewClientHTTPSpecific(t *testing.T, sendFunc sendFuncType) { /* * Send the message 'foo bar baz' to the syslog server */ - client, err := NewClient(ConnectionHTTP, ts.URL, tlsConfig) + client, err := NewClient(ConnectionHTTP, ts.URL, WithTLSConfig(tlsConfig)) if err != nil { sendErr <- fmt.Errorf("could not initialize server: %s", err) return diff --git a/connectionwriter.go b/connectionwriter.go index d36fdbe..5789a86 100644 --- a/connectionwriter.go +++ b/connectionwriter.go @@ -22,7 +22,23 @@ type ConnectionWriter interface { Close() error } -func NewConnectionWriter(connectionType ConnectionType, address string, tlsconfig *tls.Config) (ConnectionWriter, error) { +// Option configures the connection made by NewClient or NewConnectionWriter. +type Option func(*options) + +type options struct { + tlsConfig *tls.Config +} + +// WithTLSConfig sets the TLS configuration for TLS and HTTPS connections. +func WithTLSConfig(config *tls.Config) Option { + return func(o *options) { o.tlsConfig = config } +} + +func NewConnectionWriter(connectionType ConnectionType, address string, opts ...Option) (ConnectionWriter, error) { + var o options + for _, opt := range opts { + opt(&o) + } // Validate data switch connectionType { case ConnectionUDP, ConnectionTCP, ConnectionTLS, ConnectionHTTP: @@ -35,9 +51,9 @@ func NewConnectionWriter(connectionType ConnectionType, address string, tlsconfi panic("DefaultTransport is not of type *http.Transport") } transport := defaultTransport - if tlsconfig != nil { + if o.tlsConfig != nil { transport = defaultTransport.Clone() - transport.TLSClientConfig = tlsconfig + transport.TLSClientConfig = o.tlsConfig } return &httpWriter{ httpURL: address, @@ -49,7 +65,7 @@ func NewConnectionWriter(connectionType ConnectionType, address string, tlsconfi var err error // connect via udp / tcp / tls if connectionType == ConnectionTLS { - conn, err = tls.Dial(string(ConnectionTCP), address, tlsconfig) + conn, err = tls.Dial(string(ConnectionTCP), address, o.tlsConfig) } else { conn, err = net.Dial(string(connectionType), address) } From edd43e143c52f6f7aeba7ca2c9176b689fae3a73 Mon Sep 17 00:00:00 2001 From: Max Altgelt Date: Mon, 30 Nov 2026 13:25:04 +0100 Subject: [PATCH 2/5] fix: close HTTP body for bad status code --- connectionwriter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/connectionwriter.go b/connectionwriter.go index 5789a86..663eb52 100644 --- a/connectionwriter.go +++ b/connectionwriter.go @@ -93,10 +93,10 @@ func (h *httpWriter) WriteString(s string, contentType string) (int, error) { if err != nil { return 0, err } + defer func() { _ = resp.Body.Close() }() if resp.StatusCode < 200 || resp.StatusCode >= 300 { return 0, fmt.Errorf("HTTP request to write the message failed with status code %d", resp.StatusCode) } - defer func() { _ = resp.Body.Close() }() h.bytesWritten += int64(len(s)) return len(s), nil } From 95457a0e7369ff8ffea6ad57680f4709d91bc67b Mon Sep 17 00:00:00 2001 From: Max Altgelt Date: Mon, 30 Nov 2026 13:25:18 +0100 Subject: [PATCH 3/5] fix: make connection unusable on partial write --- connectionwriter.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/connectionwriter.go b/connectionwriter.go index 663eb52..38fe747 100644 --- a/connectionwriter.go +++ b/connectionwriter.go @@ -149,6 +149,10 @@ func (b *basicWriter) WriteString(s string, contentType string) (int, error) { // Send the message via TCP/UDP n, err := fmt.Fprint(b.conn, s) b.bytesWritten += int64(n) + if n > 0 && err != nil { + // Partial write, connection is unusable from now on, so close it + _ = b.conn.Close() + } return n, err } From 5e7be31de7a796fb0de7003a8e04b27f21c11e05 Mon Sep 17 00:00:00 2001 From: Max Altgelt Date: Mon, 30 Nov 2026 13:30:43 +0100 Subject: [PATCH 4/5] feat: support timeout --- client.go | 2 +- client_test.go | 18 ++++++++++++++++++ connectionwriter.go | 28 +++++++++++++++++++++++----- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 57a4a56..2c29935 100644 --- a/client.go +++ b/client.go @@ -59,7 +59,7 @@ type Client struct { // - NewClient(ConnectionTCP, ":514") // - NewClient(ConnectionTLS, "172.0.0.1:514") // - NewClient(ConnectionHTTP, "https://example.com:8080/syslog") -// - NewClient(ConnectionTLS, "172.0.0.1:514", WithTLSConfig(config)) +// - NewClient(ConnectionTLS, "172.0.0.1:514", WithTLSConfig(config), WithTimeout(10*time.Second)) func NewClient(connectionType ConnectionType, address string, opts ...Option) (*Client, error) { connWriter, err := NewConnectionWriter(connectionType, address, opts...) if err != nil { diff --git a/client_test.go b/client_test.go index af8e1ee..ddc6460 100644 --- a/client_test.go +++ b/client_test.go @@ -4,6 +4,7 @@ import ( "crypto/tls" "crypto/x509" "encoding/pem" + "errors" "fmt" "io" "net" @@ -300,3 +301,20 @@ func init() { MinVersion: tls.VersionTLS11, } } + +func TestWithTimeout(t *testing.T) { + release := make(chan struct{}) + ts := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { <-release })) + defer ts.Close() + defer close(release) + + client, err := NewClient(ConnectionHTTP, ts.URL, WithTimeout(50*time.Millisecond)) + if err != nil { + t.Fatalf("could not create client: %s", err) + } + defer func() { _ = client.Close() }() + var netErr net.Error + if err := client.SendRaw("foo"); !errors.As(err, &netErr) || !netErr.Timeout() { + t.Fatalf("expected timeout error, got: %v", err) + } +} diff --git a/connectionwriter.go b/connectionwriter.go index 38fe747..bb56686 100644 --- a/connectionwriter.go +++ b/connectionwriter.go @@ -7,6 +7,7 @@ import ( "net/http" "net/url" "strings" + "time" ) // connWriter is used to write to the connection. @@ -27,6 +28,7 @@ type Option func(*options) type options struct { tlsConfig *tls.Config + timeout time.Duration } // WithTLSConfig sets the TLS configuration for TLS and HTTPS connections. @@ -34,6 +36,15 @@ func WithTLSConfig(config *tls.Config) Option { return func(o *options) { o.tlsConfig = config } } +// WithTimeout limits how long establishing the connection and sending each +// message may block. A message that hits the timeout fails with an error. On +// TCP and TLS, a write only blocks once the send buffer is full, so a stalled +// server is noticed late; if part of a message was written by then, the +// connection is closed. A timeout of 0 means no limit. +func WithTimeout(timeout time.Duration) Option { + return func(o *options) { o.timeout = timeout } +} + func NewConnectionWriter(connectionType ConnectionType, address string, opts ...Option) (ConnectionWriter, error) { var o options for _, opt := range opts { @@ -57,22 +68,23 @@ func NewConnectionWriter(connectionType ConnectionType, address string, opts ... } return &httpWriter{ httpURL: address, - client: http.Client{Transport: transport}, + client: http.Client{Transport: transport, Timeout: o.timeout}, }, nil } var conn net.Conn var err error // connect via udp / tcp / tls + dialer := &net.Dialer{Timeout: o.timeout} if connectionType == ConnectionTLS { - conn, err = tls.Dial(string(ConnectionTCP), address, o.tlsConfig) + conn, err = tls.DialWithDialer(dialer, string(ConnectionTCP), address, o.tlsConfig) } else { - conn, err = net.Dial(string(connectionType), address) + conn, err = dialer.Dial(string(connectionType), address) } if err != nil { return nil, err } - return &basicWriter{conn: conn}, nil + return &basicWriter{conn: conn, timeout: o.timeout}, nil } type httpWriter struct { @@ -136,7 +148,8 @@ func (h *httpWriter) SourceIP() (net.IP, error) { // basicWriter writes messages to a TCP/UDP connection. type basicWriter struct { conn net.Conn - bytesWritten int64 // Number of bytes written + timeout time.Duration // Limit for each write, 0 for none + bytesWritten int64 // Number of bytes written } // WriteString writes a string to the connection. @@ -146,6 +159,11 @@ func (b *basicWriter) WriteString(s string, contentType string) (int, error) { if !strings.HasSuffix(s, "\n") { s += "\n" } + if b.timeout > 0 { + if err := b.conn.SetWriteDeadline(time.Now().Add(b.timeout)); err != nil { + return 0, err + } + } // Send the message via TCP/UDP n, err := fmt.Fprint(b.conn, s) b.bytesWritten += int64(n) From a839d14e6dc123a7ed4a1d9760e0980b81957725 Mon Sep 17 00:00:00 2001 From: Max Altgelt Date: Mon, 30 Nov 2026 13:44:20 +0100 Subject: [PATCH 5/5] chore: bump linter --- .github/workflows/go.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 6078642..d52888b 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -28,4 +28,4 @@ jobs: - name: Lint uses: golangci/golangci-lint-action@v8 with: - version: v2.1.6 + version: v2.15.0