geektutu/7days-golang · error
rpc discovery: not supported select mode
Error message
rpc discovery: not supported select mode
What it means
MultiServersDiscovery.Get only supports RandomSelect and RoundRobinSelect; any other SelectMode value falls to the default branch and returns this error. It indicates an invalid load-balancing mode was passed.
Source
Thrown at gee-rpc/day7-registry/xclient/discovery.go:65
}
// Get a server according to mode
func (d *MultiServersDiscovery) Get(mode SelectMode) (string, error) {
d.mu.Lock()
defer d.mu.Unlock()
n := len(d.servers)
if n == 0 {
return "", errors.New("rpc discovery: no available servers")
}
switch mode {
case RandomSelect:
return d.servers[d.r.Intn(n)], nil
case RoundRobinSelect:
s := d.servers[d.index%n] // servers could be updated, so mode n to ensure safety
d.index = (d.index + 1) % n
return s, nil
default:
return "", errors.New("rpc discovery: not supported select mode")
}
}
// returns all servers in discovery
func (d *MultiServersDiscovery) GetAll() ([]string, error) {
d.mu.RLock()
defer d.mu.RUnlock()
// return a copy of d.servers
servers := make([]string, len(d.servers), len(d.servers))
copy(servers, d.servers)
return servers, nil
}
// NewMultiServerDiscovery creates a MultiServersDiscovery instance
func NewMultiServerDiscovery(servers []string) *MultiServersDiscovery {
d := &MultiServersDiscovery{
servers: servers,
r: rand.New(rand.NewSource(time.Now().UnixNano())),View on GitHub (pinned to cf36443821)
Solutions
- Use only the exported constants: xclient.RandomSelect or xclient.RoundRobinSelect when creating XClient or calling Get
- Fix any hardcoded numeric SelectMode values to the defined enum members
- If a new strategy is needed, extend SelectMode and add a case in Get rather than passing an unknown value
Example fix
// before
client := xclient.NewXClient(d, 3, geerpc.GeeOption{}) // unsupported mode
// after
client := xclient.NewXClient(d, xclient.RoundRobinSelect, geerpc.GeeOption{}) Defensive patterns
Strategy: validation
Validate before calling
func validSelectMode(m xclient.SelectMode) bool {
return m == xclient.RandomSelect || m == xclient.RoundRobinSelect
}
// reject before constructing the client
if !validSelectMode(mode) { return errors.New("unsupported select mode") } Try / catch
xc, err := xclient.NewXClient(d, mode, opt)
if err != nil { return err }
_ = xc
// and when calling Get directly:
s, err := d.Get(mode)
if err != nil && strings.Contains(err.Error(), "not supported select mode") {
return fmt.Errorf("mode %d unsupported: %w", mode, err)
} Prevention
- Only use the exported SelectMode constants, never raw numbers
- Add a switch exhaustiveness check or unit test enumerating all valid modes
- Document supported modes at the call site where mode is configured
When it happens
Trigger: Passing an out-of-range or unknown SelectMode value to Get directly, or constructing XClient with a SelectMode other than the defined constants (e.g. 5) — note that with a single server, XClient may bypass Get, so the error appears once multiple servers exist.
Common situations: Using a numeric literal for SelectMode instead of the exported constants; custom enum values added client-side that the discovery does not know; copy-pasted code from another framework with different mode enums.
Related errors
- rpc discovery: no available servers
- rpc client: call failed:
- number of options is more than 1
- rpc server: service/method request ill-formed:
- rpc server: can't find service
AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03).
Data as JSON: /api/errors/bcd3262fcafe0b61.
Report an issue: GitHub.