From 0f04acaa40f6f0362e36495135e3326f38d07b6c Mon Sep 17 00:00:00 2001 From: Shivaram Lingamneni Date: Mon, 29 Jun 2020 00:30:27 -0400 Subject: [PATCH] persist last seen on shutdown --- irc/client.go | 24 +++++++++++++++++++++--- irc/server.go | 3 +++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/irc/client.go b/irc/client.go index b9ba587d..bc36a41c 100644 --- a/irc/client.go +++ b/irc/client.go @@ -1724,7 +1724,7 @@ func (client *Client) wakeWriter() { func (client *Client) writeLoop() { for { - client.performWrite() + client.performWrite(0) client.writerSemaphore.Release() client.stateMutex.RLock() @@ -1737,9 +1737,9 @@ func (client *Client) writeLoop() { } } -func (client *Client) performWrite() { +func (client *Client) performWrite(additionalDirtyBits uint) { client.stateMutex.Lock() - dirtyBits := client.dirtyBits + dirtyBits := client.dirtyBits | additionalDirtyBits client.dirtyBits = 0 account := client.account client.stateMutex.Unlock() @@ -1775,3 +1775,21 @@ func (client *Client) performWrite() { 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 +} diff --git a/irc/server.go b/irc/server.go index 98725553..8a5fbfb8 100644 --- a/irc/server.go +++ b/irc/server.go @@ -116,6 +116,9 @@ func (server *Server) Shutdown() { //TODO(dan): Make sure we disallow new nicks for _, client := range server.clients.AllClients() { client.Notice("Server is shutting down") + if client.AlwaysOn() { + client.Store(IncludeLastSeen) + } } if err := server.store.Close(); err != nil {