Fix rekey loop when new community is added

This commit is contained in:
Samuel Hawksby-Robinson 2023-05-18 11:01:18 +01:00
parent a06984aeec
commit 5c36684a3c
1 changed files with 35 additions and 26 deletions

View File

@ -4243,34 +4243,43 @@ func (m *Messenger) startCommunityRekeyLoop() error {
} }
for _, c := range cs { for _, c := range cs {
if c.IsAdmin() && c.Encrypted() { err = m.rekeyCommunity(c, rki, ticker, logger)
// if rekey time period exceeded, perform a rekey if err != nil {
if c.RekeyedAt().Add(rki).Before(time.Now()) { return err
err = m.RekeyCommunity(c.ID())
if err != nil {
return err
}
}
go func() {
logger.Debug("CommunityRekeyLoop started", zap.Binary("community ID", c.ID()))
for {
select {
case <-ticker.C:
err = m.RekeyCommunity(c.ID())
if err != nil {
logger.Error("error sending rekey message", zap.Error(err), zap.Binary("community ID", c.ID()))
}
case <-m.quit:
ticker.Stop()
logger.Debug("CommunityRekeyLoop stopped", zap.Binary("community ID", c.ID()))
return
}
}
}()
} }
} }
return nil return nil
} }
func (m *Messenger) rekeyCommunity(c *communities.Community, rki time.Duration, ticker *time.Ticker, logger *zap.Logger) error {
if c.IsAdmin() && c.Encrypted() {
// if rekey time period exceeded, perform a rekey
if c.RekeyedAt().Add(rki).Before(time.Now()) {
err := m.RekeyCommunity(c.ID())
if err != nil {
return err
}
}
go func() {
logger.Debug("CommunityRekeyLoop started", zap.Binary("community ID", c.ID()))
for {
select {
case <-ticker.C:
err := m.RekeyCommunity(c.ID())
if err != nil {
logger.Error("error sending rekey message", zap.Error(err), zap.Binary("community ID", c.ID()))
}
case <-m.quit:
ticker.Stop()
logger.Debug("CommunityRekeyLoop stopped", zap.Binary("community ID", c.ID()))
return
}
}
}()
}
return nil
}