MM-61225: Revert session pooling (#28901)

This originated from https://github.com/mattermost/mattermost/issues/15249.

However, the original idea was discarded https://github.com/mattermost/mattermost/issues/15249#issuecomment-709713065
as being too complicated to implement. Then I had another
idea to implement it just for session objects.

My thinking was that since every single request allocates a new
session struct, it would be good to use a sync.Pool for that.

However, 4 years later, now we know that the primary bottleneck
in app performance comes from websocket event marshalling.
Therefore, while it would be good to do this, it is difficult
to do it correctly (as shown by the numerous racy tests).

Hence, reverting this.

```release-note
NONE
```
Этот коммит содержится в:
Agniva De Sarker
2024-10-24 22:50:44 +05:30
коммит произвёл GitHub
родитель a8cf496e31
Коммит 6075b1cd4e
12 изменённых файлов: 21 добавлений и 86 удалений

Просмотреть файл

@@ -50,7 +50,6 @@ type PlatformService struct {
cacheProvider cache.Provider
statusCache cache.Cache
sessionCache cache.Cache
sessionPool sync.Pool
asymmetricSigningKey atomic.Pointer[ecdsa.PrivateKey]
clientConfig atomic.Value
@@ -124,11 +123,6 @@ func New(sc ServiceConfig, options ...Option) (*PlatformService, error) {
WebSocketRouter: &WebSocketRouter{
handlers: make(map[string]webSocketHandler),
},
sessionPool: sync.Pool{
New: func() any {
return &model.Session{}
},
},
licenseListeners: map[string]func(*model.License, *model.License){},
additionalClusterHandlers: map[model.ClusterEvent]einterfaces.ClusterMessageHandler{},
}

Просмотреть файл

@@ -14,19 +14,6 @@ import (
"github.com/mattermost/mattermost/server/v8/platform/services/cache"
)
func (ps *PlatformService) ReturnSessionToPool(session *model.Session) {
if session != nil {
session.Id = ""
// All existing prop fields are cleared once the session is retrieved from the pool.
// To speed up that process, clear the props here to avoid doing that in the hot path.
//
// If the request handler spawns a goroutine that uses the session, it might race with this code.
// In that case, the handler should copy the session and use the copy in the goroutine.
clear(session.Props)
ps.sessionPool.Put(session)
}
}
func (ps *PlatformService) CreateSession(c request.CTX, session *model.Session) (*model.Session, error) {
session.Token = ""
@@ -133,8 +120,8 @@ func (ps *PlatformService) ClearAllUsersSessionCache() {
}
func (ps *PlatformService) GetSession(c request.CTX, token string) (*model.Session, error) {
var session = ps.sessionPool.Get().(*model.Session)
if err := ps.sessionCache.Get(token, session); err == nil {
var session model.Session
if err := ps.sessionCache.Get(token, &session); err == nil {
if m := ps.metricsIFace; m != nil {
m.IncrementMemCacheHitCounterSession()
}
@@ -145,7 +132,7 @@ func (ps *PlatformService) GetSession(c request.CTX, token string) (*model.Sessi
}
if session.Id != "" {
return session, nil
return &session, nil
}
return ps.GetSessionContext(c, token)
@@ -206,8 +193,6 @@ func (ps *PlatformService) RevokeSession(c request.CTX, session *model.Session)
func (ps *PlatformService) RevokeAccessToken(c request.CTX, token string) error {
session, _ := ps.GetSession(c, token)
defer ps.ReturnSessionToPool(session)
schan := make(chan error, 1)
go func() {
schan <- ps.Store.Session().Remove(token)

Просмотреть файл

@@ -194,6 +194,15 @@ func (ps *PlatformService) PopulateWebConnConfig(s *model.Session, cfg *WebConnC
// NewWebConn returns a new WebConn instance.
func (ps *PlatformService) NewWebConn(cfg *WebConnConfig, suite SuiteIFace, runner HookRunner) *WebConn {
userID := cfg.Session.UserId
session := cfg.Session
if cfg.Session.UserId != "" {
ps.Go(func() {
ps.SetStatusOnline(userID, false)
ps.UpdateLastActivityAtIfNeeded(session)
})
}
// Disable TCP_NO_DELAY for higher throughput
var tcpConn *net.TCPConn
switch conn := cfg.WebSocket.UnderlyingConn().(type) {
@@ -245,23 +254,9 @@ func (ps *PlatformService) NewWebConn(cfg *WebConnConfig, suite SuiteIFace, runn
remoteAddress: cfg.RemoteAddress,
xForwardedFor: cfg.XForwardedFor,
}
wc.SetSession(&cfg.Session)
userID := cfg.Session.UserId
if userID != "" {
// UpdateLastActivityAtIfNeeded might block if the Hub is busy.
// Create a goroutine to avoid blocking the creation of the websocket connection.
ps.Go(func() {
ps.SetStatusOnline(userID, false)
session := wc.GetSession()
if session != nil {
ps.UpdateLastActivityAtIfNeeded(*session)
}
})
}
wc.Active.Store(cfg.Active)
wc.SetSession(&cfg.Session)
wc.SetSessionToken(cfg.Session.Token)
wc.SetSessionExpiresAt(cfg.Session.ExpiresAt)
wc.SetConnectionID(cfg.ConnectionID)
@@ -393,8 +388,6 @@ func (wc *WebConn) GetSession() *model.Session {
// SetSession sets the session of the connection.
func (wc *WebConn) SetSession(v *model.Session) {
// Clone the session first as WebConn takes ownership of the object
// and the web.Hub will return it to the [sync.Pool] once the WebConn gets removed.
if v != nil {
v = v.DeepCopy()
}

Просмотреть файл

@@ -649,8 +649,6 @@ func (i *hubConnectionIndex) Add(wc *WebConn) {
}
func (i *hubConnectionIndex) Remove(wc *WebConn) {
wc.Platform.ReturnSessionToPool(wc.GetSession())
userConnIndex, ok := i.byConnection[wc]
if !ok {
return