docs: consolidate runnable examples and publish v0.1.1 guides
This commit is contained in:
32
examples/event-protocol/README.md
Normal file
32
examples/event-protocol/README.md
Normal file
@@ -0,0 +1,32 @@
|
||||
# WebSocket 结果通知与 ACK
|
||||
|
||||
接收取消事件并确认,断开后重报原证明,验证 ACK 去重。
|
||||
|
||||
## 准备
|
||||
|
||||
- Go 1.27.1、当前版本 Orbit;服务启动和初始化见[仓库 README](../../README.md#快速开始)。
|
||||
- 初始化时增加 --event-access;用隔离队列运行,避免混入其他取消事件。
|
||||
- [示例总索引](../README.md)提供统一初始化和权限说明;完整字段、失败处理见[详细手册](../../docs/examples/event-protocol.md)。
|
||||
|
||||
## 运行
|
||||
|
||||
以下命令从仓库根目录执行;凭据路径替换为实际初始化输出。
|
||||
|
||||
```sh
|
||||
cd examples
|
||||
export ORBIT_URL=http://127.0.0.1:8080
|
||||
export ORBIT_CREDENTIALS_FILE="$PWD/../.local/development.credentials.json"
|
||||
go run ./event-protocol
|
||||
```
|
||||
|
||||
## 结果核对
|
||||
|
||||
ack_replayed=true same_receipt=true pending=0 subscription_enabled=false。具体 ID 和统计值以本次运行为准。程序会核对上述关键条件,失败时打印错误并以非零状态退出。
|
||||
|
||||
## 接入自己的项目
|
||||
|
||||
阅读[main.go](main.go)中的客户端创建、请求及错误处理。业务项目通过 `go get git.yuntuo.hk/k2safe/orbit/sdk/go@v0.1.1` 引入 SDK,无需导入本示例包。
|
||||
|
||||
示例运行会写入真实平台数据,应使用专用测试应用/队列。正常结束保留历史和审计;重复演示通常创建新身份,不能当作上次失败操作的恢复。真实业务应在第一次请求前持久化操作身份,网络结果不明时沿原身份核对。
|
||||
|
||||
每次演示保留一个暂停订阅,命名空间最多 64 个订阅;不要将重复运行示例作为持续消费者。
|
||||
112
examples/event-protocol/main.go
Normal file
112
examples/event-protocol/main.go
Normal file
@@ -0,0 +1,112 @@
|
||||
// event-protocol 验证冻结结果、连接切换与ACK重报;不执行业务写入。
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
orbit "git.yuntuo.hk/k2safe/orbit/sdk/go"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
func run() error {
|
||||
token, queue := os.Getenv("ORBIT_TOKEN"), os.Getenv("ORBIT_QUEUE_ID")
|
||||
if path := os.Getenv("ORBIT_CREDENTIALS_FILE"); path != "" {
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var credential struct {
|
||||
Token string `json:"token"`
|
||||
QueueID string `json:"queue_id"`
|
||||
}
|
||||
if json.Unmarshal(raw, &credential) != nil {
|
||||
return errors.New("invalid credential file")
|
||||
}
|
||||
if token == "" {
|
||||
token = credential.Token
|
||||
}
|
||||
if queue == "" {
|
||||
queue = credential.QueueID
|
||||
}
|
||||
}
|
||||
if token == "" || queue == "" {
|
||||
return errors.New("event-enabled token and isolated example queue are required")
|
||||
}
|
||||
api, err := orbit.NewClient(os.Getenv("ORBIT_URL"), orbit.Options{Token: token})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, stop := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer stop()
|
||||
id, err := orbit.NewEventMessageID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
created, err := api.CreateSubscription(ctx, "sub-"+id, orbit.CreateSubscriptionRequest{Name: "protocol-" + id, QueueID: queue, Types: []string{"task.cancelled"}, MaxPending: 10, MaxAttempts: 3})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sub := created.Subscription
|
||||
task, err := api.SubmitTask(ctx, "task-"+id, orbit.SubmitTaskRequest{QueueID: queue, Handler: "object.prepare", HandlerVersion: "v1", PayloadVersion: "v1", Payload: json.RawMessage(`{}`)})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = api.CancelTask(ctx, task.Task.ID, "cancel-"+id, orbit.CancelTaskRequest{ExpectedRevision: task.Task.Revision, Reason: "event protocol example"}); err != nil {
|
||||
return err
|
||||
}
|
||||
first, err := api.DialEvents(ctx, orbit.EventRegistration{SubscriptionID: sub.ID, Capacity: 1})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer first.Close()
|
||||
grant, err := first.Next(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if grant.Event.Snapshot.TaskID != task.Task.ID || grant.Event.Snapshot.State != orbit.TaskCancelled {
|
||||
return errors.New("unexpected example queue event")
|
||||
}
|
||||
// 本例只读取并验证结果,没有业务数据库写入;业务处理必须先提交Inbox事务再ACK。
|
||||
ackID, err := orbit.NewEventMessageID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
confirmed, err := first.Ack(ctx, ackID, grant.Proof())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
first.Close()
|
||||
second, err := api.DialEvents(ctx, orbit.EventRegistration{SubscriptionID: sub.ID, Capacity: 1})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer second.Close()
|
||||
replayID, err := orbit.NewEventMessageID()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
replayed, err := second.Ack(ctx, replayID, grant.Proof())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !replayed.Deduplicated || !replayed.AcknowledgedAt.Equal(confirmed.AcknowledgedAt) {
|
||||
return errors.New("acknowledgement receipt changed across connections")
|
||||
}
|
||||
paused, err := api.ConfigureSubscription(ctx, sub.ID, "pause-"+id, orbit.ConfigureSubscriptionRequest{ExpectedRevision: sub.Revision, Enabled: false, Reason: "protocol example completed"})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("event=%s task=%s state=%s\n", grant.Event.ID, task.Task.ID, grant.Event.Snapshot.State)
|
||||
fmt.Printf("ack_replayed=%t same_receipt=true pending=%d subscription_enabled=%t\n", replayed.Deduplicated, paused.Subscription.PendingCount, paused.Subscription.Enabled)
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user