Skip to content

feat(edge-workers): Cloudflare Workers-style serverless edge runtime#4

Open
minjcore wants to merge 1 commit into
mainfrom
feat/edge-workers
Open

feat(edge-workers): Cloudflare Workers-style serverless edge runtime#4
minjcore wants to merge 1 commit into
mainfrom
feat/edge-workers

Conversation

@minjcore

Copy link
Copy Markdown
Owner

Tổng quan

apps/edge-workers — serverless edge runtime kiểu Cloudflare Workers, xây trên mô hình verticle của Fluxor.

Execution flow:

HTTP/IO  →  CPU layer (event loop · verticle)        →  engine (subprocess)  →  script
gateway      EdgeVerticle:                              PHP / Node              logic thuần
(net/http)    1. RunOnEventLoop → match route (<20µs)    (stdin → stdout)       zero HTTP
              2. SubmitBlockingFunc → worker pool

Event loop chỉ dispatch (route match, race-free); worker pool chạy subprocess blocking → event loop không bao giờ nghẽn. Dùng runtime thật của Fluxor: pkg/core (verticle + eventloop + worker pool), pkg/scheduler (cron), pkg/entrypoint (bootstrap).

Features

  • Engines: PHP (php) + Node (node) chạy subprocess ngắn hạn (1 request = 1 "isolate"), timeout cứng, capture stdout/stderr. Java loại cố ý (quá nặng cho per-request edge).
  • Script logic-only: worker không mở socket — đọc request JSON từ stdin, ghi response JSON ra stdout. Mọi networking ở tầng Go.
  • Routing: longest-prefix match.
  • KV binding: in-memory store, nhiều namespace + TTL. Cơ chế snapshot vào request → script trả mutations (put/delete) → write-back sau khi response xong. State bền vững cross-request, không cần network.
  • Cron trigger: worker khai báo cron expr (5-field) → schedule qua pkg/scheduler; fire bằng synthetic request trigger:"scheduled" qua cùng đường eventloop→pool. Validate an toàn (cron sai → 400, không crash).
  • Env / secrets binding: inject vào process environment của subprocess.
  • Management API (control plane): REST CRUD GET/POST/DELETE /workers + /health, deploy/xoá worker lúc runtime không cần restart. Bearer-token auth (ADMIN_TOKEN, so sánh constant-time); /health để mở.
  • Ops: ports config qua env (EDGE_ADDR/ADMIN_ADDR), cross-compile static linux binary (CGO off), 4 demo worker seed sẵn.

Files

engine.go · worker.go · kv.go · verticle.go · cron.go · server.go · main.go · workers/{hello.php,echo.js,counter.php,heartbeat.js} · README.md · Makefile · config.json

Đã test

  • gofmt + go build + go vet sạch.
  • Chạy thật trên 2 server Linux (systemd): PHP/Node/KV/cron đều verify — cron fire đúng mốc phút, KV counter persist cross-request, token auth chặn 401/cho 200 đúng.

🤖 Generated with Claude Code

Add apps/edge-workers, a serverless edge runtime built on the Fluxor
verticle model. Flow: HTTP/IO -> CPU (event loop + worker pool verticle)
-> lightweight engine (PHP/Node subprocess) -> user script.

- engine.go: Engine interface + PHP/Node subprocess engines (timeout,
  stdin/stdout JSON contract). JVM intentionally excluded as too heavy.
- verticle.go: EdgeVerticle dispatches on its event loop (route match,
  <20us) and offloads the blocking subprocess to the worker pool.
- worker.go: worker model, route registry (longest-prefix), request/
  response contract. Scripts handle logic only -- no HTTP/network.
- kv.go: in-memory KV store (namespaces + TTL) via snapshot/write-back.
- cron.go: cron triggers via pkg/scheduler, safe expression validation.
- server.go: HTTP gateway (data plane) + management API (control plane)
  with bearer-token auth (ADMIN_TOKEN, constant-time compare).
- main.go: bootstrap via pkg/entrypoint, env-configurable ports, demo
  worker seeding.
- workers/: demo scripts (hello.php, echo.js, counter.php KV,
  heartbeat.js cron).

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@minjcore minjcore requested a review from caokhang91 June 17, 2026 07:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant