3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-11-22 20:09:41 +01:00

Merge pull request #1156 from slingamn/lastseen

tweaks to lastSeen persistence
This commit is contained in:
Shivaram Lingamneni 2020-06-29 18:04:55 -07:00 committed by GitHub
commit a7107bfb67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 4 deletions

View File

@ -1245,6 +1245,7 @@ func (am *AccountManager) Unregister(account string, erase bool) error {
joinedChannelsKey := fmt.Sprintf(keyAccountJoinedChannels, casefoldedAccount) joinedChannelsKey := fmt.Sprintf(keyAccountJoinedChannels, casefoldedAccount)
lastSeenKey := fmt.Sprintf(keyAccountLastSeen, casefoldedAccount) lastSeenKey := fmt.Sprintf(keyAccountLastSeen, casefoldedAccount)
unregisteredKey := fmt.Sprintf(keyAccountUnregistered, casefoldedAccount) unregisteredKey := fmt.Sprintf(keyAccountUnregistered, casefoldedAccount)
modesKey := fmt.Sprintf(keyAccountModes, casefoldedAccount)
var clients []*Client var clients []*Client
@ -1299,6 +1300,7 @@ func (am *AccountManager) Unregister(account string, erase bool) error {
tx.Delete(channelsKey) tx.Delete(channelsKey)
tx.Delete(joinedChannelsKey) tx.Delete(joinedChannelsKey)
tx.Delete(lastSeenKey) tx.Delete(lastSeenKey)
tx.Delete(modesKey)
_, err := tx.Delete(vhostQueueKey) _, err := tx.Delete(vhostQueueKey)
am.decrementVHostQueueCount(casefoldedAccount, err) am.decrementVHostQueueCount(casefoldedAccount, err)

View File

@ -34,7 +34,7 @@ const (
maxDeviceIDsPerClient = 64 maxDeviceIDsPerClient = 64
// controls how often often we write an autoreplay-missed client's // controls how often often we write an autoreplay-missed client's
// deviceid->lastseentime mapping to the database // deviceid->lastseentime mapping to the database
lastSeenWriteInterval = time.Minute * 10 lastSeenWriteInterval = time.Hour
) )
// ResumeDetails is a place to stash data at various stages of // ResumeDetails is a place to stash data at various stages of
@ -1724,7 +1724,7 @@ func (client *Client) wakeWriter() {
func (client *Client) writeLoop() { func (client *Client) writeLoop() {
for { for {
client.performWrite() client.performWrite(0)
client.writerSemaphore.Release() client.writerSemaphore.Release()
client.stateMutex.RLock() client.stateMutex.RLock()
@ -1737,9 +1737,9 @@ func (client *Client) writeLoop() {
} }
} }
func (client *Client) performWrite() { func (client *Client) performWrite(additionalDirtyBits uint) {
client.stateMutex.Lock() client.stateMutex.Lock()
dirtyBits := client.dirtyBits dirtyBits := client.dirtyBits | additionalDirtyBits
client.dirtyBits = 0 client.dirtyBits = 0
account := client.account account := client.account
client.stateMutex.Unlock() client.stateMutex.Unlock()
@ -1775,3 +1775,21 @@ func (client *Client) performWrite() {
client.server.accounts.saveModes(account, uModes) client.server.accounts.saveModes(account, uModes)
} }
} }
// Blocking store; see Channel.Store and Socket.BlockingWrite
func (client *Client) Store(dirtyBits uint) (err error) {
defer func() {
client.stateMutex.Lock()
isDirty := client.dirtyBits != 0
client.stateMutex.Unlock()
if isDirty {
client.wakeWriter()
}
}()
client.writerSemaphore.Acquire()
defer client.writerSemaphore.Release()
client.performWrite(dirtyBits)
return nil
}

View File

@ -106,6 +106,7 @@ func (client *Client) AddSession(session *Session) (success bool, numSessions in
newSessions[len(newSessions)-1] = session newSessions[len(newSessions)-1] = session
if client.accountSettings.AutoreplayMissed || session.deviceID != "" { if client.accountSettings.AutoreplayMissed || session.deviceID != "" {
lastSeen = client.lastSeen[session.deviceID] lastSeen = client.lastSeen[session.deviceID]
client.setLastSeen(time.Now().UTC(), session.deviceID)
} }
client.sessions = newSessions client.sessions = newSessions
if client.autoAway { if client.autoAway {

View File

@ -116,6 +116,9 @@ func (server *Server) Shutdown() {
//TODO(dan): Make sure we disallow new nicks //TODO(dan): Make sure we disallow new nicks
for _, client := range server.clients.AllClients() { for _, client := range server.clients.AllClients() {
client.Notice("Server is shutting down") client.Notice("Server is shutting down")
if client.AlwaysOn() {
client.Store(IncludeLastSeen)
}
} }
if err := server.store.Close(); err != nil { if err := server.store.Close(); err != nil {