Skip to content

Commit

Permalink
💈 api: sort user list and stats by username
Browse files Browse the repository at this point in the history
  • Loading branch information
database64128 committed Mar 23, 2023
1 parent 4d47fb1 commit 2820a12
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions cred/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/database64128/shadowsocks-go/maps"
"github.com/database64128/shadowsocks-go/mmap"
"github.com/database64128/shadowsocks-go/slices"
"github.com/database64128/shadowsocks-go/ss2022"
"go.uber.org/zap"
)
Expand Down Expand Up @@ -44,6 +45,11 @@ type UserCredential struct {
UPSK []byte `json:"uPSK"`
}

// Less is useful for sorting user credentials by username.
func (uc UserCredential) Less(other UserCredential) bool {
return uc.Name < other.Name
}

type cachedUserCredential struct {
uPSK []byte
uPSKHash [ss2022.IdentityHeaderLength]byte
Expand All @@ -60,6 +66,7 @@ func (s *ManagedServer) Credentials() []UserCredential {
})
}
s.mu.RUnlock()
slices.SortFunc(ucs, UserCredential.Less)
return ucs
}

Expand Down
9 changes: 9 additions & 0 deletions stats/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package stats
import (
"sync"
"sync/atomic"

"github.com/database64128/shadowsocks-go/slices"
)

type trafficCollector struct {
Expand Down Expand Up @@ -82,6 +84,11 @@ type User struct {
Traffic
}

// Less is useful for sorting users by name.
func (u User) Less(other User) bool {
return u.Name < other.Name
}

func (uc *userCollector) snapshot(username string) User {
return User{
Name: username,
Expand Down Expand Up @@ -164,6 +171,7 @@ func (sc *serverCollector) Snapshot() (s Server) {
s.Users = append(s.Users, u)
}
sc.mu.RUnlock()
slices.SortFunc(s.Users, User.Less)
return
}

Expand All @@ -178,6 +186,7 @@ func (sc *serverCollector) SnapshotAndReset() (s Server) {
s.Users = append(s.Users, u)
}
sc.mu.RUnlock()
slices.SortFunc(s.Users, User.Less)
return
}

Expand Down

0 comments on commit 2820a12

Please sign in to comment.