fix: several client list bugs

This commit is contained in:
0x1a8510f2 2023-04-16 03:37:30 +01:00
parent 22fee5b76f
commit 305f46ccbe
Signed by: 0x1a8510f2
GPG Key ID: 1C692E355D76775D
3 changed files with 16 additions and 2 deletions

View File

@ -55,6 +55,11 @@ func handleClients(r *http.Request, w http.ResponseWriter, s *state) {
return
}
// Make sure the requested page does not exceed the max.
if reqdata.Limit > MAX_DATA_PAGE_SIZE {
reqdata.Limit = MAX_DATA_PAGE_SIZE
}
// Collect necessary information.
clients := s.GetClients(reqdata.Offset, reqdata.Limit)

View File

@ -18,6 +18,8 @@ const (
STATE_CLEANUP_INTERVAL = 30 * time.Second
STATE_CLIENT_EXPIRY_DELAY = proto.HEARTBEAT_MARK_DEAD_DELAY
STATE_REQUEST_EXPIRY_DELAY = 10 * time.Minute
MAX_DATA_PAGE_SIZE = 200
)
type Config struct {

View File

@ -81,6 +81,13 @@ func (l *clientList) GetPage(offset, limit int) []*client {
return []*client{}
}
// If the remainder of the client list after the offset is
// smaller than the limit, reduce the limit to the size of that
// remainder to avoid nulls in the returned data.
if maxLimit := len(l.clients) - offset; maxLimit < limit {
limit = maxLimit
}
page := make([]*client, limit)
current := l.head
for i := 0; i < offset+limit; i++ {
@ -88,8 +95,8 @@ func (l *clientList) GetPage(offset, limit int) []*client {
break
}
if i > offset && i < offset+limit {
page = append(page, current)
if i >= offset && i < offset+limit {
page[i-offset] = current
}
current = current.next