{"record":{"id":"4268c6a55b2f3ed3","repo":"geektutu/7days-golang","slug":"rpc-discovery-not-supported-select-mode","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/day6-load-balance/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/day6-load-balance/xclient/discovery.go#L47-L83","documentation":"Guard in MultiServersDiscovery.Get: the SelectMode passed is neither RandomSelect nor RoundRobinSelect, so no load-balancing strategy applies. It fires when a caller uses an unimplemented or invalid select mode constant.","triggerScenarios":"Calling Get(mode) with an int SelectMode outside the defined constants (e.g. 2, -1, or a value from a different package's enum), or passing the wrong enum type cast to SelectMode.","commonSituations":"Config-driven mode selection parsing user input into an unvalidated int, mixing up SelectMode constants from another load-balancer package, or extending the enum locally without adding a case in Get.","solutions":["Use only the exported constants: xclient.RandomSelect or xclient.RoundRobinSelect.","Validate any externally supplied mode value against the supported constants before calling Get.","If you need a new strategy, add a case to MultiServersDiscovery.Get and define a new SelectMode constant."],"exampleFix":"// before\nmode := xclient.SelectMode(cfg.Mode) // arbitrary int\naddr, err := d.Get(mode)\n// after\nvar mode xclient.SelectMode = xclient.RoundRobinSelect\nif cfg.Mode == \"random\" { mode = xclient.RandomSelect }\naddr, err := d.Get(mode)","handlingStrategy":"validation","validationCode":"func validMode(m xclient.SelectMode) bool {\n    return m == xclient.RandomSelect || m == xclient.RoundRobinSelect\n}","typeGuard":null,"tryCatchPattern":"if !validMode(mode) { return errors.New(\"mode must be RandomSelect or RoundRobinSelect\") }\naddr, err := d.Get(mode)","preventionTips":["Only use the exported SelectMode constants, never raw ints.","Validate config-parsed mode values at startup, not at call time.","Centralize mode selection in one helper that defaults to RoundRobinSelect."],"tags":["rpc","discovery","invalid-argument","load-balance"],"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"}