Declare + start
Builder cung cấp finite target snapshot; Core validate capability, target và bounds trước service readiness.
Product knowledge · Advanced · Service-owned outbound
Application chọn logical dependency và business policy. CoAkka HTTP Runtime sở hữu bounded target registry, endpoint/DNS/pool/TLS mechanics, request progress và typed terminal outcome.
01 · Ownership model
Đặt tên dependency, authorization/credential intent, request meaning, status mapping, idempotency và retry policy.
Validate target snapshot; sở hữu endpoint selection, DNS, connection pool, TLS, redirects, HTTP framing, deadlines và terminal facts.
Chiếu builder/request/terminal tự nhiên cho host language và bảo vệ type, buffer, callback cùng owner lifetime.
Sở hữu HTTP response/business effect của nó. Received response không tự có nghĩa business success.
Không gọi surface này là CoAkka HTTP Client. Đây là outbound API của service trong CoAkka HTTP Runtime; CoAkka HTTP Client là product riêng đang được hoạch định.
02 · Logical target declaration
logical target: sample.upstream · generation 1
endpoint: loopback
connect: 127.0.0.1:<port>
HTTP authority: 127.0.0.1:<port>
security: plaintext for this local example
incoming request data ──X──► arbitrary scheme/host/portLogical targets là construction-time snapshots. Handler chọn tên dependency đã khai báo; không biến query, header hoặc body của caller thành arbitrary outbound destination.
03 · Target fields
sample.upstream; request dùng key này thay vì URL tùy ý.SINGLE_OWNER chọn một owner hợp lệ; WEIGHTED_ROUND_ROBIN phân phối theo relative endpoint weights khi có nhiều candidate.04 · Request intent
submit:
logical target = sample.upstream
method = GET
path/query = /orders/A%2FB?view=summary
headers/body = bounded application values
call deadline = 3000 ms
returns: call identity · not an HTTP responseResolve variables và URL-encode value trước submit. Không gửi /orders/{id}, arbitrary full URL hoặc raw untrusted text vào field path/query.
05 · Request fields
| Field | Meaning |
|---|---|
| Logical target | Chọn declared dependency snapshot; không trực tiếp chọn arbitrary socket. |
| Method | HTTP method của call; không tự nói operation có idempotent hay safe để retry. |
| Path/query | Concrete encoded origin-form request target; không có fragment hoặc route syntax. |
| Headers/body | Ordered bounded HTTP values; application sở hữu auth token, media type, codec và secret policy. |
| Call deadline | Finite monotonic budget của operation; khác reader wait và inbound handler/transport timeouts. |
| Call identity | Opaque slot/generation identity trả về khi admission thành công; dùng để match terminal chính xác. |
06 · Call lifecycle
Builder cung cấp finite target snapshot; Core validate capability, target và bounds trước service readiness.
Core admit/refuse request và trả call identity. Chưa có remote response ở bước này.
Core chọn endpoint, resolve/connect, áp dụng security, gửi request và đọc bounded response theo deadline.
Một typed terminal gắn call identity cho biết cause/phase/certainty cùng optional HTTP response.
07 · Terminal facts
ADMISSION, PROVIDER_ADOPTION, TRANSPORT, RESPONSE, CANCELLATION, SHUTDOWN.404/500 vẫn có thể là RESPONSE.Giữ unknown numeric enum value để forward compatibility; không tự đổi nó thành một known category.
08 · Exact terminal vocabulary
| Dimension | Stable values và nghĩa |
|---|---|
| Reason | RESPONSE; PROVIDER_FAILURE/PROVIDER_REJECTED/INVALID_PROVIDER_RESULT; CANCELLED/DEADLINE_EXCEEDED/STOPPED; RESPONSE_HEAD_LIMIT/RESPONSE_BODY_LIMIT/RESPONSE_AGGREGATE_LIMIT; OUT_OF_MEMORY; CLOCK_FAILURE. |
| Retry | NEVER, IF_SAFE, DELIVERY_UNCERTAIN. Đây là transport disposition; IF_SAFE vẫn yêu cầu application chứng minh safe/idempotent. |
| Certainty | NO_REQUEST_BYTES_SENT, REQUEST_UNCERTAIN, RESPONSE_HEAD_OBSERVED, COMPLETE_RESPONSE. Nó mô tả wire evidence, không phải business commit state. |
| Provider code / diagnostic | Bounded operator evidence; không branch business logic bằng diagnostic text hoặc đưa nó thẳng vào public response. |
09 · Capacity and time
| Boundary | Required law |
|---|---|
| Target registry / endpoints | Finite declared entries; invalid/unsupported target fails closed. |
| DNS / connections / pools | Finite work, connections and retained idle state; exact target owns capability/effective facts. |
| In-flight calls | Finite ceiling; pressure/refusal không trở thành application hidden queue. |
| Redirects | Finite hop/policy bound; authority, credential scope và TLS identity phải vẫn đúng. |
| Response | Finite header/body/retained terminal bytes; application không collect vượt ceiling. |
| Call deadline | Operation progress budget. Deadline terminal khác reader wait timeout. |
| Terminal reader wait | Khoảng chờ để lấy event; hết wait chỉ nghĩa chưa quan sát terminal trong cửa sổ đó. |
10 · DNS, TLS and redirects
DNS/IP/port dùng mở connection; có thể khác HTTP authority.
Host/authority logic của request và virtual hosting; không suy từ resolved IP.
Danh tính certificate cần verify cùng trust roots; mTLS client identity là material riêng.
Mỗi hop vẫn chịu finite bound và configured trust/credential policy; không forward secret theo phỏng đoán.
Feature-check outbound và security provider của exact package. Không log private key, token, raw credential hoặc operator-only TLS diagnostic vào public response.
Advanced · Outbound HTTP
Chạy dịch vụ đích trên cùng máy trước. Mỗi ví dụ CoAkka khai báo upstream với địa chỉ cố định, gửi một GET rồi đọc kết quả tương ứng. Ví dụ không lấy URL bất kỳ từ request đầu vào.
mkdir -p upstream-lab
printf 'hello upstream\n' > upstream-lab/hello.txt
python3 -m http.server --protocol HTTP/1.1 8091 --bind 127.0.0.1 --directory upstream-labDùng SDK 2.3.0 đã kiểm tra. Lưu outbound.c trong thư mục dự án ở terminal thứ hai. Cùng mã C ABI này build được bằng trình biên dịch C hoặc C++ trên Linux và macOS.
#include <coakka/http/host.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static coakka_http_host_bytes_t bytes(const char *text) {
coakka_http_host_bytes_t span;
span.data = (const uint8_t *)text;
span.size = strlen(text);
return span;
}
static bool check(coakka_http_host_status_t status, const char *operation) {
if (status == COAKKA_HTTP_HOST_OK) {
return true;
}
fprintf(stderr, "%s: %s\n", operation, coakka_http_host_status_name(status));
return false;
}
static bool request_once(coakka_http_host_service_t *service) {
coakka_http_host_outbound_request_t request;
coakka_http_host_outbound_request_init(&request);
request.logical_target = bytes("upstream");
request.method = bytes("GET");
request.target = bytes("/hello.txt");
request.timeout_ms = 3000U;
coakka_http_host_outbound_call_t call;
if (!check(coakka_http_host_submit_outbound(service, &request, &call),
"submit outbound request")) {
return false;
}
coakka_http_host_outbound_event_t event;
coakka_http_host_outbound_event_init(&event);
if (!check(coakka_http_host_take_outbound(service, 5000U, &event),
"take outbound result")) {
return false;
}
const bool matched =
event.call.slot == call.slot && event.call.generation == call.generation;
const bool replied =
matched && event.reason == COAKKA_HTTP_HOST_OUTBOUND_RESPONSE;
if (replied) {
printf("HTTP %u\n", (unsigned)event.response_status);
if (event.response_status == 200U && event.response_body.size != 0U) {
// The response body is borrowed until the result is released.
fwrite(event.response_body.data, 1U, event.response_body.size, stdout);
}
} else {
fprintf(stderr,
"Outbound result: matched=%d reason=%" PRIu32 " phase=%" PRIu32
"\n",
(int)matched, event.reason, event.phase);
}
const bool released =
check(coakka_http_host_release_outbound(service, &event),
"release outbound result");
return replied && released;
}
int main(void) {
coakka_http_host_outbound_endpoint_t endpoint;
coakka_http_host_outbound_endpoint_init(&endpoint);
endpoint.node_id = bytes("local");
endpoint.connect_host = bytes("127.0.0.1");
endpoint.connect_port = 8091U;
endpoint.http_authority = bytes("127.0.0.1:8091");
endpoint.available = 1U;
coakka_http_host_outbound_target_t target;
coakka_http_host_outbound_target_init(&target);
target.name = bytes("upstream");
target.generation = 1U;
target.endpoints = &endpoint;
target.endpoint_count = 1U;
coakka_http_host_configuration_t configuration;
coakka_http_host_configuration_init(&configuration);
configuration.bind_address = bytes("127.0.0.1");
configuration.outbound_targets = ⌖
configuration.outbound_target_count = 1U;
coakka_http_host_service_t *service = NULL;
uint32_t failed_route = COAKKA_HTTP_HOST_FAILED_INDEX_NONE;
if (!check(coakka_http_host_service_create(&configuration, NULL, 0U, &service,
&failed_route),
"create outbound service")) {
return EXIT_FAILURE;
}
coakka_http_host_issue_t issue;
coakka_http_host_issue_init(&issue);
const bool replied =
check(coakka_http_host_service_start(service, &issue), "start service") &&
request_once(service);
const bool closed =
check(coakka_http_host_service_destroy(&service), "destroy service");
return replied && closed ? EXIT_SUCCESS : EXIT_FAILURE;
}
cc -std=c11 -I"$COAKKA_HTTP_SDK/include" outbound.c -L"$COAKKA_HTTP_SDK/lib" \
-Wl,-rpath,"$COAKKA_HTTP_SDK/lib" -lcoakka_http_host -o outbound-c
./outbound-cc++ -x c++ -std=c++20 -I"$COAKKA_HTTP_SDK/include" outbound.c -L"$COAKKA_HTTP_SDK/lib" \
-Wl,-rpath,"$COAKKA_HTTP_SDK/lib" -lcoakka_http_host -o outbound-cpp
./outbound-cppDùng dự án JVM 2.3.2. Lưu file trong thư mục study/ dưới src/main/kotlin hoặc src/main/java. Lệnh mặc định dùng macOS ARM64. Trên Pi, thêm -Dcoakka.target=linux-aarch64 vào Maven hoặc -PcoakkaTarget=linux-aarch64 vào Gradle.
package study
import coakka.http.OutboundEndpoint
import coakka.http.OutboundReason
import coakka.http.OutboundRequest
import coakka.http.OutboundStrategy
import coakka.http.OutboundTarget
import coakka.http.ServiceBuilder
fun main() {
val endpoint = OutboundEndpoint(
nodeId = "local",
connectHost = "127.0.0.1",
connectPort = 8091,
httpAuthority = "127.0.0.1:8091",
)
val target = OutboundTarget(
name = "upstream",
generation = 1,
strategy = OutboundStrategy.SINGLE_OWNER,
endpoints = listOf(endpoint),
)
ServiceBuilder().listen("127.0.0.1", 0).outboundTarget(target).start().use { service ->
val call = service.submitOutbound(OutboundRequest(
logicalTarget = "upstream",
method = "GET",
target = "/hello.txt",
timeoutMillis = 3_000,
))
val result = checkNotNull(service.takeOutbound(5_000)) { "Missing outbound result" }
check(result.call == call) { "Result belongs to a different call" }
check(result.reason == OutboundReason.RESPONSE) { "Outbound request ended: ${result.reason}" }
println("HTTP ${result.responseStatus}")
if (result.responseStatus == 200) print(result.responseBody().toString(Charsets.UTF_8))
}
}
mvn -q compile dependency:build-classpath -Dmdep.outputFile=target/classpath.txt
java -Djava.library.path=target/coakka-native/native \
-cp "target/classes:$(cat target/classpath.txt)" study.KotlinOutboundKt./gradlew runKotlinOutboundDùng dự án JVM 2.3.2. Lưu file trong thư mục study/ dưới src/main/kotlin hoặc src/main/java. Lệnh mặc định dùng macOS ARM64. Trên Pi, thêm -Dcoakka.target=linux-aarch64 vào Maven hoặc -PcoakkaTarget=linux-aarch64 vào Gradle.
package study;
import coakka.http.Headers;
import coakka.http.OutboundCallId;
import coakka.http.OutboundEndpoint;
import coakka.http.OutboundReason;
import coakka.http.OutboundRequest;
import coakka.http.OutboundStrategy;
import coakka.http.OutboundTarget;
import coakka.http.OutboundTerminal;
import coakka.http.Service;
import coakka.http.ServiceBuilder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
public final class JavaOutbound {
private JavaOutbound() {}
public static void main(String[] args) throws Exception {
OutboundEndpoint endpoint = new OutboundEndpoint(
"local", "127.0.0.1", 8091, "127.0.0.1:8091");
OutboundTarget target = new OutboundTarget(
"upstream", 1L, OutboundStrategy.SINGLE_OWNER, Collections.singletonList(endpoint));
Service service = new ServiceBuilder()
.listen("127.0.0.1", 0)
.outboundTarget(target)
.start();
try {
OutboundCallId call = service.submitOutbound(new OutboundRequest(
"upstream", "GET", "/hello.txt", new Headers(), new byte[0], 3_000));
OutboundTerminal result = service.takeOutbound(5_000L);
if (result == null || !result.getCall().equals(call)) {
throw new IllegalStateException("Missing result for this call");
}
if (result.getReason() != OutboundReason.RESPONSE) {
throw new IllegalStateException("Outbound request ended: " + result.getReason());
}
System.out.println("HTTP " + result.getResponseStatus());
if (result.getResponseStatus() == 200) {
System.out.print(new String(result.responseBody(), StandardCharsets.UTF_8));
}
} finally {
service.close();
}
}
}
mvn -q compile dependency:build-classpath -Dmdep.outputFile=target/classpath.txt
java -Djava.library.path=target/coakka-native/native \
-cp "target/classes:$(cat target/classpath.txt)" study.JavaOutbound./gradlew runJavaOutboundVới các ngôn ngữ khác, mở bài outbound đầy đủ: Go · Python · Node.js · Bun · C# · Swift.
Khi file còn đó, kết quả là HTTP 200 và hello upstream. Đổi tên hello.txt rồi chạy lại: HTTP 404 vẫn là response, không phải lỗi kết nối. Dừng dịch vụ đích rồi thử lại để thấy kết quả không có response. Hết thời gian chờ không có nghĩa là được phép gửi lại một thao tác nghiệp vụ.
11 · Cancellation, retry and idempotency
Cancellation là intent cho tới khi matching terminal xác nhận outcome. Đừng free caller state khi Core còn sở hữu call.
Hết deadline không chứng minh remote chưa nhận hoặc chưa tạo effect; response có thể đã bị mất trên đường về.
Tạo attempt/call identity mới, bounded attempts/backoff/total budget; giữ business idempotency key khi contract cần.
Với charge/create/non-idempotent work và outcome ambiguous, đọc authoritative business state trước khi lặp.
12 · Failure law
| Observed outcome | Meaning / next action |
|---|---|
| Startup target refusal | Declaration/capability/bound invalid; service chưa ready với candidate đó. |
| Synchronous submit refusal | Call chưa được admit; inspect stable category và pressure/closed state. |
| Transport/provider terminal | Không có HTTP response; branch reason/phase/retry/certainty và sửa đúng network/security/provider owner. |
| HTTP 4xx/5xx response | Remote đã trả HTTP response; application quyết định business mapping/retry. |
| Cancel/deadline terminal | Giữ ambiguity; không suy rollback hoặc exactly-once. |
| Reader wait timeout | Chưa lấy được terminal trong wait window; không tự submit duplicate. |
| Close/drain refusal | Giữ service/call/application owner reachable tới khi cleanup contract hội tụ. |
13 · Integration checklist