简体   繁体   English

套接字 IO 事件有时无法从托管在 Google Cloud Run 上的服务到达客户端

[英]Socket IO event sometimes doesn't get to client from a Service hosted on Google Cloud Run

I have a NodeJS service hosted on Google Cloud Run that uses Socket IO to communicate back to the browser client whenever the service instance is running.我有一个托管在 Google Cloud Run 上的 NodeJS 服务,它使用 Socket IO 在服务实例运行时与浏览器客户端通信。

However, I am noticing something weird.但是,我注意到一些奇怪的事情。

The weird thing is that sometimes when the server emits a socket event to the client, the client gets the event immediately but on some other occasions the event never gets to the client.奇怪的是,有时当服务器向客户端发出套接字事件时,客户端会立即收到该事件,但在其他一些情况下,该事件永远不会到达客户端。 This happens so randomly that it's really hard to reproduce where the disconnection is coming from.这种情况发生得如此随机,以至于很难重现断开连接的来源。

Below is my client code:下面是我的客户端代码:

client_socket.js client_socket.js

import io from "socket.io-client";

const socketUrl = EndPoints.SOCKET_IO_BASE;
let socketOptions = { transports: ["websocket"] }
let socket;
if (!socket) {
    socket = io(socketUrl, socketOptions);
    socket.on('connect', () => {
        console.log(`Connected to Server`);
    })
    socket.on('disconnect', () => {
        console.log(`Disconnected from Server`); //This never gets called when the Cloud Run service instance is running, so I can assume a disconnect never happened.
    })
}
export default socket;

Funny enough, a disconnect event was never fired back to the client while the Cloud Run service instance is running, meaning the client is still connected to the service.有趣的是,当 Cloud Run 服务实例正在运行时,断开连接事件从未被触发回客户端,这意味着客户端仍然连接到该服务。 So, it's really weird that on some occasions it doesn't get events from the server even while been connected.因此,在某些情况下,即使已连接,它也不会从服务器获取事件,这真的很奇怪。

Please note that on the Google Cloud Run service side I have set the timeout of my service to 3600s which is more than good enough to ensure the service is running long enough to keep the socket connection in place.请注意,在 Google Cloud Run 服务端,我已将我的服务超时设置为 3600 秒,这足以确保服务运行足够长的时间以保持套接字连接到位。

Based on this documentation on best practices :基于关于最佳实践的文档:

The most difficult part of creating WebSockets services on Cloud Run is synchronizing data between multiple Cloud Run container instances.在 Cloud Run 上创建 WebSockets 服务最困难的部分是在多个 Cloud Run 容器实例之间同步数据。 This is difficult because of the autoscaling and stateless nature of container instances, and because of the limits for concurrency and request timeouts .由于容器实例的自动缩放和无状态性质,以及并发请求超时的限制,这很困难。

One suggestion is by using session affinity .一个建议是使用session affinity If enabled, Cloud Run will route sequential requests for a given client to the same container instance and will use a session affinity cookie with a TTL of 30 days.如果启用,Cloud Run 会将给定客户端的顺序请求路由到同一容器实例,并将使用 TTL 为 30 天的 session 关联 cookie。 It will also inspect the value to identify requests by the same client and direct the requests to the same instance.它还将检查该值以识别同一客户端的请求并将请求定向到同一实例。 Still, it is not guaranteed that it will be serviced by the same instance.尽管如此,仍不能保证它将由同一个实例提供服务。

Also, this feature is still in the preview phase and may change while still in development.此外,此功能仍处于预览阶段,可能会在开发过程中发生变化。

It is recommended to use external data storage such as database ( Cloud SQL ) or external message queue ( Redis Pub/Sub / Memorystore / Firestore real-time updates ) that can deliver updates to all instances over connections initiated by the container instance.建议使用外部数据存储,例如数据库( Cloud SQL )或外部消息队列( Redis Pub/Sub / Memorystore / Firestore 实时更新),可以通过容器实例发起的连接向所有实例传递更新。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase 托管重写不会重定向到 Google Cloud Run - Firebase Hosting rewrite doesn't redirect to Google Cloud Run PHP 托管在 Google Cloud Run 环境中的 WebServer:持久化文件(图片) - PHP WebServer hosted in Google Cloud Run environment: persist files (images) SSL 使用 Google Cloud Run 进行客户端身份验证 - SSL Client Authentication with Google Cloud Run 如何从 Java 脚本安全地调用 Google Cloud Run 服务? - How to securely call a Google Cloud Run service from Java Script? Cloud Run + Cloud Endpoints + Service Account Authentication – 在 curl 中有效,但在 JS 中使用 fetch API 时无效 - Cloud Run + Cloud Endpoints + Service Account Authentication – works in curl but doesn't when using fetch API in JS 我无法以编程方式使用 node.js 客户端库从 google-cloud-automl 获取 model id - I can't programmatically get the model id from google-cloud-automl with node.js client library Google Cloud Run - 创建服务任务永远加载 - Google Cloud Run - create service task is loadingforever Cloud Run 服务未获取客户端正确的 IP 地址 - Cloud Run Service not picking up client correct IP address 如何在不使用本地服务帐户密钥文件的情况下从 Cloud Run 服务访问 Google Cloud Storage 存储桶? - How to access to a Google Cloud Storage bucket from a Cloud Run service without using a local Service Account key file? “google-cloud-firestore”发行版未添加到 PyInstaller 构建中 - 'google-cloud-firestore' distribution doesn't get added to PyInstaller build
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM