go network_error ai_generated partial

dial tcp [::1]:PORT: connect: connection refused

ID: go/connection-refused

Also available as: JSON · Markdown
82%Fix Rate
85%Confidence
50Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1 active

Root Cause

TCP connection refused. Target server not running or wrong host/port.

generic

Workarounds

  1. 95% success Verify the server is running on the expected host:port
    curl -v http://localhost:PORT  # or: ss -tlnp | grep PORT

    Sources: https://pkg.go.dev/net#Dial

  2. 88% success Check if using IPv6 [::1] vs IPv4 127.0.0.1 — server may only listen on one
    :1] vs IPv4 127.0.0.1 — server may only listen on one

    Sources: https://pkg.go.dev/net#Dial

  3. 82% success Add retry logic with backoff for services that start slowly
    import "time"
    
    func connectWithRetry(addr string, maxRetries int) (net.Conn, error) {
        var conn net.Conn
        var err error
        for i := 0; i < maxRetries; i++ {
            conn, err = net.DialTimeout("tcp", addr, 5*time.Second)
            if err == nil {
                return conn, nil
            }
            time.Sleep(time.Duration(1<<uint(i)) * time.Second)
        }
        return nil, err
    }

    Sources: https://pkg.go.dev/net#Dial

Dead Ends

Common approaches that don't work:

  1. Increase the connection timeout 80% fail

    Connection refused is immediate — timeout doesn't help

  2. Use net.DialUDP instead 90% fail

    If the server expects TCP, switching to UDP won't work

Error Chain

Frequently confused with: