Skip to content

Commit

Permalink
Improve sending messages while reconnecting (#63)
Browse files Browse the repository at this point in the history
* Add sync.Cond and CanSendMessages flag to `messagix.Client`

* Enable sending messages on Event_Ready, and wait for it on handleMatrixMessages

* Add `EnableSendingMessages` and `WaitUntilCanSendMessages` to `Client`

* Add sending retry to Meta messages

* Improve error handling

* Improve error handling

* Add `disableSendingMessages` and improve `WaitUntilCanSendMessages`

* Move `disableSendingMessages` call
  • Loading branch information
javiercr authored May 22, 2024
1 parent ff202e8 commit 35f2b31
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 9 deletions.
58 changes: 51 additions & 7 deletions messagix/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ type Client struct {
unnecessaryCATRequests int

stopCurrentConnection atomic.Pointer[context.CancelFunc]

canSendMessages bool
sendMessagesCond *sync.Cond
}

func NewClient(cookies *cookies.Cookies, logger zerolog.Logger) *Client {
Expand All @@ -88,13 +91,15 @@ func NewClient(cookies *cookies.Cookies, logger zerolog.Logger) *Client {
},
Timeout: 60 * time.Second,
},
cookies: cookies,
Logger: logger,
lsRequests: 0,
graphQLRequests: 1,
platform: cookies.Platform,
activeTasks: make([]int, 0),
taskMutex: &sync.Mutex{},
cookies: cookies,
Logger: logger,
lsRequests: 0,
graphQLRequests: 1,
platform: cookies.Platform,
activeTasks: make([]int, 0),
taskMutex: &sync.Mutex{},
canSendMessages: false,
sendMessagesCond: sync.NewCond(&sync.Mutex{}),
}
cli.http.CheckRedirect = cli.checkHTTPRedirect

Expand Down Expand Up @@ -222,6 +227,7 @@ func (c *Client) Connect() error {
for {
connectStart := time.Now()
err := c.socket.Connect()
c.disableSendingMessages()
if ctx.Err() != nil {
return
}
Expand Down Expand Up @@ -388,3 +394,41 @@ func (c *Client) GetTaskId() int {
c.activeTasks = append(c.activeTasks, id)
return id
}

func (c *Client) EnableSendingMessages() {
c.sendMessagesCond.L.Lock()
c.canSendMessages = true
c.sendMessagesCond.Broadcast()
c.sendMessagesCond.L.Unlock()
}

func (c *Client) disableSendingMessages() {
c.sendMessagesCond.L.Lock()
c.canSendMessages = false
c.sendMessagesCond.L.Unlock()
}

func (c *Client) WaitUntilCanSendMessages(timeout time.Duration) error {
timer := time.NewTimer(timeout)
defer timer.Stop()

done := make(chan struct{})
go func() {
c.sendMessagesCond.L.Lock()
defer c.sendMessagesCond.L.Unlock()
c.sendMessagesCond.Wait()
close(done)
}()

for !c.canSendMessages {
select {
case <-timer.C:
return fmt.Errorf("timeout waiting for canSendMessages")
case <-done:
if c.canSendMessages {
return nil
}
}
}
return nil
}
3 changes: 3 additions & 0 deletions messagix/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ func (s *Socket) handleReadyEvent(data *Event_Ready) error {
data.client = s.client
s.client.eventHandler(data.Finish())
s.previouslyConnected = true

s.client.EnableSendingMessages()

return nil
}

Expand Down
20 changes: 18 additions & 2 deletions portal.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import (
"go.mau.fi/mautrix-meta/msgconv"
)

const MaxMetaSendAttempts = 5

func (br *MetaBridge) GetPortalByMXID(mxid id.RoomID) *Portal {
br.portalsLock.Lock()
defer br.portalsLock.Unlock()
Expand Down Expand Up @@ -651,10 +653,24 @@ func (portal *Portal) handleMatrixMessage(ctx context.Context, sender *User, evt
portal.pendingMessages[otid] = evt.ID
messageTS := time.Now()
var resp *table.LSTable
resp, err = sender.Client.ExecuteTasks(tasks...)

retries := 0
for retries < MaxMetaSendAttempts {
if err = sender.Client.WaitUntilCanSendMessages(15 * time.Second); err != nil {
log.Err(err).Msg("Error waiting to be able to send messages, retrying")
} else {
resp, err = sender.Client.ExecuteTasks(tasks...)
if err == nil {
break
}
log.Err(err).Msg("Failed to send message to Meta, retrying")
}
retries++
}

log.Trace().Any("response", resp).Msg("Meta send response")
var msgID string
if err == nil {
if resp != nil && err == nil {
for _, replace := range resp.LSReplaceOptimsiticMessage {
if replace.OfflineThreadingId == otidStr {
msgID = replace.MessageId
Expand Down

0 comments on commit 35f2b31

Please sign in to comment.