fix: total clients

This commit is contained in:
0x1a8510f2 2023-04-16 03:47:20 +01:00
parent 305f46ccbe
commit dcc522bc7f
Signed by: 0x1a8510f2
GPG Key ID: 1C692E355D76775D
2 changed files with 7 additions and 7 deletions

View File

@ -61,12 +61,12 @@ func handleClients(r *http.Request, w http.ResponseWriter, s *state) {
}
// Collect necessary information.
clients := s.GetClients(reqdata.Offset, reqdata.Limit)
clients, totalClients := s.GetClients(reqdata.Offset, reqdata.Limit)
// Build response data.
data, err := json.Marshal(map[string]any{
"clients": clients,
"total": len(clients),
"total": totalClients,
})
if err != nil {
panic(fmt.Sprintf("error while generating `clients` API response: %v", err))

View File

@ -76,9 +76,9 @@ func (l *clientList) Get(id string) (*client, bool) {
return c, ok
}
func (l *clientList) GetPage(offset, limit int) []*client {
if offset > len(l.clients) {
return []*client{}
func (l *clientList) GetPage(offset, limit int) ([]*client, int) {
if totalClients := len(l.clients); offset > totalClients {
return []*client{}, totalClients
}
// If the remainder of the client list after the offset is
@ -101,7 +101,7 @@ func (l *clientList) GetPage(offset, limit int) []*client {
current = current.next
}
return page
return page, len(l.clients)
}
type request struct {
@ -199,7 +199,7 @@ func (s *state) Prune() {
wg.Wait()
}
func (s *state) GetClients(offset, limit int) []*client {
func (s *state) GetClients(offset, limit int) ([]*client, int) {
s.clientsMutex.Lock()
defer s.clientsMutex.Unlock()