watchtower: refactor putClientSessionBody

Refactor the putClientSessionBody to take in a session sub-bucket rather
than the top-level session bucket. This is mainly to make an upcoming
commit diff easier to parse.
This commit is contained in:
Elle Mouton 2022-10-18 11:14:16 +02:00
parent 25afc8ad90
commit 4ea6c7d2ae
No known key found for this signature in database
GPG key ID: D7D916376026F177

View file

@ -711,9 +711,14 @@ func (c *ClientDB) CreateClientSession(session *ClientSession) error {
return err
}
sessionBkt, err := sessions.CreateBucket(session.ID[:])
if err != nil {
return err
}
// Finally, write the client session's body in the sessions
// bucket.
return putClientSessionBody(sessions, session)
return putClientSessionBody(sessionBkt, session)
}, func() {})
}
@ -1082,7 +1087,7 @@ func (c *ClientDB) CommitUpdate(id *SessionID,
// eliminate serialization of full struct during CommitUpdate?
// Can also read/write directly to byes [:2] without migration.
session.SeqNum++
err = putClientSessionBody(sessions, session)
err = putClientSessionBody(sessionBkt, session)
if err != nil {
return err
}
@ -1154,15 +1159,15 @@ func (c *ClientDB) AckUpdate(id *SessionID, seqNum uint16,
// also read/write directly to byes [2:4] without migration.
session.TowerLastApplied = lastApplied
// Can't fail because getClientSession succeeded.
sessionBkt := sessions.NestedReadWriteBucket(id[:])
// Write the client session with the updated last applied value.
err = putClientSessionBody(sessions, session)
err = putClientSessionBody(sessionBkt, session)
if err != nil {
return err
}
// Can't fail because of getClientSession succeeded.
sessionBkt := sessions.NestedReadWriteBucket(id[:])
// If the commits sub-bucket doesn't exist, there can't possibly
// be a corresponding committed update to remove.
sessionCommits := sessionBkt.NestedReadWriteBucket(
@ -1439,16 +1444,11 @@ func filterClientSessionCommits(sessionBkt kvdb.RBucket, s *ClientSession,
// putClientSessionBody stores the body of the ClientSession (everything but the
// CommittedUpdates and AckedUpdates).
func putClientSessionBody(sessions kvdb.RwBucket,
func putClientSessionBody(sessionBkt kvdb.RwBucket,
session *ClientSession) error {
sessionBkt, err := sessions.CreateBucketIfNotExists(session.ID[:])
if err != nil {
return err
}
var b bytes.Buffer
err = session.Encode(&b)
err := session.Encode(&b)
if err != nil {
return err
}
@ -1461,8 +1461,14 @@ func putClientSessionBody(sessions kvdb.RwBucket,
func markSessionStatus(sessions kvdb.RwBucket, session *ClientSession,
status CSessionStatus) error {
sessionBkt, err := sessions.CreateBucketIfNotExists(session.ID[:])
if err != nil {
return err
}
session.Status = status
return putClientSessionBody(sessions, session)
return putClientSessionBody(sessionBkt, session)
}
// getChanSummary loads a ClientChanSummary for the passed chanID.