Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Cloudflare WorkersでGoを動かすライブラリを作っている話

Avatar for syumai syumai
January 24, 2023

Cloudflare WorkersでGoを動かすライブラリを作っている話

Avatar for syumai

syumai

January 24, 2023
Tweet

More Decks by syumai

Other Decks in Programming

Transcript

  1. 自己紹介 syumai Go Documentation 輪読会 / ECMAScript 仕様輪 読会 主催

    株式会社ベースマキナで管理画面のSaaS を開発中 Go でGraphQL サーバー (gqlgen) や TypeScript で フロントエンドを書いています Twitter: @__syumai Website: https://syum.ai
  2. syumai/workers とは https://github.com/syumai/workers http.Handler を作って、 workers.Serve に渡すだけでCloudflare Workers 上でHTTP サーバーとして動作する

    必要なツールはtinygo とwrangler (Cloudflare Workers のCLI) だけ tinygo を使ってWebAssembly を生成して実行する
  3. workers を使ったコードのサンプル 普通に http.HandlerFunc を作るだけ func main() { handler :=

    http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { name := req.URL.Query().Get("name") if name == "" { name = "world" } fmt.Fprintf(w, "Hello, %s!", name) }) workers.Serve(handler) } https://hello.syumai.workers.dev/?name=syumai (=> Hello, syumai と表示)
  4. gqlgen 製のGraphQL サーバー gqlgen 公式から拾ってきたSTAR WARS server STAR WARS 関連の情報をGraphQL

    経由で引っ張るAPI https://gqlgen-starwars-example.syumai.workers.dev/ https://github.com/syumai/workers-playground/tree/main/gqlgen- starwars-example
  5. syum.ai のベンチマーク (hey で200req) Summary: Total: 1.6804 secs Slowest: 1.4459

    secs Fastest: 0.0234 secs Average: 0.2505 secs Requests/sec: 119.0185 Response time histogram: 0.023 [1] | 0.166 [149] |▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪▪ 0.308 [1] | 0.450 [0] | 0.592 [0] | 0.735 [16] |▪▪▪▪ <- 700ms以上かかっているリクエストが結構ある 0.877 [22] |▪▪▪▪▪▪ 1.019 [3] |▪ 1.161 [4] |▪ 1.304 [3] |▪ 1.446 [1] | Latency distribution: 10% in 0.0329 secs 25% in 0.0558 secs 50% in 0.0741 secs 75% in 0.1660 secs 90% in 0.7640 secs <- 90 percentileで約760ms 95% in 0.9667 secs 99% in 1.2275 secs
  6. syum.ai のベンチマーク Go のWorker は0ms cold start の限界を超えていそう 700ms 以上かかっているリクエストは、Worker

    のロードが追いつい ていない感じがする (Wasm のサイズは、未圧縮で5.6MB) とは言え、75 percentile で約170ms なので、実用的なスピードは出て はいる
  7. Bundled / Unbound のプラン内容 Bundled: 50ms CPU time / invocation

    Unbound: 400,000 GB-s https://developers.cloudflare.com/workers/platform/pricing/
  8. Bundled / Unbound どちらを使うか? 料金的には、Bundled のプランが月$5 で1000 万リクエストを捌けるの で、こちらの方が魅力的 Unbound

    だと同じ料金で100 万リクエストで、使用メモリ量に対す る課金も生まれる ただ、50ms CPU time に収まらないので、Go のWorker はUnbound 必須と言えそう… 。
  9. Cloudflare D1 サポート Cloudflare D1 の sql/driver を実装中 Go だけでCloudflare

    Worker 上でもっと実用的なアプリケーション を作れるようにしたいです! https://github.com/syumai/workers/pull/18