From 867f8402658decc2ddd233da456366074fe3aba0 Mon Sep 17 00:00:00 2001 From: UUBulb <35923940+uubulb@users.noreply.github.com> Date: Sat, 23 Nov 2024 01:13:39 +0800 Subject: [PATCH] optimize sorting, fix a bug (#15) --- cmd/dashboard/controller/ws.go | 6 ++--- pkg/utils/utils.go | 37 +++++++++++++++++++++++++++++++ service/singleton/crontask.go | 8 ++----- service/singleton/ddns.go | 8 ++----- service/singleton/nat.go | 8 ++----- service/singleton/notification.go | 8 ++----- service/singleton/server.go | 1 + 7 files changed, 49 insertions(+), 27 deletions(-) diff --git a/cmd/dashboard/controller/ws.go b/cmd/dashboard/controller/ws.go index 2c7d282..a858ed3 100644 --- a/cmd/dashboard/controller/ws.go +++ b/cmd/dashboard/controller/ws.go @@ -106,9 +106,8 @@ func getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) { serverList = singleton.SortedServerListForGuest } - var servers []model.StreamServer - for i := 0; i < len(serverList); i++ { - server := serverList[i] + servers := make([]model.StreamServer, 0, len(serverList)) + for _, server := range serverList { servers = append(servers, model.StreamServer{ ID: server.ID, Name: server.Name, @@ -125,5 +124,6 @@ func getServerStat(c *gin.Context, withPublicNote bool) ([]byte, error) { Servers: servers, }) }) + return v.([]byte), err } diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 08f2766..7407129 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -114,3 +114,40 @@ func Itoa[T constraints.Integer](i T) string { return "" } } + +// From go1.23 + +// Compare returns +// +// -1 if x is less than y, +// 0 if x equals y, +// +1 if x is greater than y. +// +// For floating-point types, a NaN is considered less than any non-NaN, +// a NaN is considered equal to a NaN, and -0.0 is equal to 0.0. +func Compare[T constraints.Ordered](x, y T) int { + xNaN := isNaN(x) + yNaN := isNaN(y) + if xNaN { + if yNaN { + return 0 + } + return -1 + } + if yNaN { + return +1 + } + if x < y { + return -1 + } + if x > y { + return +1 + } + return 0 +} + +// isNaN reports whether x is a NaN without requiring the math package. +// This will always return false if T is not floating-point. +func isNaN[T constraints.Ordered](x T) bool { + return x != x +} diff --git a/service/singleton/crontask.go b/service/singleton/crontask.go index 7dc790e..1b85f8a 100644 --- a/service/singleton/crontask.go +++ b/service/singleton/crontask.go @@ -11,6 +11,7 @@ import ( "github.com/robfig/cron/v3" "github.com/naiba/nezha/model" + "github.com/naiba/nezha/pkg/utils" pb "github.com/naiba/nezha/proto" ) @@ -83,12 +84,7 @@ func UpdateCronList() { CronList = append(CronList, c) } slices.SortFunc(CronList, func(a, b *model.Cron) int { - if a.ID < b.ID { - return -1 - } else if a.ID == b.ID { - return 0 - } - return 1 + return utils.Compare(a.ID, b.ID) }) } diff --git a/service/singleton/ddns.go b/service/singleton/ddns.go index 2d2cc54..538a05e 100644 --- a/service/singleton/ddns.go +++ b/service/singleton/ddns.go @@ -12,6 +12,7 @@ import ( ddns2 "github.com/naiba/nezha/pkg/ddns" "github.com/naiba/nezha/pkg/ddns/dummy" "github.com/naiba/nezha/pkg/ddns/webhook" + "github.com/naiba/nezha/pkg/utils" ) var ( @@ -56,12 +57,7 @@ func UpdateDDNSList() { DDNSList = append(DDNSList, p) } slices.SortFunc(DDNSList, func(a, b *model.DDNSProfile) int { - if a.ID < b.ID { - return -1 - } else if a.ID == b.ID { - return 0 - } - return 1 + return utils.Compare(a.ID, b.ID) }) } diff --git a/service/singleton/nat.go b/service/singleton/nat.go index fe27eac..fc822ea 100644 --- a/service/singleton/nat.go +++ b/service/singleton/nat.go @@ -5,6 +5,7 @@ import ( "sync" "github.com/naiba/nezha/model" + "github.com/naiba/nezha/pkg/utils" ) var ( @@ -59,12 +60,7 @@ func UpdateNATList() { NATList = append(NATList, n) } slices.SortFunc(NATList, func(a, b *model.NAT) int { - if a.ID < b.ID { - return -1 - } else if a.ID == b.ID { - return 0 - } - return 1 + return utils.Compare(a.ID, b.ID) }) } diff --git a/service/singleton/notification.go b/service/singleton/notification.go index d2b26d8..c9f634a 100644 --- a/service/singleton/notification.go +++ b/service/singleton/notification.go @@ -8,6 +8,7 @@ import ( "time" "github.com/naiba/nezha/model" + "github.com/naiba/nezha/pkg/utils" ) const ( @@ -85,12 +86,7 @@ func UpdateNotificationList() { NotificationListSorted = append(NotificationListSorted, n) } slices.SortFunc(NotificationListSorted, func(a, b *model.Notification) int { - if a.ID < b.ID { - return -1 - } else if a.ID == b.ID { - return 0 - } - return 1 + return utils.Compare(a.ID, b.ID) }) } diff --git a/service/singleton/server.go b/service/singleton/server.go index c586024..286911a 100644 --- a/service/singleton/server.go +++ b/service/singleton/server.go @@ -46,6 +46,7 @@ func ReSortServer() { defer SortedServerLock.Unlock() SortedServerList = make([]*model.Server, 0, len(ServerList)) + SortedServerListForGuest = make([]*model.Server, 0) for _, s := range ServerList { SortedServerList = append(SortedServerList, s) if !s.HideForGuest {