4bce095964
Mueve duckdb_open, clickhouse_open, postgres_open, matrix_* y keyring_token_store
del paquete monolitico functions/infra a subpaquetes propios
(functions/infra/{duckdb,clickhouse,postgres,matrix,keyring}). El paquete infra ya
no importa los drivers (go-duckdb, clickhouse-go, pgx, mautrix, go-keyring), por lo
que las apps que solo usan funciones ligeras (process, cron, http, sqlite) dejan de
arrastrarlos. Reduccion de binarios: dag_engine 72->10MB, registry_api 70->8.7MB,
services_api 70->9MB, call_monitor 68->6.6MB, sqlite_api 70->8.9MB.
Los IDs del registry se mantienen estables (domain: infra en frontmatter). Se
preservan los build tags goolm/libolm de matrix_crypto_init.
Tambien corrige TestSSEHandler: el test leia el body con un unico Read() que con
HTTP chunked solo capturaba el primer evento; ahora usa io.ReadAll hasta EOF.
Co-Authored-By: Claude Opus 4.8 (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/matrix_sync_service_test.go | functions/infra/matrix/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.