621e8895c9
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
3.9 KiB
3.9 KiB
name, kind, lang, domain, version, purity, signature, description, tags, params, output, uses_functions, uses_types, returns, returns_optional, error_type, imports, tested, tests, test_file_path, file_path
| name | kind | lang | domain | version | purity | signature | description | tags | params | output | uses_functions | uses_types | returns | returns_optional | error_type | imports | tested | tests | test_file_path | file_path | ||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| matrix_sync_service | function | go | infra | 0.1.0 | impure | func MatrixSyncService(ctx context.Context, cfg MatrixSyncServiceConfig) (*MatrixSyncServiceHandle, error) | Arranca el sync loop de mautrix contra Synapse en background con backoff exponencial, emite eventos Matrix normalizados via canal Go y expone funcion de stop idempotente. |
|
|
*MatrixSyncServiceHandle con Events <-chan MatrixSyncEvent (canal de eventos normalizados), Errors <-chan error (errores transitorios no fatales), Stop func() (cancela y cierra todo, idempotente). | false | error_go_core |
|
true |
|
functions/infra/matrix_sync_service_test.go | functions/infra/matrix_sync_service.go |
Ejemplo
ctx := context.Background()
h, err := MatrixSyncService(ctx, MatrixSyncServiceConfig{
Client: client, // *mautrix.Client de matrix_client_init
})
if err != nil {
panic(err)
}
defer h.Stop()
// Consumir errores transitorios en goroutine separada
go func() {
for e := range h.Errors {
log.Println("matrix sync error:", e)
}
}()
// Loop de eventos (bloquea hasta que h.Stop() se llame o ctx sea cancelado)
for ev := range h.Events {
fmt.Printf("[%s] %s: %s\n", ev.Type, ev.Sender, ev.Body)
}
Cuando usarla
Usar despues de MatrixClientInit (y opcionalmente MatrixCryptoInit) para recibir el stream de eventos de Matrix en tiempo real. Es el servicio long-running central de cualquier cliente Matrix: matrix_client_pc, admin_panel, bots, monitores. Un solo MatrixSyncService por client, durante toda la vida de la aplicacion.
Gotchas
- Solo UN Sync por client: dos goroutines llamando
SyncWithContextsimultaneamente sobre el mismo client rompe elsincetoken y produce duplicados o perdidas. Esta funcion garantiza una sola goroutine de sync si es llamada una sola vez. NO llamarMatrixSyncServicedos veces sobre el mismo*mautrix.Client. - Crypto antes del Sync: mensajes
m.room.encryptedque llegan antes de inicializarMatrixCryptoInitquedan sin descifrar (emitidos conType:"encrypted",Body:"",Raw:*event.Event). Inicializar crypto siempre ANTES de llamar a esta funcion. - Buffer de channel: si el consumer no drena
Eventscon suficiente rapidez, el sync se bloquea en el punto de emision. Synapse puede acumular deltas. Mantener el consumer rapido o aumentarChannelBuffer. - Errores fatales (401/M_UNKNOWN_TOKEN): no cierran el servicio automaticamente — se emiten a
Errorsy el servicio espera con backoff maximo. El caller decide llamarStop()y re-autenticar. - Stop idempotente: llamar
Stop()multiples veces es seguro; no causa panic. - Build tag: el paquete
infrarequiere-tags goolmpara compilar tests sin libolm (dependencia C de la crypto de mautrix). Los tests usan//go:build goolm.