Admit + authorize
Request đi qua route, limits và application authorization trước khi feed bắt đầu.
Product knowledge · Advanced · Realtime sessions
Chọn response stream cho byte tuần tự, SSE cho event một chiều và WebSocket cho session hai chiều. Cả ba vẫn cần admission, authorization, pressure, cancellation và finite shutdown.
01 · Choose the interaction
| Shape | Contract | Dùng khi |
|---|---|---|
| Response stream | Một HTTP response, server → client, bounded chunks và optional trailers. | Byte tuần tự, export hoặc generation incremental rồi kết thúc. |
| SSE | Một HTTP event stream, server → client, event fields có kiểu. | Notification/feed mà client không cần gửi message trên cùng channel. |
| WebSocket | HTTP handshake rồi upgraded bidirectional session với typed frames. | Client và server cùng cần gửi message trong một session. |
API hiện diện không chứng minh target đã bật provider. Đọc effective capability từ exact package đã load trước khi quảng bá endpoint realtime.
02 · SSE lifecycle
Request đi qua route, limits và application authorization trước khi feed bắt đầu.
Dùng helper/value của connector; Core sở hữu HTTP framing và transport state.
Chỉ submit event có bound khi writable; producer và source queue vẫn do application sở hữu.
Normal end, disconnect, timeout hoặc drain đều dừng producer và release retained state.
03 · SSE event fields
04 · WebSocket lifecycle
Validate route, origin/credential theo application policy và requested subprotocol. Plain GET không phải WebSocket client.
Chỉ sau accepted upgrade mới có session. Transport authentication vẫn không thay business authorization.
Consume/copy borrowed payload trong connector lifetime; keep pending output, handler tasks và app queues hữu hạn.
Close handshake, peer abort, cancel và service drain đều hội tụ về một idempotent cleanup owner.
05 · Frames and messages
Coi decoded text là application input chưa tin cậy; text không tự là JSON hoặc một schema hợp lệ.
Bytes có media/schema do application định nghĩa; borrowed payload phải copy trước khi giữ.
Control/progress evidence của session; không thay fresh business liveness hoặc authorization.
Protocol close intent và reason; application vẫn dọn task, subscription và retained state do application sở hữu.
06 · Bounds and pressure
| Boundary | Khi đầy/chậm |
|---|---|
| Concurrent sessions | Refuse/shed admission; không spawn owner hoặc subscription vô hạn. |
| Frame/message bytes | Reject/close theo typed limit outcome; không collect vượt ceiling. |
| Pending output | Wait bounded writability, coalesce/drop chỉ khi business semantics cho phép, hoặc close. |
| Application source queue | Application chọn finite capacity và overflow law; Core pressure không bound queue này thay app. |
| Progress/idle/drain time | Use monotonic finite deadline; timeout không chứng minh peer đã xử lý hay cleanup hoàn tất. |
07 · Reconnect and resume
Client reconnect theo bounded policy. Application có thể dùng event ID/cursor để đọc lại từ store riêng; nếu không có replay store, feed chỉ tiếp tục từ policy hiện tại.
Handshake mới tạo session mới. Resume token, sequence, dedupe và subscription restore là application protocol, không phải transport guarantee.
Disconnect không chứng minh event/message cuối chưa tới. Command có side effect cần idempotency hoặc reconciliation trước retry.
Bound attempts, total time và jitter ở client/application; server không biến reconnect storm thành unbounded admission.
08 · Finite drain
remove instance from new-traffic selection
→ stop new realtime admission
→ stop or quiesce application producers
→ request bounded SSE finish / WebSocket close
→ wait for terminals within monotonic drain deadline
→ close service only after owned readers/tasks converge
deadline/refusal → report failure → keep owner reachable → operator escalationLoad balancer removal chỉ ngăn selection mới; keep-alive, SSE và WebSocket cũ vẫn có thể sống. Deployment cần spare capacity, reconnect policy và outer stop budget.
09 · Failure law
| Failure | Owner action |
|---|---|
| Capability unavailable | Fail closed/disable route for that target; không silently downgrade thành một contract khác. |
| Handshake/auth/subprotocol refusal | Application trả public-safe HTTP refusal trước upgrade; không tạo fake session. |
| Session/frame/output limit | Refuse, shed hoặc close với typed evidence; không che bằng hidden queue. |
| Peer disconnect/abort | Cancel producers/subscriptions, release borrowed/retained state; business delivery có thể ambiguous. |
| Drain/close timeout | Giữ service và application owner còn reachable; timeout không cho phép free live state. |
10 · Integration checklist