{"record":{"id":"fb3bccace138d8b1","repo":"geektutu/7days-golang","slug":"rpc-client-connect-timeout-expect-within-s-fb3bcc","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/day7-registry/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/day7-registry/client.go#L255-L291","documentation":"Day7-registry dialTimeout: with opt.ConnectTimeout > 0, if the async dial + options handshake does not complete in time, this timeout error is returned while the background attempt is abandoned. ConnectTimeout == 0 disables the deadline (blocks until result).","triggerScenarios":"Dial/DialHTTP/XDial to a dead, wrong, or slow endpoint exceeding ConnectTimeout; the heart-beat-enabled server is down; an anonymous goroutine dial (via registry-selected server) fails to finish in time.","commonSituations":"Registry returning stale server addresses; server crashed between registry registration and dial; tight ConnectTimeout in production configs; network partitions.","solutions":["Confirm the target address from the registry is alive; refresh/fix registry entries.","Raise opt.ConnectTimeout (DefaultOption uses 5s) in the passed Option.","Investigate network connectivity; retry after the server recovers."],"exampleFix":"// before\nclient, err := geerpc.XDial(\"tcp@10.0.0.1:9999\") // 5s default too short\n// after\nopt := geerpc.DefaultOption\nopt.ConnectTimeout = 15 * time.Second\nclient, err := geerpc.XDial(\"tcp@10.0.0.1:9999\", opt)","handlingStrategy":"retry","validationCode":"opt := geerpc.DefaultOption\nopt.ConnectTimeout = 15 * time.Second\n// pre-flight check against registry-selected addresses:\nif _, err := net.DialTimeout(\"tcp\", addr, 3*time.Second); err != nil {\n    // pick another server from the registry\n}","typeGuard":null,"tryCatchPattern":"client, err := geerpc.XDial(\"tcp@\"+addr, opt)\nif err != nil {\n    if strings.Contains(err.Error(), \"connect timeout\") {\n        client, err = dialWithRetry(addr, opt, 3) // exponential backoff\n    }\n    if err != nil { return err }\n}","preventionTips":["Keep registry entries fresh with heart-beats and TTLs so dead servers drop out.","Pick another server from the registry on timeout instead of hard-failing.","Tune ConnectTimeout to your network's p99 latency.","Watch for patterns of timeouts against one address — that node is likely 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"}