|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + restate "github.com/restatedev/sdk-go" |
| 5 | + "log/slog" |
| 6 | + "time" |
| 7 | +) |
| 8 | + |
| 9 | +type SubscriptionRequest struct { |
| 10 | + UserID string `json:"userId"` |
| 11 | +} |
| 12 | + |
| 13 | +type MyService struct{} |
| 14 | + |
| 15 | +func (MyService) Run(ctx restate.Context) error { |
| 16 | + // 1. IDEMPOTENCY: Add an idempotency key to the header of your requests |
| 17 | + // Restate deduplicates calls automatically. Nothing to do here. |
| 18 | + |
| 19 | + // 2. DURABLE RPC: Call other services without manual retry and deduplication logic |
| 20 | + // Restate persists all requests and ensures execution till completion |
| 21 | + response, err := restate.Object[string](ctx, "SubscriptionService", "my-sub-123", "Add"). |
| 22 | + Request(SubscriptionRequest{UserID: "123"}) |
| 23 | + if err != nil { |
| 24 | + return err |
| 25 | + } |
| 26 | + slog.Info("Response was: " + response) |
| 27 | + |
| 28 | + // 3. DURABLE MESSAGING: send (delayed) messages to other services without deploying a message broker |
| 29 | + // Restate persists the timers and triggers execution |
| 30 | + restate.ObjectSend(ctx, "SubscriptionService", "my-sub-123", "Add"). |
| 31 | + Send(SubscriptionRequest{UserID: "123"}) |
| 32 | + |
| 33 | + // 4. DURABLE PROMISES: tracked by Restate, can be moved between processes and survive failures |
| 34 | + // Awakeables: block the workflow until notified by another handler |
| 35 | + awakeable := restate.Awakeable[string](ctx) |
| 36 | + // Wait on the result |
| 37 | + result, err := awakeable.Result() |
| 38 | + if err != nil { |
| 39 | + return err |
| 40 | + } |
| 41 | + slog.Info("Promise resolved", "result", result) |
| 42 | + // Another process can resolve an awakeable with its ID |
| 43 | + restate.ResolveAwakeable[string](ctx, awakeable.Id(), "hello") |
| 44 | + |
| 45 | + // 5. DURABLE TIMERS: sleep or wait for a timeout, tracked by Restate and recoverable |
| 46 | + // When this runs on FaaS, the handler suspends and the timer is tracked by Restate |
| 47 | + // Example of durable recoverable sleep |
| 48 | + // If the service crashes two seconds later, Restate will invoke it after another 3 seconds |
| 49 | + err = restate.Sleep(ctx, 5*time.Second) |
| 50 | + if err != nil { |
| 51 | + return err |
| 52 | + } |
| 53 | + // Example of waiting on a promise (awakeable/call/...) or a timeout |
| 54 | + timeout := restate.After(ctx, 5*time.Second) |
| 55 | + selector := restate.Select(ctx, awakeable, timeout) |
| 56 | + switch selector.Select() { |
| 57 | + case awakeable: |
| 58 | + result, err := awakeable.Result() |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + slog.Info("Awakeable won with result: " + result) |
| 63 | + case timeout: |
| 64 | + if err := timeout.Done(); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + slog.Info("Sleep won") |
| 68 | + } |
| 69 | + // Example of scheduling a handler for later on |
| 70 | + restate.ObjectSend(ctx, "SubscriptionService", "my-sub-123", "Cancel"). |
| 71 | + Send(nil, restate.WithDelay(24*time.Hour)) |
| 72 | + |
| 73 | + // 7. PERSIST RESULTS: avoid re-execution of actions on retries |
| 74 | + // Use this for non-deterministic actions or interaction with APIs, DBs, ... |
| 75 | + // For example, generate idempotency keys that are stable across retries |
| 76 | + // Then use these to call other APIs and let them deduplicate |
| 77 | + paymentDeduplicationID := restate.Rand(ctx).UUID().String() |
| 78 | + success, err := restate.Run(ctx, func(ctx restate.RunContext) (string, error) { |
| 79 | + return chargeBankAccount(paymentDeduplicationID, 100) |
| 80 | + }) |
| 81 | + if err != nil { |
| 82 | + return err |
| 83 | + } |
| 84 | + slog.Info("Payment was successful: " + success) |
| 85 | + |
| 86 | + return nil |
| 87 | +} |
| 88 | + |
| 89 | +func chargeBankAccount(paymentDeduplicationID string, amount int64) (string, error) { |
| 90 | + // Implementation here |
| 91 | + return "", nil |
| 92 | +} |
0 commit comments