mirror of
https://github.com/hanzoai/team.git
synced 2026-09-30 01:57:35 +00:00
The GET /api/v4/limits/server endpoint ran expensive active-user and
single-channel-guest count queries for every user on login and app
refresh, even though non-admins never receive those counts. The
single-channel-guest count is a full ChannelMembers scan, so this put a
full-table-scan query on a high-frequency hot path.
Add an includeUserCounts parameter to App.GetServerLimits so the user
and guest count queries only run when the caller needs them. The api4
handler passes the existing admin check, keeping the cheap
license-derived and post-history fields for all users while skipping the
expensive queries for non-admins. Internal callers that genuinely need
the counts (isAtUserLimit, user creation/activation) pass true.
(cherry picked from commit cdc10d47fe)
Co-authored-by: Ben Schumacher <[email protected]>
Co-authored-by: Hanzo Dev <[email protected]>
49 lines
1.5 KiB
Go
49 lines
1.5 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package api4
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
|
|
"github.com/mattermost/mattermost/server/public/shared/mlog"
|
|
)
|
|
|
|
func (api *API) InitLimits() {
|
|
api.BaseRoutes.Limits.Handle("/server", api.APISessionRequired(getServerLimits)).Methods(http.MethodGet)
|
|
}
|
|
|
|
func getServerLimits(c *Context, w http.ResponseWriter, r *http.Request) {
|
|
isAdmin := c.IsSystemAdmin() && c.App.SessionHasPermissionTo(*c.AppContext.Session(), model.PermissionSysconsoleReadUserManagementUsers)
|
|
|
|
// Only admins receive (and need) the user/guest counts, so only compute them for
|
|
// admins. This keeps the expensive count queries off the per-login/per-refresh hot
|
|
// path that non-admin clients hit via loadMe()/loadConfigAndMe().
|
|
serverLimits, err := c.App.GetServerLimits(isAdmin)
|
|
if err != nil {
|
|
c.Err = err
|
|
return
|
|
}
|
|
|
|
// Non-admin users only get message history limit information, no user count data
|
|
if !isAdmin {
|
|
limitedData := &model.ServerLimits{
|
|
MaxUsersLimit: 0,
|
|
MaxUsersHardLimit: 0,
|
|
ActiveUserCount: 0,
|
|
SingleChannelGuestCount: 0,
|
|
SingleChannelGuestLimit: 0,
|
|
LastAccessiblePostTime: serverLimits.LastAccessiblePostTime,
|
|
PostHistoryLimit: serverLimits.PostHistoryLimit,
|
|
}
|
|
serverLimits = limitedData
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(serverLimits); err != nil {
|
|
c.Logger.Warn("Error writing server limits response", mlog.Err(err))
|
|
}
|
|
}
|