summaryrefslogtreecommitdiffstats
path: root/bridge
diff options
context:
space:
mode:
authorWim <wim@42.be>2020-08-24 00:12:30 +0200
committerGitHub <noreply@github.com>2020-08-24 00:12:30 +0200
commit56159b9bce7de33359ef776fa1018ced5f68d296 (patch)
tree5e7952e3eb19aca573e2174ddad958fe288e409b /bridge
parentb2af76e7dc90317369c767a686d87937aff54a20 (diff)
downloadmatterbridge-msglm-56159b9bce7de33359ef776fa1018ced5f68d296.tar.gz
matterbridge-msglm-56159b9bce7de33359ef776fa1018ced5f68d296.tar.bz2
matterbridge-msglm-56159b9bce7de33359ef776fa1018ced5f68d296.zip
Sleep when ratelimited on joins (matrix). Fixes #1201 (#1206)
Diffstat (limited to 'bridge')
-rw-r--r--bridge/matrix/matrix.go40
1 files changed, 39 insertions, 1 deletions
diff --git a/bridge/matrix/matrix.go b/bridge/matrix/matrix.go
index 615dcfb7..c2305bd5 100644
--- a/bridge/matrix/matrix.go
+++ b/bridge/matrix/matrix.go
@@ -2,12 +2,14 @@ package bmatrix
import (
"bytes"
+ "encoding/json"
"fmt"
"html"
"mime"
"regexp"
"strings"
"sync"
+ "time"
"github.com/42wim/matterbridge/bridge"
"github.com/42wim/matterbridge/bridge/config"
@@ -25,6 +27,12 @@ type Bmatrix struct {
*bridge.Config
}
+type httpError struct {
+ Errcode string `json:"errcode"`
+ Err string `json:"error"`
+ RetryAfterMs int `json:"retry_after_ms"`
+}
+
func New(cfg *bridge.Config) bridge.Bridger {
b := &Bmatrix{Config: cfg}
b.htmlTag = regexp.MustCompile("</.*?>")
@@ -60,14 +68,25 @@ func (b *Bmatrix) Disconnect() error {
}
func (b *Bmatrix) JoinChannel(channel config.ChannelInfo) error {
+retry:
resp, err := b.mc.JoinRoom(channel.Name, "", nil)
if err != nil {
+ httpErr := handleError(err)
+ if httpErr.Errcode == "M_LIMIT_EXCEEDED" {
+ b.Log.Infof("getting ratelimited by matrix, sleeping approx %d seconds before joining %s", httpErr.RetryAfterMs/1000, channel.Name)
+ time.Sleep((time.Duration(httpErr.RetryAfterMs) * time.Millisecond))
+
+ goto retry
+ }
+
return err
}
+
b.Lock()
b.RoomMap[resp.RoomID] = channel.Name
b.Unlock()
- return err
+
+ return nil
}
func (b *Bmatrix) Send(msg config.Message) (string, error) {
@@ -394,3 +413,22 @@ func (b *Bmatrix) getAvatarURL(sender string) string {
}
return url
}
+
+func handleError(err error) *httpError {
+ mErr, ok := err.(matrix.HTTPError)
+ if !ok {
+ return &httpError{
+ Err: "not a HTTPError",
+ }
+ }
+
+ var httpErr httpError
+
+ if err := json.Unmarshal(mErr.Contents, &httpErr); err != nil {
+ return &httpError{
+ Err: "unmarshal failed",
+ }
+ }
+
+ return &httpErr
+}