[#1133] services/session: remove expired tokens every epoch

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2022-02-03 14:43:05 +03:00 committed by Alex Vanin
parent b3b3b8b20f
commit 0695ec4125
2 changed files with 17 additions and 0 deletions

View file

@ -2,6 +2,8 @@ package main
import ( import (
sessionGRPC "github.com/nspcc-dev/neofs-api-go/v2/session/grpc" sessionGRPC "github.com/nspcc-dev/neofs-api-go/v2/session/grpc"
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
"github.com/nspcc-dev/neofs-node/pkg/morph/event/netmap"
sessionTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/session/grpc" sessionTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/session/grpc"
sessionSvc "github.com/nspcc-dev/neofs-node/pkg/services/session" sessionSvc "github.com/nspcc-dev/neofs-node/pkg/services/session"
"github.com/nspcc-dev/neofs-node/pkg/services/session/storage" "github.com/nspcc-dev/neofs-node/pkg/services/session/storage"
@ -9,6 +11,9 @@ import (
func initSessionService(c *cfg) { func initSessionService(c *cfg) {
c.privateTokenStore = storage.New() c.privateTokenStore = storage.New()
addNewEpochNotificationHandler(c, func(ev event.Event) {
c.privateTokenStore.RemoveOld(ev.(netmap.NewEpoch).EpochNumber())
})
server := sessionTransportGRPC.New( server := sessionTransportGRPC.New(
sessionSvc.NewSignService( sessionSvc.NewSignService(

View file

@ -49,3 +49,15 @@ func (s *TokenStore) Get(ownerID *owner.ID, tokenID []byte) *PrivateToken {
return t return t
} }
// RemoveOld removes all tokens expired since provided epoch.
func (s *TokenStore) RemoveOld(epoch uint64) {
s.mtx.Lock()
defer s.mtx.Unlock()
for k, tok := range s.tokens {
if tok.ExpiredAt() <= epoch {
delete(s.tokens, k)
}
}
}