wtclient: don't unnecessarily load exhausted sessions

In this commit, a new `ExhaustedSessionFilter` function is added and
used as a PostEvalFilterFn used when loading sessions from the DB. It
allows us to not unnecessarily load exhausted sessions into memory for
areas of the code where they will not be needed.
This commit is contained in:
Elle Mouton 2023-03-20 17:44:24 +02:00
parent 49cd23725a
commit 8623e6107d
No known key found for this signature in database
GPG key ID: D7D916376026F177

View file

@ -74,6 +74,14 @@ func (c *TowerClient) genSessionFilter(
}
}
// ExhaustedSessionFilter constructs a wtdb.ClientSessionFilterFn filter
// function that will filter out any sessions that have been exhausted.
func ExhaustedSessionFilter() wtdb.ClientSessionFilterFn {
return func(session *wtdb.ClientSession) bool {
return session.SeqNum < session.Policy.MaxUpdates
}
}
// RegisteredTower encompasses information about a registered watchtower with
// the client.
type RegisteredTower struct {
@ -414,6 +422,7 @@ func New(config *Config) (*TowerClient, error) {
wtdb.WithPreEvalFilterFn(c.genSessionFilter(true)),
wtdb.WithPerMaxHeight(perMaxHeight),
wtdb.WithPerCommittedUpdate(perCommittedUpdate),
wtdb.WithPostEvalFilterFn(ExhaustedSessionFilter()),
)
if err != nil {
return nil, err
@ -1627,6 +1636,7 @@ func (c *TowerClient) handleNewTower(msg *newTowerMsg) error {
sessions, err := getClientSessions(
c.cfg.DB, c.cfg.SecretKeyRing, &tower.ID,
wtdb.WithPreEvalFilterFn(c.genSessionFilter(true)),
wtdb.WithPostEvalFilterFn(ExhaustedSessionFilter()),
)
if err != nil {
return fmt.Errorf("unable to determine sessions for tower %x: "+