{"record":{"id":"86990700ba606305","repo":"micro/go-micro","slug":"tcp-dial-s-w","errorCode":null,"errorMessage":"tcp dial %s: %w","messagePattern":"tcp dial (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"health/health.go","lineNumber":269,"sourceCode":"\n// PingCheck creates a check from a ping function (like sql.DB.Ping)\nfunc PingCheck(ping func() error) CheckFunc {\n\treturn func(ctx context.Context) error {\n\t\treturn ping()\n\t}\n}\n\n// PingContextCheck creates a check from a ping function that accepts context\nfunc PingContextCheck(ping func(context.Context) error) CheckFunc {\n\treturn ping\n}\n\n// TCPCheck creates a check that verifies TCP connectivity\nfunc TCPCheck(addr string, timeout time.Duration) CheckFunc {\n\treturn func(ctx context.Context) error {\n\t\tconn, err := net.DialTimeout(\"tcp\", addr, timeout)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"tcp dial %s: %w\", addr, err)\n\t\t}\n\t\tconn.Close()\n\t\treturn nil\n\t}\n}\n\n// HTTPCheck creates a check that verifies an HTTP endpoint returns 200\nfunc HTTPCheck(url string, timeout time.Duration) CheckFunc {\n\treturn func(ctx context.Context) error {\n\t\tclient := &http.Client{Timeout: timeout}\n\t\treq, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\tresp, err := client.Do(req)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"http get %s: %w\", url, err)\n\t\t}","sourceCodeStart":251,"sourceCodeEnd":287,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/health/health.go#L251-L287","documentation":"TCPCheck returns a CheckFunc used by the health package that attempts net.DialTimeout to the given address. If the dial fails (connection refused, timeout, DNS failure, network unreachable), the error is wrapped as \"tcp dial <addr>: <underlying error>\" so the health check reports failure with the address and cause included.","triggerScenarios":"The health check runs and net.DialTimeout cannot establish a TCP connection within the timeout: target service is down, listening on a different port, DNS name doesn't resolve, firewall drops packets, or the timeout is shorter than the connection setup time.","commonSituations":"Dependency service crashed or not yet started (startup ordering); wrong host/port in config; Kubernetes service DNS not ready; firewall/security group blocking the port; timeout set too aggressively (e.g. 100ms) for a remote dependency.","solutions":["Read the wrapped cause: connection refused means nothing is listening; i/o timeout means network/timeout issue; no such host means DNS failure.","Verify the target service is running and listening on the configured addr (netstat/ss or telnet/nc).","Correct the address in configuration if the host/port is wrong.","Increase the timeout parameter, and configure the health framework's retry/interval so transient startup delays don't fail the check."],"exampleFix":"// before\ncheck := health.TCPCheck(\"db:5432\", 100*time.Millisecond)\n// after\ncheck := health.TCPCheck(\"postgres.default.svc.cluster.local:5432\", 3*time.Second)","handlingStrategy":"retry","validationCode":"// probe before registering the check\nconn, err := net.DialTimeout(\"tcp\", addr, timeout)\nif err != nil {\n    return fmt.Errorf(\"dependency %s unreachable: %w\", addr, err)\n}\nconn.Close()","typeGuard":null,"tryCatchPattern":"if err := check(ctx); err != nil {\n    var nerr net.Error\n    if errors.As(err, &nerr) && nerr.Timeout() {\n        // increase timeout / retry\n    } else if strings.Contains(err.Error(), \"connection refused\") {\n        // dependency down: wait for startup, then retry\n    }\n}","preventionTips":["Set realistic dial timeouts (seconds, not milliseconds) for remote dependencies.","Use resolvable, environment-correct hostnames/ports in health check config.","Configure health check intervals/retries so startup races don't flip status.","Alert on the specific wrapped cause (refused vs timeout vs DNS) for fast diagnosis."],"tags":["health","tcp","network","connectivity"],"backgroundTag":"connection-refused","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}