Signed-off-by: crystalstall <crystalruby@qq.com>

refactor: using slices.Contains to simplify the code

Signed-off-by: crystalstall <crystalruby@qq.com>
This commit is contained in:
crystalstall 2025-01-06 16:13:18 +08:00
parent a441ad771e
commit 616914abe2
3 changed files with 6 additions and 23 deletions

View file

@ -19,6 +19,7 @@ import (
"fmt"
"log/slog"
"net"
"slices"
"strconv"
"strings"
"time"
@ -248,12 +249,7 @@ func (d *Discovery) shouldWatchFromName(name string) bool {
return true
}
for _, sn := range d.watchedServices {
if sn == name {
return true
}
}
return false
return slices.Contains(d.watchedServices, name)
}
// shouldWatchFromTags returns whether the service of the given name should be watched based on its tags.

View file

@ -221,7 +221,7 @@ type BlockMetaCompaction struct {
}
func (bm *BlockMetaCompaction) SetOutOfOrder() {
if bm.containsHint(CompactionHintFromOutOfOrder) {
if bm.FromOutOfOrder() {
return
}
bm.Hints = append(bm.Hints, CompactionHintFromOutOfOrder)
@ -229,16 +229,7 @@ func (bm *BlockMetaCompaction) SetOutOfOrder() {
}
func (bm *BlockMetaCompaction) FromOutOfOrder() bool {
return bm.containsHint(CompactionHintFromOutOfOrder)
}
func (bm *BlockMetaCompaction) containsHint(hint string) bool {
for _, h := range bm.Hints {
if h == hint {
return true
}
}
return false
return slices.Contains(bm.Hints, CompactionHintFromOutOfOrder)
}
const (

View file

@ -15,6 +15,7 @@ package testutil
import (
"net"
"slices"
"sync"
"testing"
)
@ -48,12 +49,7 @@ func RandomUnprivilegedPort(t *testing.T) int {
}
func portWasUsed(port int) bool {
for _, usedPort := range usedPorts {
if port == usedPort {
return true
}
}
return false
return slices.Contains(usedPorts, port)
}
func getPort() (int, error) {