session: add ListSessionsByType method

And use it to replace one call to ListSessions which uses a filter
function which would be inefficient in SQL land.
This commit is contained in:
Elle Mouton 2025-02-09 12:22:53 +02:00
parent 00230029f3
commit 6c36b01fd8
No known key found for this signature in database
GPG key ID: D7D916376026F177
4 changed files with 37 additions and 4 deletions

View file

@ -164,6 +164,9 @@ type Store interface {
// ListSessions returns all sessions currently known to the store.
ListSessions(filterFn func(s *Session) bool) ([]*Session, error)
// ListSessionsByType returns all sessions of the given type.
ListSessionsByType(t Type) ([]*Session, error)
// RevokeSession updates the state of the session with the given local
// public key to be revoked.
RevokeSession(*btcec.PublicKey) error

View file

@ -371,6 +371,16 @@ func (db *BoltStore) ListSessions(filterFn func(s *Session) bool) ([]*Session, e
return db.listSessions(filterFn)
}
// ListSessionsByType returns all sessions currently known to the store that
// have the given type.
//
// NOTE: this is part of the Store interface.
func (db *BoltStore) ListSessionsByType(t Type) ([]*Session, error) {
return db.listSessions(func(s *Session) bool {
return s.Type == t
})
}
// listSessions returns all sessions currently known to the store that pass the
// given filter function.
func (db *BoltStore) listSessions(filterFn func(s *Session) bool) ([]*Session,

View file

@ -31,7 +31,7 @@ func TestBasicSessionStore(t *testing.T) {
clock.SetTime(testTime.Add(time.Second))
s2 := newSession(t, db, clock, "session 2")
clock.SetTime(testTime.Add(2 * time.Second))
s3 := newSession(t, db, clock, "session 3")
s3 := newSession(t, db, clock, "session 3", withType(TypeAutopilot))
clock.SetTime(testTime.Add(3 * time.Second))
s4 := newSession(t, db, clock, "session 4")
@ -64,6 +64,22 @@ func TestBasicSessionStore(t *testing.T) {
assertEqualSessions(t, s2, sessions[1])
assertEqualSessions(t, s3, sessions[2])
// Test the ListSessionsByType method.
sessions, err = db.ListSessionsByType(TypeMacaroonAdmin)
require.NoError(t, err)
require.Equal(t, 2, len(sessions))
assertEqualSessions(t, s1, sessions[0])
assertEqualSessions(t, s2, sessions[1])
sessions, err = db.ListSessionsByType(TypeAutopilot)
require.NoError(t, err)
require.Equal(t, 1, len(sessions))
assertEqualSessions(t, s3, sessions[0])
sessions, err = db.ListSessionsByType(TypeMacaroonReadonly)
require.NoError(t, err)
require.Empty(t, sessions)
// Ensure that we can retrieve each session by both its local pub key
// and by its ID.
for _, s := range []*Session{s1, s2, s3} {
@ -310,6 +326,12 @@ func withLinkedGroupID(groupID *ID) testSessionModifier {
}
}
func withType(t Type) testSessionModifier {
return func(s *Session) {
s.Type = t
}
}
func newSession(t *testing.T, db Store, clock clock.Clock, label string,
mods ...testSessionModifier) *Session {

View file

@ -1259,9 +1259,7 @@ func (s *sessionRpcServer) ListAutopilotSessions(_ context.Context,
_ *litrpc.ListAutopilotSessionsRequest) (
*litrpc.ListAutopilotSessionsResponse, error) {
sessions, err := s.cfg.db.ListSessions(func(s *session.Session) bool {
return s.Type == session.TypeAutopilot
})
sessions, err := s.cfg.db.ListSessionsByType(session.TypeAutopilot)
if err != nil {
return nil, fmt.Errorf("error fetching sessions: %v", err)
}