Por lo general, las fallas de publicación las causa el cliente y cuellos de botella, como CPU de servicio insuficientes, estado de subprocesos deficiente o la congestión de la red. La política de reintentos del editor define de las veces que Pub/Sub intenta entregar un mensaje y el tiempo que tarda entre cada intento.
En este documento, se proporciona información sobre el uso de solicitudes de reintento con mensajes que se publican en un tema.
Antes de comenzar
Antes de configurar el flujo de trabajo de publicación, asegúrate de haber completado los siguientes pasos: tareas:
- Obtén más información sobre los temas y el flujo de trabajo de publicación.
- Crea un tema.
Roles obligatorios
Para obtener los permisos que necesitas para reintentar las solicitudes de mensajes a un tema,
solicita a tu administrador que te otorgue el
Rol de IAM de publicador de Pub/Sub (roles/pubsub.publisher
) en el tema.
Para obtener más información sobre cómo otorgar roles, consulta Administra el acceso a proyectos, carpetas y organizaciones.
También puedes obtener los permisos necesarios mediante roles personalizados o cualquier otro rol predefinido.
Necesitas permisos adicionales para crear o actualizar temas y suscripciones.
Información acerca de las solicitudes de reintento
La configuración de reintento controla cómo las bibliotecas cliente de Pub/Sub reintentan las solicitudes de publicación. Las bibliotecas cliente tienen cualquiera de las siguientes configuraciones de reintento:
- Tiempo de espera de solicitud inicial: La cantidad de tiempo antes de que una biblioteca cliente deja de esperar a que se complete la solicitud de publicación inicial.
- Retraso de reintentos: La cantidad de tiempo que una biblioteca cliente espera para reintentar una solicitud después de que se agota el tiempo de espera de la solicitud.
- Tiempo de espera total: La cantidad de tiempo después de que una biblioteca cliente deje de reintentar las solicitudes de publicación.
Para reintentar las solicitudes de publicación, el tiempo de espera de la solicitud inicial debe ser menor que el tiempo de espera total. Por ejemplo, si usas la retirada exponencial, las bibliotecas cliente calculan el tiempo de espera de la solicitud y la demora en el reintento de la siguiente manera:
- Después de cada solicitud de publicación, el tiempo de espera de la solicitud aumenta según el multiplicador de tiempo de espera de la solicitud, hasta el tiempo máximo de espera de la solicitud.
- Después de cada reintento, la demora de reintentos aumenta según el multiplicador de demora en reintentos, hasta el retraso máximo de reintentos.
Reintentar una solicitud de mensaje
Durante el proceso de publicación, es posible que veas una publicación transitoria o permanente fallas. En el caso de los errores transitorios, generalmente no es necesario tomar ninguna medida ya que Pub/Sub reintenta enviar los mensajes automáticamente.
Un error también puede ocurrir cuando una operación de publicación tiene éxito, pero esta el cliente publicador no recibe a tiempo la respuesta. En este caso, también se vuelve a intentar la operación de publicación. Como resultado, puedes tener dos mensajes idénticos con IDs de mensaje diferentes.
En caso de errores persistentes, considera implementar las acciones adecuadas fuera del proceso de publicación para evitar sobrecargar Pub/Sub.
Las fallas de publicación se reintentan automáticamente, excepto los errores que no que garantizan reintentos. Este código de muestra muestra cómo crear un publicador con Configuración de reintentos (ten en cuenta que no todas las bibliotecas cliente admiten los reintentos personalizados) configuración; Consulta la documentación de referencia de la API. para el idioma que elegiste):
C++
Antes de probar esta muestra, sigue las instrucciones de configuración de C++ en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para C++.
C#
Antes de probar esta muestra, sigue las instrucciones de configuración de C# en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para C#.
Go
Antes de probar esta muestra, sigue las instrucciones de configuración de Go en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Go.
Java
Antes de probar esta muestra, sigue las instrucciones de configuración de Java en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Java.
Node.js
Antes de probar esta muestra, sigue las instrucciones de configuración de Node.js en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Node.js.
Node.js
Antes de probar esta muestra, sigue las instrucciones de configuración de Node.js en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Node.js.
Python
Antes de probar esta muestra, sigue las instrucciones de configuración de Python en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Python.
Cómo reintentar solicitudes con claves de ordenamiento
Supongamos que tienes un solo cliente publicador. Estás usando el Pub/Sub para publicar los mensajes 1, 2 y 3 del la misma clave de orden A. Ahora, supongamos que la respuesta publicada para el mensaje 1 es que el cliente publicador no recibe antes de que venza el plazo de RPC. El mensaje 1 se debe volver a publicar. La secuencia de mensajes que recibió el cliente suscriptor se convierte en 1, 1, 2 y 3, si supones que el mensaje 2 se publica solo después de mensaje 1 se completa correctamente. Cada mensaje publicado tiene su propio ID de mensaje. Desde la perspectiva del cliente suscriptor, se mostraron cuatro mensajes se publicaron y los dos primeros tienen el mismo contenido.
Volver a intentar las solicitudes de publicación con claves de orden también puede ser complicado por lotes configuración. La biblioteca cliente agrupa los mensajes para lograr una publicación más eficiente. Continúa con el ejemplo anterior y imagina que los mensajes 1 y 2 se agrupan en lotes. Este lote se envía al servidor como una sola solicitud. Si el servidor no muestra una respuesta a tiempo, el cliente publicador vuelve a intentar esto lote de dos mensajes. Por lo tanto, es posible que el cliente suscriptor recibe los mensajes 1, 2, 1, 2 y 3. Si usas Pub/Sub biblioteca cliente para publicar mensajes en orden y una operación de publicación falla, el servicio falla en las operaciones de publicación para todos los mensajes restantes del mismo clave de ordenamiento. Un cliente publicador puede decidir seguir cualquiera de las siguientes operaciones:
Volver a publicar todos los mensajes con errores en orden
Volver a publicar un subconjunto de los mensajes con errores en orden
Publica un nuevo conjunto de mensajes
Si se produce un error que no se puede reintentar, la biblioteca cliente no publica el mensaje y deja de publicar otros mensajes con la misma clave de ordenamiento. Por ejemplo, cuando un publicador envía un mensaje a un tema que no existe, se produce un error que no se puede reintentar. Para continuar publicando mensajes con la misma clave de ordenamiento, llama a un método para reanudar la publicación y comienza a realizar publicaciones nuevamente.
En el siguiente ejemplo, se muestra cómo reanudar la publicación de mensajes con la misma clave de ordenamiento.
C++
Antes de probar esta muestra, sigue las instrucciones de configuración de C++ en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para C++.
C#
Antes de probar esta muestra, sigue las instrucciones de configuración de C# en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para C#.
Go
Antes de probar esta muestra, sigue las instrucciones de configuración de Go en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Go.
Java
Antes de probar esta muestra, sigue las instrucciones de configuración de Java en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Java.
Node.js
Antes de probar esta muestra, sigue las instrucciones de configuración de Node.js en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Node.js.
Python
Antes de probar esta muestra, sigue las instrucciones de configuración de Python en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Python.
Ruby
Antes de probar esta muestra, sigue las instrucciones de configuración de Ruby en la guía de inicio rápido sobre el uso de bibliotecas cliente. Si quieres obtener más información, consulta la documentación de referencia de la API de Pub/Sub para Ruby.
¿Qué sigue?
Para aprender a configurar opciones de publicación avanzadas, consulta lo siguiente: