

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Neptune 无服务器的心跳配置
<a name="best-practices-gremlin-heartbeat-serverless"></a>

在 Neptune Serverless 中使用 Gremlin WebSocket 客户端时，你需要适当配置客户端的 ping 间隔，以便在扩展事件期间保持稳定的连接。Gremlin 客户端使用 WebSocket 连接并定期发送 ping 来验证连接是否处于活动状态。客户端期望服务器在 ping 间隔时间范围内做出响应。如果服务器没有响应，客户端会自动关闭连接。

**对于 Neptune **预配置的**实例，我们建议将 ping 间隔设置为 5 秒。**对于 Neptune **Serverless 集群**，我们建议将 ping 间隔设置为至少 **20 秒**，以适应扩展操作期间可能出现的延迟。此参数控制客户端在向服务器写入数据之间等待多长时间，然后再发送 ping 以验证连接是否仍处于活动状态。

此参数的配置因客户端实现而异：

**Java 客户端配置**

对于 Java TinkerPop Gremlin 客户端，请配置以下参数：`keepAliveInterval`

```
Cluster.Builder builder = Cluster.build()
    .addContactPoint(endpoint)
    .keepAliveInterval(20000); // Configure ping interval in milliseconds
```

有关 Java 驱动程序配置的更多详细信息，请参阅 [Java TinkerPop 文档](https://tinkerpop.apache.org/docs/current/reference/#gremlin-java-configuration)。

**Go 客户端配置**

对于 Gremlin Go 客户端，请配置以下参数：`KeepAliveInterval`

```
rc, err := driver.NewDriverRemoteConnection(endpoint,
    func(settings *driver.DriverRemoteConnectionSettings) {
        settings.TraversalSource = "g"
        settings.AuthInfo = auth
        settings.KeepAliveInterval = 20 * time.Second // Configure ping interval
        ...
    })
```

有关 Go 驱动程序配置的更多详细信息，请参阅 [Go TinkerPop 文档](https://tinkerpop.apache.org/docs/current/reference/#gremlin-go-configuration)。

**JavaScript/Node.js 客户端配置**

对于 JavaScript /Node.js Gremlin 客户端，请配置以下参数：`pingInterval`

```
const gremlin = require('gremlin');
const DriverRemoteConnection = gremlin.driver.DriverRemoteConnection;

const connection = new DriverRemoteConnection(endpoint, {
    traversalSource: 'g',
    pingInterval: 20000  // Configure ping interval in milliseconds
});
```

有关 JavaScript 驱动程序配置的更多详细信息，请参阅[JavaScript TinkerPop 文档](https://tinkerpop.apache.org/docs/current/reference/#gremlin-javascript-configuration)。

**Python 客户端配置**

对于 Python Gremlin 客户端，ping 间隔通常在传输层进行管理。有关配置选项，请参阅特定的传输实现文档：

```
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection

g = traversal().with_remote(
    DriverRemoteConnection('wss://your-neptune-endpoint:your-neptune-port/gremlin','g',
        transport_factory=lambda: AiohttpTransport(read_timeout=60,
                                                    write_timeout=20,
                                                    heartbeat=20, // Configure heartbeat
                                                    call_from_event_loop=True,
                                                    max_content_length=100*1024*1024,
                                                    ssl_options=ssl.create_default_context(Purpose.CLIENT_AUTH))))
```

有关 Python 驱动程序配置的更多详细信息，请参阅 [Python TinkerPop 文档](https://tinkerpop.apache.org/docs/current/reference/#gremlin-python-configuration)。

此配置可确保您的客户端在 Neptune Serverless 扩展事件期间保持连接稳定性，从而防止不必要的连接关闭并提高应用程序的可靠性。