geektutu/7days-golang · error

rpc client: connect timeout: expect within %s

Error message

rpc client: connect timeout: expect within %s

What it means

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.

Source

Thrown at gee-rpc/day6-load-balance/client.go:273

	}
	// close the connection if client is nil
	defer func() {
		if err != nil {
			_ = conn.Close()
		}
	}()
	ch := make(chan clientResult)
	go func() {
		client, err := f(conn, opt)
		ch <- clientResult{client: client, err: err}
	}()
	if opt.ConnectTimeout == 0 {
		result := <-ch
		return result.client, result.err
	}
	select {
	case <-time.After(opt.ConnectTimeout):
		return nil, fmt.Errorf("rpc client: connect timeout: expect within %s", opt.ConnectTimeout)
	case result := <-ch:
		return result.client, result.err
	}
}

// Dial connects to an RPC server at the specified network address
func Dial(network, address string, opts ...*Option) (*Client, error) {
	return dialTimeout(NewClient, network, address, opts...)
}

// NewHTTPClient new a Client instance via HTTP as transport protocol
func NewHTTPClient(conn net.Conn, opt *Option) (*Client, error) {
	_, _ = io.WriteString(conn, fmt.Sprintf("CONNECT %s HTTP/1.0\n\n", defaultRPCPath))

	// Require successful HTTP response
	// before switching to RPC protocol.
	resp, err := http.ReadResponse(bufio.NewReader(conn), &http.Request{Method: "CONNECT"})
	if err == nil && resp.Status == connected {

View on GitHub (pinned to cf36443821)

Solutions

  1. Verify the server is running and reachable at the address (telnet/curl the port).
  2. Increase opt.ConnectTimeout in the Option passed to Dial.
  3. If no timeout is desired, set ConnectTimeout to 0 and handle hanging separately.

Example fix

// before
client, err := geerpc.Dial("tcp", "10.0.0.1:9999") // ConnectTimeout too small
// after
opt := geerpc.DefaultOption
opt.ConnectTimeout = 10 * time.Second
client, err := geerpc.Dial("tcp", "10.0.0.1:9999", opt)
Defensive patterns

Strategy: retry

Validate before calling

opt := geerpc.DefaultOption
opt.ConnectTimeout = 10 * time.Second
// optionally pre-check reachability before dialing:
conn, err := net.DialTimeout("tcp", addr, opt.ConnectTimeout)
if err != nil { /* address unreachable; fix before geerpc.Dial */ } else { conn.Close() }

Try / catch

client, err := geerpc.Dial("tcp", addr, opt)
if err != nil {
    if strings.Contains(err.Error(), "connect timeout") {
        // retry with backoff or fail over to another server
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: Wrong host/port in config; server process down or blocked; network latency/防火墙 dropping SYN; very short ConnectTimeout set in Options.

Understand the failure class

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/5b83831851b14514. Report an issue: GitHub.