Skip to content

Conversation

@fengyuchuanshen
Copy link

There is a new function added in the go1.21 standard library, which can make the code more concise and easy to read.

@coderabbitai
Copy link

coderabbitai bot commented Sep 11, 2025

Walkthrough

Two files were updated to replace explicit loops for membership checks with slices.Contains, adding the slices import. Logic and public APIs remain unchanged.

Changes

Cohort / File(s) Summary
Membership check refactor
cluster/hostname.go, pkg/apis/akash.network/v2beta2/node_info.go
Added slices import and replaced manual loops with slices.Contains(...) for hostname and storage class membership checks. No public API changes.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks (3 passed)

✅ Passed checks (3 passed)
Check name Status Explanation
Title Check ✅ Passed The title "refactor: use slices.Contains to simplify code" succinctly and accurately summarizes the primary change (replacing explicit membership loops with slices.Contains) and is concise, focused, and free of noise.
Description Check ✅ Passed The description clearly references the Go 1.21 slices.Contains function and explains the intent to use it to make the code more concise and readable, which matches the changes in the provided summaries.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.

Poem

I nibbled loops to tidy lines, so clean, so keen—
A hop to Contains, the swiftest I’ve seen.
Hostnames and classes, checked in a blink,
Less gnawing of code, more time to think.
Thump-thump! Refactors neat—what a treat! 🥕✨

✨ Finishing touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
pkg/apis/akash.network/v2beta2/node_info.go (1)

5-8: Likely JSON tag bug: Model uses json:"string".

Assuming the field should marshal as “model”, fix the tag.

Apply:

 type GPUCapabilities struct {
   Vendor string `json:"vendor" capabilities:"vendor"`
-  Model  string `json:"string" capabilities:"model"`
+  Model  string `json:"model" capabilities:"model"`
 }
🧹 Nitpick comments (4)
cluster/hostname.go (4)

377-381: Fix error message to report the blocked domain, not the hostname.

Minor UX nit; the %q currently prints hostname.

-      return fmt.Errorf("%w: domain %q is blocked by this provider", ErrHostnameNotAllowed, hostname)
+      return fmt.Errorf("%w: domain %q is blocked by this provider", ErrHostnameNotAllowed, blockedDomain)

272-274: Optional: precompute a set for exact hostnames to make checks O(1).

If isHostnameBlocked is on hot paths, consider a map alongside the slice.

Example:

 type hostnameService struct {
   inUse map[string]hostnameID
@@
-  blockedHostnames []string
+  blockedHostnames []string
+  blockedHostnamesSet map[string]struct{}
   blockedDomains   []string
 }

Populate in newHostnameService:

 blockedHostnames := make([]string, 0)
 blockedDomains := make([]string, 0)
+blockedSet := make(map[string]struct{})
@@
       blockedHostnames = append(blockedHostnames, name[1:])
     } else {
       blockedHostnames = append(blockedHostnames, name)
     }
   }
+  for _, h := range blockedHostnames { blockedSet[h] = struct{}{} }
@@
   hs := &hostnameService{
@@
-    blockedHostnames: blockedHostnames,
+    blockedHostnames: blockedHostnames,
+    blockedHostnamesSet: blockedSet,

Use it in isHostnameBlocked:

- if slices.Contains(hs.blockedHostnames, hostname) {
+ if _, ok := hs.blockedHostnamesSet[hostname]; ok {

372-385: Optional: normalize case in isHostnameBlocked for safety.

Callers lower-case today, but normalizing here removes that coupling.

 func (hs *hostnameService) isHostnameBlocked(hostname string) error {
+  hostname = strings.ToLower(hostname)
   if slices.Contains(hs.blockedHostnames, hostname) {

62-71: Replace opaque error message "bob".

Unclear to operators. Prefer a descriptive error.

-  return nil, errors.New("bob")
+  return nil, errors.New("wait canceled")

If this path represents a timeout, consider wiring a context with deadline and returning context.DeadlineExceeded.

📜 Review details

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 01bf5cc and 4e4896b.

📒 Files selected for processing (2)
  • cluster/hostname.go (2 hunks)
  • pkg/apis/akash.network/v2beta2/node_info.go (2 hunks)
🔇 Additional comments (4)
pkg/apis/akash.network/v2beta2/node_info.go (2)

19-21: LGTM: simpler, equivalent semantics.

slices.Contains preserves prior behavior for nil/empty slices and improves readability.


3-3: Confirmed: Go ≥1.21 — no action required. go.mod contains "go 1.24.2" and "toolchain go1.24.6". Workflows use actions/setup-go@v5 with GOVERSION populated by script/tools.sh gotoolchain (which reads the toolchain from go.mod), so CI will install go1.24.6.

cluster/hostname.go (2)

6-6: LGTM: stdlib slices import is appropriate.


372-375: LGTM: direct membership check reads better.

Same O(n) behavior as the prior loop; clearer intent.

@fengyuchuanshen
Copy link
Author

@troian @cloud-j-luna Hi, Could you please review this PR at your convenience? Thank you very much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant