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

+10 -3
View File
@@ -83,7 +83,7 @@ func (m *ClientDB) CreateTower(lnAddr *lnwire.NetAddress) (*wtdb.Tower, error) {
tower = m.towers[towerID]
tower.AddAddress(lnAddr.Address)
towerSessions, err := m.listClientSessions(&towerID)
towerSessions, err := m.listClientSessions(&towerID, nil)
if err != nil {
return nil, err
}
@@ -135,7 +135,7 @@ func (m *ClientDB) RemoveTower(pubKey *btcec.PublicKey, addr net.Addr) error {
return nil
}
towerSessions, err := m.listClientSessions(&tower.ID)
towerSessions, err := m.listClientSessions(&tower.ID, nil)
if err != nil {
return err
}
@@ -220,18 +220,20 @@ func (m *ClientDB) MarkBackupIneligible(_ lnwire.ChannelID, _ uint64) error {
// optional tower ID can be used to filter out any client sessions in the
// response that do not correspond to this tower.
func (m *ClientDB) ListClientSessions(tower *wtdb.TowerID,
filterFn wtdb.ClientSessionFilterFn,
opts ...wtdb.ClientSessionListOption) (
map[wtdb.SessionID]*wtdb.ClientSession, error) {
m.mu.Lock()
defer m.mu.Unlock()
return m.listClientSessions(tower, opts...)
return m.listClientSessions(tower, filterFn, opts...)
}
// listClientSessions returns the set of all client sessions known to the db. An
// optional tower ID can be used to filter out any client sessions in the
// response that do not correspond to this tower.
func (m *ClientDB) listClientSessions(tower *wtdb.TowerID,
filterFn wtdb.ClientSessionFilterFn,
opts ...wtdb.ClientSessionListOption) (
map[wtdb.SessionID]*wtdb.ClientSession, error) {
@@ -246,6 +248,11 @@ func (m *ClientDB) listClientSessions(tower *wtdb.TowerID,
if tower != nil && *tower != session.TowerID {
continue
}
if filterFn != nil && !filterFn(&session) {
continue
}
sessions[session.ID] = &session
if cfg.PerMaxHeight != nil {