From 616914abe22ae745349b1891ba8065c2fd4abf4b Mon Sep 17 00:00:00 2001 From: crystalstall Date: Mon, 6 Jan 2025 16:13:18 +0800 Subject: [PATCH] Signed-off-by: crystalstall refactor: using slices.Contains to simplify the code Signed-off-by: crystalstall --- discovery/consul/consul.go | 8 ++------ tsdb/block.go | 13 ++----------- util/testutil/port.go | 8 ++------ 3 files changed, 6 insertions(+), 23 deletions(-) diff --git a/discovery/consul/consul.go b/discovery/consul/consul.go index 33b82d23a..4c8de6e29 100644 --- a/discovery/consul/consul.go +++ b/discovery/consul/consul.go @@ -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. diff --git a/tsdb/block.go b/tsdb/block.go index 6483f8d8b..4ffd2463c 100644 --- a/tsdb/block.go +++ b/tsdb/block.go @@ -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 ( diff --git a/util/testutil/port.go b/util/testutil/port.go index 7cf4cf1cc..91c129174 100644 --- a/util/testutil/port.go +++ b/util/testutil/port.go @@ -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) {