mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
bridgev2,appservice: move appservice ping loop to appservice package
This commit is contained in:
parent
4f6d4d7c63
commit
6f370cc3bb
2 changed files with 70 additions and 45 deletions
68
appservice/ping.go
Normal file
68
appservice/ping.go
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
// Copyright (c) 2025 Tulir Asokan
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
package appservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/rs/zerolog"
|
||||
|
||||
"maunium.net/go/mautrix"
|
||||
)
|
||||
|
||||
func (intent *IntentAPI) EnsureAppserviceConnection(ctx context.Context, appserviceID string) {
|
||||
var pingResp *mautrix.RespAppservicePing
|
||||
var txnID string
|
||||
var retryCount int
|
||||
var err error
|
||||
const maxRetries = 6
|
||||
for {
|
||||
txnID = intent.TxnID()
|
||||
pingResp, err = intent.AppservicePing(ctx, appserviceID, txnID)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
var httpErr mautrix.HTTPError
|
||||
var pingErrBody string
|
||||
if errors.As(err, &httpErr) && httpErr.RespError != nil {
|
||||
if val, ok := httpErr.RespError.ExtraData["body"].(string); ok {
|
||||
pingErrBody = strings.TrimSpace(val)
|
||||
}
|
||||
}
|
||||
outOfRetries := retryCount >= maxRetries
|
||||
level := zerolog.ErrorLevel
|
||||
if outOfRetries {
|
||||
level = zerolog.FatalLevel
|
||||
}
|
||||
evt := zerolog.Ctx(ctx).WithLevel(level).Err(err).Str("txn_id", txnID)
|
||||
if pingErrBody != "" {
|
||||
bodyBytes := []byte(pingErrBody)
|
||||
if json.Valid(bodyBytes) {
|
||||
evt.RawJSON("body", bodyBytes)
|
||||
} else {
|
||||
evt.Str("body", pingErrBody)
|
||||
}
|
||||
}
|
||||
if outOfRetries {
|
||||
evt.Msg("Homeserver -> appservice connection is not working")
|
||||
zerolog.Ctx(ctx).Info().Msg("See https://docs.mau.fi/faq/as-ping for more info")
|
||||
os.Exit(13)
|
||||
}
|
||||
evt.Msg("Homeserver -> appservice connection is not working, retrying in 5 seconds...")
|
||||
time.Sleep(5 * time.Second)
|
||||
retryCount++
|
||||
}
|
||||
zerolog.Ctx(ctx).Debug().
|
||||
Str("txn_id", txnID).
|
||||
Int64("duration_ms", pingResp.DurationMS).
|
||||
Msg("Homeserver -> appservice connection works")
|
||||
}
|
||||
|
|
@ -10,7 +10,6 @@ import (
|
|||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
|
|
@ -343,50 +342,8 @@ func (br *Connector) ensureConnection(ctx context.Context) {
|
|||
br.Log.Debug().Msg("Homeserver does not support checking status of homeserver -> bridge connection")
|
||||
return
|
||||
}
|
||||
var pingResp *mautrix.RespAppservicePing
|
||||
var txnID string
|
||||
var retryCount int
|
||||
const maxRetries = 6
|
||||
for {
|
||||
txnID = br.Bot.TxnID()
|
||||
pingResp, err = br.Bot.AppservicePing(ctx, br.Config.AppService.ID, txnID)
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
var httpErr mautrix.HTTPError
|
||||
var pingErrBody string
|
||||
if errors.As(err, &httpErr) && httpErr.RespError != nil {
|
||||
if val, ok := httpErr.RespError.ExtraData["body"].(string); ok {
|
||||
pingErrBody = strings.TrimSpace(val)
|
||||
}
|
||||
}
|
||||
outOfRetries := retryCount >= maxRetries
|
||||
level := zerolog.ErrorLevel
|
||||
if outOfRetries {
|
||||
level = zerolog.FatalLevel
|
||||
}
|
||||
evt := br.Log.WithLevel(level).Err(err).Str("txn_id", txnID)
|
||||
if pingErrBody != "" {
|
||||
bodyBytes := []byte(pingErrBody)
|
||||
if json.Valid(bodyBytes) {
|
||||
evt.RawJSON("body", bodyBytes)
|
||||
} else {
|
||||
evt.Str("body", pingErrBody)
|
||||
}
|
||||
}
|
||||
if outOfRetries {
|
||||
evt.Msg("Homeserver -> bridge connection is not working")
|
||||
br.Log.Info().Msg("See https://docs.mau.fi/faq/as-ping for more info")
|
||||
os.Exit(13)
|
||||
}
|
||||
evt.Msg("Homeserver -> bridge connection is not working, retrying in 5 seconds...")
|
||||
time.Sleep(5 * time.Second)
|
||||
retryCount++
|
||||
}
|
||||
br.Log.Debug().
|
||||
Str("txn_id", txnID).
|
||||
Int64("duration_ms", pingResp.DurationMS).
|
||||
Msg("Homeserver -> bridge connection works")
|
||||
|
||||
br.Bot.EnsureAppserviceConnection(ctx, br.Config.AppService.ID)
|
||||
}
|
||||
|
||||
func (br *Connector) fetchMediaConfig(ctx context.Context) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue