{"record":{"id":"bcd3262fcafe0b61","repo":"geektutu/7days-golang","slug":"rpc-discovery-not-supported-select-mode-bcd326","errorCode":null,"errorMessage":"rpc discovery: not supported select mode","messagePattern":"rpc discovery: not supported select mode","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"gee-rpc/day7-registry/xclient/discovery.go","lineNumber":65,"sourceCode":"}\n\n// Get a server according to mode\nfunc (d *MultiServersDiscovery) Get(mode SelectMode) (string, error) {\n\td.mu.Lock()\n\tdefer d.mu.Unlock()\n\tn := len(d.servers)\n\tif n == 0 {\n\t\treturn \"\", errors.New(\"rpc discovery: no available servers\")\n\t}\n\tswitch mode {\n\tcase RandomSelect:\n\t\treturn d.servers[d.r.Intn(n)], nil\n\tcase RoundRobinSelect:\n\t\ts := d.servers[d.index%n] // servers could be updated, so mode n to ensure safety\n\t\td.index = (d.index + 1) % n\n\t\treturn s, nil\n\tdefault:\n\t\treturn \"\", errors.New(\"rpc discovery: not supported select mode\")\n\t}\n}\n\n// returns all servers in discovery\nfunc (d *MultiServersDiscovery) GetAll() ([]string, error) {\n\td.mu.RLock()\n\tdefer d.mu.RUnlock()\n\t// return a copy of d.servers\n\tservers := make([]string, len(d.servers), len(d.servers))\n\tcopy(servers, d.servers)\n\treturn servers, nil\n}\n\n// NewMultiServerDiscovery creates a MultiServersDiscovery instance\nfunc NewMultiServerDiscovery(servers []string) *MultiServersDiscovery {\n\td := &MultiServersDiscovery{\n\t\tservers: servers,\n\t\tr:       rand.New(rand.NewSource(time.Now().UnixNano())),","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/geektutu/7days-golang/blob/cf3644382101dc13e7fd92e8f5c66cabc51bcd3b/gee-rpc/day7-registry/xclient/discovery.go#L47-L83","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nclient := xclient.NewXClient(d, 3, geerpc.GeeOption{}) // unsupported mode\n// after\nclient := xclient.NewXClient(d, xclient.RoundRobinSelect, geerpc.GeeOption{})","handlingStrategy":"validation","validationCode":"func validSelectMode(m xclient.SelectMode) bool {\n    return m == xclient.RandomSelect || m == xclient.RoundRobinSelect\n}\n// reject before constructing the client\nif !validSelectMode(mode) { return errors.New(\"unsupported select mode\") }","typeGuard":null,"tryCatchPattern":"xc, err := xclient.NewXClient(d, mode, opt)\nif err != nil { return err }\n_ = xc\n// and when calling Get directly:\ns, err := d.Get(mode)\nif err != nil && strings.Contains(err.Error(), \"not supported select mode\") {\n    return fmt.Errorf(\"mode %d unsupported: %w\", mode, err)\n}","preventionTips":["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"],"tags":["go","rpc","load-balancing","invalid-argument"],"backgroundTag":"unsupported-select-mode","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"}