Open13

Serverless WebSocket

雪猫雪猫

Lambda

WebSocket の接続は API Gateway で管理してバックエンドは Lambda に実装します。

AWS SDK

Node.js 18 ランタイムには v3 がプリインストールされている。
それ未満のランタイムには v2 がプリインストールされている。

https://aws.amazon.com/jp/about-aws/whats-new/2022/11/aws-lambda-support-node-js-18/
https://makky12.hatenablog.com/entry/2022/07/19/120500
https://zenn.dev/shinnoki/articles/dc59fc61738ecf

CORS

WebSocket には関係なさそう。

https://developer.mozilla.org/ja/docs/Web/HTTP/CORS#simple_requests
https://www.serverless.com/framework/docs/providers/aws/events/http-api#cors-setup
https://dev.classmethod.jp/articles/apigateway-lambda-proxy-cors/

API Gateway

https://makky12.hatenablog.com/entry/2020/09/02/120500

雪猫雪猫
雪猫雪猫

Close Code

Code 理由
1001 クライアントでのブラウザ更新やタブ移動による切断
1005 クライアントから close() で切断
1006 デプロイ時などサーバーから切断
雪猫雪猫

遭遇したエラー

https://developers.cloudflare.com/durable-objects/platform/limits/

A WebSocket 'attachment' cannot be larger than 2048 bytes.'attachment' was 2061 bytes.

ws.serializeAttachment()The serialized size of value is limited to 2,048 bytes に引っかかっていそう。

Durable Object's isolate exceeded its memory limit due to overflowing the storage cache. This could be due to writing too many values to storage without stopping to wait for writes to complete, or due to reading too many values in a single operation (e.g. a large list()). All objects in the isolate were reset.

WebSocket の close 時に不要になった Storage のデータを削除しておらずゴミが増えていたので不要なデータを削除

The Durable Object's code has been updated, this version can no longer access storage.

デプロイのタイミングで動いていたコード?

https://developers.cloudflare.com/kv/platform/limits/

Too many API requests by single worker invocation.

Within a single invocation, a Worker can make up to 1,000 operations to external services に引っかかっていそう。
KV のデータが最大 500 個分必要なので get をその分呼んでいる。どうやら Workers が起動している間(constructor が呼ばれて終了するまで?)の複数のリクエストの累計のようなので超えてしまう。
REST API を使うことで bulk にできる。

https://developers.cloudflare.com/kv/api/read-key-value-pairs/#requesting-more-keys-per-worker-invocation-with-bulk-requests