{"record":{"id":"5b83831851b14514","repo":"geektutu/7days-golang","slug":"rpc-client-connect-timeout-expect-within-s-5b8383","errorCode":null,"errorMessage":"rpc client: connect timeout: expect within %s","messagePattern":"rpc client: connect timeout: expect within (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-rpc/day6-load-balance/client.go","lineNumber":273,"sourceCode":"\t}\n\t// close the connection if client is nil\n\tdefer func() {\n\t\tif err != nil {\n\t\t\t_ = conn.Close()\n\t\t}\n\t}()\n\tch := make(chan clientResult)\n\tgo func() {\n\t\tclient, err := f(conn, opt)\n\t\tch <- clientResult{client: client, err: err}\n\t}()\n\tif opt.ConnectTimeout == 0 {\n\t\tresult := <-ch\n\t\treturn result.client, result.err\n\t}\n\tselect {\n\tcase <-time.After(opt.ConnectTimeout):\n\t\treturn nil, fmt.Errorf(\"rpc client: connect timeout: expect within %s\", opt.ConnectTimeout)\n\tcase result := <-ch:\n\t\treturn result.client, result.err\n\t}\n}\n\n// Dial connects to an RPC server at the specified network address\nfunc Dial(network, address string, opts ...*Option) (*Client, error) {\n\treturn dialTimeout(NewClient, network, address, opts...)\n}\n\n// NewHTTPClient new a Client instance via HTTP as transport protocol\nfunc NewHTTPClient(conn net.Conn, opt *Option) (*Client, error) {\n\t_, _ = io.WriteString(conn, fmt.Sprintf(\"CONNECT %s HTTP/1.0\\n\\n\", defaultRPCPath))\n\n\t// Require successful HTTP response\n\t// before switching to RPC protocol.\n\tresp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: \"CONNECT\"})\n\tif err == nil && resp.Status == connected {","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/geektutu/7days-golang/blob/cf3644382101dc13e7fd92e8f5c66cabc51bcd3b/gee-rpc/day6-load-balance/client.go#L255-L291","documentation":"dialTimeout dials asynchronously and, when opt.ConnectTimeout is non-zero, abandons the attempt if the connection isn't established within that window. The returned error indicates the client never completed the handshake (options exchange) in time. It is a client-side timeout, not a server refusal.","triggerScenarios":"Dial/DialHTTP/XDial against an unreachable or overloaded host where the TCP connect plus option negotiation exceeds opt.ConnectTimeout (default 5s); ConnectTimeout set to 0 means wait indefinitely instead.","commonSituations":"Wrong host/port in config; server process down or blocked; network latency/防火墙 dropping SYN; very short ConnectTimeout set in Options.","solutions":["Verify the server is running and reachable at the address (telnet/curl the port).","Increase opt.ConnectTimeout in the Option passed to Dial.","If no timeout is desired, set ConnectTimeout to 0 and handle hanging separately."],"exampleFix":"// before\nclient, err := geerpc.Dial(\"tcp\", \"10.0.0.1:9999\") // ConnectTimeout too small\n// after\nopt := geerpc.DefaultOption\nopt.ConnectTimeout = 10 * time.Second\nclient, err := geerpc.Dial(\"tcp\", \"10.0.0.1:9999\", opt)","handlingStrategy":"retry","validationCode":"opt := geerpc.DefaultOption\nopt.ConnectTimeout = 10 * time.Second\n// optionally pre-check reachability before dialing:\nconn, err := net.DialTimeout(\"tcp\", addr, opt.ConnectTimeout)\nif err != nil { /* address unreachable; fix before geerpc.Dial */ } else { conn.Close() }","typeGuard":null,"tryCatchPattern":"client, err := geerpc.Dial(\"tcp\", addr, opt)\nif err != nil {\n    if strings.Contains(err.Error(), \"connect timeout\") {\n        // retry with backoff or fail over to another server\n    }\n    return err\n}","preventionTips":["Monitor server availability and refresh service addresses.","Set ConnectTimeout deliberately instead of relying on defaults.","Add retry with backoff around dialing for flaky networks.","Alert on repeated dial timeouts — usually means the server is down."],"tags":["rpc","timeout","network"],"backgroundTag":"connection-timeout","analyzedSha":"cf3644382101dc13e7fd92e8f5c66cabc51bcd3b","analyzedAt":"2026-09-03T18:31:24.087Z","contentChangedAt":"2026-09-03T18:31:24.087Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}