watchtower: add ClientSessionFilterFn to session requests

In this commit, a new ClientSessionFilterFn parameter is added to the
DB's ListClientSession method which can be used to allow the caller to
specify a filter function for filtering sessions read from the DB.
Currently all filtering of sessions are done after the sessions have
been read from the DB, so adding this option should provide some
efficiency.
This commit is contained in:
Elle Mouton committed 2023-03-16 14:59:08 +02:00
1 parent 8a2999c789
commit 40ac82e439
5 files changed
+62 -28

No files matched your search

+7 -6
View File
@@ -49,11 +49,12 @@ func (h *clientDBHarness) insertSession(session *wtdb.ClientSession,
}
func (h *clientDBHarness) listSessions(id *wtdb.TowerID,
filterFn wtdb.ClientSessionFilterFn,
opts ...wtdb.ClientSessionListOption) map[wtdb.SessionID]*wtdb.ClientSession {
h.t.Helper()
sessions, err := h.db.ListClientSessions(id, opts...)
sessions, err := h.db.ListClientSessions(id, filterFn, opts...)
require.NoError(h.t, err, "unable to list client sessions")
return sessions
@@ -80,7 +81,7 @@ func (h *clientDBHarness) createTower(lnAddr *lnwire.NetAddress,
require.ErrorIs(h.t, err, expErr)
require.NotZero(h.t, tower.ID, "tower id should never be 0")
for _, session := range h.listSessions(&tower.ID) {
for _, session := range h.listSessions(&tower.ID, nil) {
require.Equal(h.t, wtdb.CSessionActive, session.Status)
}
@@ -123,7 +124,7 @@ func (h *clientDBHarness) removeTower(pubKey *btcec.PublicKey, addr net.Addr,
return
}
for _, session := range h.listSessions(&tower.ID) {
for _, session := range h.listSessions(&tower.ID, nil) {
require.Equal(h.t, wtdb.CSessionInactive,
session.Status, "expected status for session "+
"%v to be %v, got %v", session.ID,
@@ -268,7 +269,7 @@ func testCreateClientSession(h *clientDBHarness) {
// First, assert that this session is not already present in the
// database.
_, ok := h.listSessions(nil)[session.ID]
_, ok := h.listSessions(nil, nil)[session.ID]
require.Falsef(h.t, ok, "session for id %x should not exist yet",
session.ID)
@@ -296,7 +297,7 @@ func testCreateClientSession(h *clientDBHarness) {
h.insertSession(session, nil)
// Verify that the session now exists in the database.
_, ok = h.listSessions(nil)[session.ID]
_, ok = h.listSessions(nil, nil)[session.ID]
require.Truef(h.t, ok, "session for id %x should exist now", session.ID)
// Attempt to insert the session again, which should fail due to the
@@ -344,7 +345,7 @@ func testFilterClientSessions(h *clientDBHarness) {
// We should see the expected sessions for each tower when filtering
// them.
for towerID, expectedSessions := range towerSessions {
sessions := h.listSessions(&towerID)
sessions := h.listSessions(&towerID, nil)
require.Len(h.t, sessions, len(expectedSessions))
for _, expectedSession := range expectedSessions {