102 lines
3.5 KiB
Go
102 lines
3.5 KiB
Go
// application-resources 演示同一应用的两个命名空间共享积压额度。
|
|
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 client(path string) (*orbit.Client, string, error) {
|
|
raw, err := os.ReadFile(path)
|
|
if err != nil {
|
|
return nil, "", err
|
|
}
|
|
var v struct {
|
|
Token string `json:"token"`
|
|
QueueID string `json:"queue_id"`
|
|
}
|
|
if err := json.Unmarshal(raw, &v); err != nil {
|
|
return nil, "", err
|
|
}
|
|
c, err := orbit.NewClient(os.Getenv("ORBIT_URL"), orbit.Options{Token: v.Token})
|
|
return c, v.QueueID, err
|
|
}
|
|
func run() (err error) {
|
|
a, qa, err := client(os.Getenv("ORBIT_CREDENTIALS_FILE"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
b, qb, err := client(os.Getenv("ORBIT_SECOND_CREDENTIALS_FILE"))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
|
defer cancel()
|
|
before, err := a.GetApplicationResources(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
peer, err := b.GetApplicationResources(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if before.ApplicationID != peer.ApplicationID || qa == qb || before.UnfinishedTasks != 0 {
|
|
return errors.New("use two namespaces in the same idle, dedicated example application")
|
|
}
|
|
op, err := orbit.NewWorkerMessageID()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
config, err := a.ConfigureApplicationResources(ctx, "config-"+op, orbit.ConfigureApplicationResourcesRequest{ExpectedRevision: before.Revision, Reason: "cross namespace resource example", MaxUnfinishedTasks: 1, MaxConcurrency: 2, RatePerSecond: 10})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
cleanup, stop := context.WithTimeout(context.Background(), 8*time.Second)
|
|
defer stop()
|
|
_, restoreErr := a.ConfigureApplicationResources(cleanup, "restore-"+op, orbit.ConfigureApplicationResourcesRequest{ExpectedRevision: config.Resources.Revision, Reason: "restore example configuration", MaxUnfinishedTasks: before.MaxUnfinishedTasks, MaxConcurrency: before.MaxConcurrency, RatePerSecond: before.RatePerSecond})
|
|
err = errors.Join(err, restoreErr)
|
|
}()
|
|
input := orbit.SubmitTaskRequest{QueueID: qa, Handler: "object.prepare", HandlerVersion: "v1", PayloadVersion: "v1", Payload: json.RawMessage(`{"object_ref":"objects/example"}`), TaskTimeoutMS: 20000, Retry: orbit.RetryPolicy{MaxAttempts: 1, AttemptTimeoutMS: 10000}}
|
|
first, err := a.SubmitTask(ctx, "first-"+op, input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
input.QueueID = qb
|
|
_, err = b.SubmitTask(ctx, "second-"+op, input)
|
|
var full *orbit.APIError
|
|
if !errors.As(err, &full) || full.Code != "APPLICATION_FULL" {
|
|
return fmt.Errorf("expected APPLICATION_FULL, got %v", err)
|
|
}
|
|
fmt.Printf("cross_namespace_backpressure=%s http=%d\n", full.Code, full.StatusCode)
|
|
if _, err := a.CancelTask(ctx, first.Task.ID, "cancel-first-"+op, orbit.CancelTaskRequest{ExpectedRevision: 1, Reason: "free shared slot"}); err != nil {
|
|
return err
|
|
}
|
|
second, err := b.SubmitTask(ctx, "second-"+op, input)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("same_identity_retry_accepted=%t\n", second.Task.State == orbit.TaskWaiting)
|
|
if _, err := b.CancelTask(ctx, second.Task.ID, "cancel-second-"+op, orbit.CancelTaskRequest{ExpectedRevision: 1, Reason: "complete example"}); err != nil {
|
|
return err
|
|
}
|
|
after, err := a.GetApplicationResources(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fmt.Printf("unfinished=%d active=%d pending_starts=%d\n", after.UnfinishedTasks, after.ActiveAttempts, after.PendingStarts)
|
|
return nil
|
|
}
|