watchtower: extend client db to filter sessions for a specific tower
This currently takes O(N) time as there does not exist an index of active client sessions for each watchtower within the client's database. This index is likely to be added in the future.
This commit is contained in:
5 files changed
+111
-29
No files matched your search
@@ -384,29 +384,53 @@ func (c *ClientDB) CreateClientSession(session *ClientSession) error {
|
||||
})
|
||||
}
|
||||
|
||||
// ListClientSessions returns the set of all client sessions known to the db.
|
||||
func (c *ClientDB) ListClientSessions() (map[SessionID]*ClientSession, error) {
|
||||
clientSessions := make(map[SessionID]*ClientSession)
|
||||
// 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 (c *ClientDB) ListClientSessions(id *TowerID) (map[SessionID]*ClientSession, error) {
|
||||
var clientSessions map[SessionID]*ClientSession
|
||||
err := c.db.View(func(tx *bbolt.Tx) error {
|
||||
sessions := tx.Bucket(cSessionBkt)
|
||||
if sessions == nil {
|
||||
return ErrUninitializedDB
|
||||
}
|
||||
var err error
|
||||
clientSessions, err = listClientSessions(sessions, id)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return sessions.ForEach(func(k, _ []byte) error {
|
||||
// We'll load the full client session since the client
|
||||
// will need the CommittedUpdates and AckedUpdates on
|
||||
// startup to resume committed updates and compute the
|
||||
// highest known commit height for each channel.
|
||||
session, err := getClientSession(sessions, k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return clientSessions, nil
|
||||
}
|
||||
|
||||
clientSessions[session.ID] = session
|
||||
// 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 listClientSessions(sessions *bbolt.Bucket,
|
||||
id *TowerID) (map[SessionID]*ClientSession, error) {
|
||||
|
||||
clientSessions := make(map[SessionID]*ClientSession)
|
||||
err := sessions.ForEach(func(k, _ []byte) error {
|
||||
// We'll load the full client session since the client will need
|
||||
// the CommittedUpdates and AckedUpdates on startup to resume
|
||||
// committed updates and compute the highest known commit height
|
||||
// for each channel.
|
||||
session, err := getClientSession(sessions, k)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Filter out any sessions that don't correspond to the given
|
||||
// tower if one was set.
|
||||
if id != nil && session.TowerID != *id {
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
clientSessions[session.ID] = session
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
Reference in new issue
Block a user