ワーカーの動作
Cloudflare Workersは、ブラウザやNode.jsのJavaScript ↗と似た動作をしますが、コードについて考える方法にはいくつかの違いがあります。内部的には、WorkersランタイムはV8エンジン ↗を使用しています — ChromiumやNode.jsで使用されているのと同じエンジンです。Workersランタイムは、ほとんどの現代のブラウザで利用可能な多くの標準APIも実装しています。
ブラウザやNode.js用に書かれたJavaScriptの違いは、ランタイムで発生します。個々のマシン上で実行されるのではなく(例えば、ブラウザアプリケーションや中央集権的なサーバー ↗上で)、Workers関数はCloudflareのグローバルネットワーク ↗上で実行されます - 数百の場所に分散された数千のマシンから成る成長中のグローバルネットワークです。
これらのマシンのそれぞれは、Workersランタイムのインスタンスをホストしており、それぞれのランタイムは数千のユーザー定義アプリケーションを実行することができます。このガイドでは、これらの違いのいくつかをレビューします。
詳細については、コンテナなしのクラウドコンピューティングに関するブログ記事 ↗を参照してください。
主な違いは、Isolates、リクエストごとの計算、分散実行の3つです。
V8 ↗は、あなたのコードにアクセスできる変数と安全に実行される環境を提供する軽量コンテキストであるisolatesを調整します。isolatesは、関数が実行されるサンドボックスと考えることもできます。
A single instance of the runtime can run hundreds or thousands of isolates, seamlessly switching between them. Each isolate’s memory is completely isolated, so each piece of code is protected from other untrusted or user-written code on the runtime. Isolates are also designed to start very quickly. Instead of creating a virtual machine for each function, an isolate is created within an existing environment. This model eliminates the cold starts of the virtual machine model.
Unlike other serverless providers which use containerized processes ↗ each running an instance of a language runtime, Workers pays the overhead of a JavaScript runtime once on the start of a container. Workers processes are able to run essentially limitless scripts with almost no individual overhead. Any given isolate can start around a hundred times faster than a Node process on a container or virtual machine. Notably, on startup isolates consume an order of magnitude less memory.
特定のisolateは独自のスコープを持っていますが、isolateは必ずしも長寿命ではありません。isolateは、以下の理由でスピンダウンされ、追い出されることがあります:
- マシンのリソース制限。
- 疑わしいスクリプト - isolateサンドボックスから抜け出そうとするものと見なされるもの。
- 個別のリソース制限。
このため、一般的には、これらの事態を考慮しない限り、グローバルスコープに可変状態を保存しないことが推奨されます。
CloudflareがWorkersランタイムでのセキュリティをどのように扱っているかに興味がある場合は、IsolatesがセキュリティおよびSpectre脅威の軽減にどのように関連しているかについてもっと読むことができます。
Most Workers are a variation on the default Workers flow:
export default { async fetch(request, env, ctx) { return new Response('Hello World!'); },};export default { async fetch(request, env, ctx): Promise<Response> { return new Response('Hello World!'); },} satisfies ExportedHandler<Env>;For Workers written in ES modules syntax, when a request to your *.workers.dev subdomain or to your Cloudflare-managed domain is received by any of Cloudflare’s data centers, the request invokes the fetch() handler defined in your Worker code with the given request. You can respond to the request by returning a Response object.
Isolatesは、リクエストの期間中は弾力性があり、継続的に利用可能ですが、稀にisolateが追い出されることがあります。Workerが公式の制限に達したり、リクエストが実行されているマシンでリソースが非常に逼迫している場合、ランタイムはイベントが適切に解決された後にisolateを選択的に追い出します。
他のすべてのJavaScriptプラットフォームと同様に、単一のWorkersインスタンスは、単一スレッドのイベントループ内で複数のリクエストを処理することができます。つまり、リクエストを処理している間に他のリクエストが来た場合、asyncタスク(例えばfetch)を待機している間に他のリクエストが処理される可能性があります(または処理されない可能性があります)。
ユーザーのリクエストが同じまたは異なるWorkerのインスタンスにルーティングされる保証はないため、Cloudflareはグローバル状態を使用したり変更したりしないことを推奨しています。
fetch()ハンドラー - WorkerへのHTTPリクエストがfetch()ハンドラーに渡される方法を確認します。- Request - 受信したHTTPリクエストが
fetch()ハンドラーに渡される方法を学びます。 - Workersの制限 - Workerのサイズ、起動時間などを含むWorkersの制限について学びます。