-
Notifications
You must be signed in to change notification settings - Fork 323
Expand file tree
/
Copy pathclient.go
More file actions
242 lines (223 loc) · 7.76 KB
/
Copy pathclient.go
File metadata and controls
242 lines (223 loc) · 7.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package workflowstreams
import (
"context"
"errors"
"iter"
"sync"
"time"
enumspb "go.temporal.io/api/enums/v1"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/temporal"
)
// Options configures a Client.
type Options struct {
// BatchInterval is the interval between automatic flushes. Default: 2s.
BatchInterval time.Duration
// MaxBatchSize triggers a flush once the buffer reaches this many items.
// Zero disables size-based flushing.
MaxBatchSize int
// MaxRetryDuration is the maximum time to retry a failed flush before
// returning a FlushTimeoutError. Must be less than the workflow's publisher
// TTL (default 15m) to preserve exactly-once delivery. Default: 10m.
MaxRetryDuration time.Duration
// DataConverter is used to turn published values into Payloads and is the
// converter callers should use to decode subscribed items. Default:
// converter.GetDefaultDataConverter().
DataConverter converter.DataConverter
}
// SubscribeOptions configures a subscription.
type SubscribeOptions struct {
// Topics filters the subscription. Empty or nil means all topics.
Topics []string
// FromOffset is the global offset to start from. Zero means the beginning.
FromOffset int64
// PollCooldown is the minimum interval between polls when no more items are
// immediately ready. Default: 100ms.
PollCooldown time.Duration
}
// Client publishes to and subscribes from a workflow stream from external code
// (activities, starters, other workflows). The publish path is owned by an
// internal publisher; the Client itself holds the target workflow and the read
// (subscribe/query) surface.
type Client struct {
c client.Client
workflowID string
followCAN bool
pub *publisher
mu sync.Mutex
topicHandles map[string]*TopicHandle
}
// NewClient creates a Client targeting workflowID through the given Temporal
// client. The returned Client follows continue-as-new chains in Subscribe.
func NewClient(c client.Client, workflowID string, opts Options) *Client {
dc := opts.DataConverter
if dc == nil {
dc = converter.GetDefaultDataConverter()
}
wsc := &Client{
c: c,
workflowID: workflowID,
followCAN: true,
topicHandles: map[string]*TopicHandle{},
}
wsc.pub = newPublisher(func(ctx context.Context, in PublishInput) error {
return c.SignalWorkflow(ctx, workflowID, "", PublishSignalName, in)
}, dc, opts)
return wsc
}
// NewClientFromActivity creates a Client targeting the current activity's parent
// workflow, using the activity's Temporal client. It returns an error if the
// activity has no parent workflow (a standalone activity); in that case use
// NewClient with an explicit workflow ID.
func NewClientFromActivity(ctx context.Context, opts Options) (*Client, error) {
info := activity.GetInfo(ctx)
if info.WorkflowExecution.ID == "" {
return nil, errors.New("workflowstreams: NewClientFromActivity requires an activity scheduled by a workflow; " +
"from a standalone activity use NewClient with an explicit workflow ID")
}
return NewClient(activity.GetClient(ctx), info.WorkflowExecution.ID, opts), nil
}
// Topic returns a handle for publishing to and subscribing from name. Repeated
// calls with the same name return the same handle.
func (c *Client) Topic(name string) *TopicHandle {
c.mu.Lock()
defer c.mu.Unlock()
if h, ok := c.topicHandles[name]; ok {
return h
}
h := &TopicHandle{name: name, client: c}
c.topicHandles[name] = h
return h
}
// Flush sends buffered (and pending) items and waits for server confirmation.
// It returns once the items buffered at call time have been signaled to the
// workflow and acknowledged. Returns a FlushTimeoutError if a pending batch
// cannot be sent within MaxRetryDuration.
func (c *Client) Flush(ctx context.Context) error { return c.pub.flush(ctx) }
// Close stops the background publisher and drains any remaining items. Call it
// (e.g. via defer) to guarantee a final flush. It surfaces any deferred
// FlushTimeoutError from a prior background flush failure.
func (c *Client) Close(ctx context.Context) error { return c.pub.close(ctx) }
// GetOffset queries the current global offset of the stream.
func (c *Client) GetOffset(ctx context.Context) (int64, error) {
val, err := c.c.QueryWorkflow(ctx, c.workflowID, "", OffsetQueryName)
if err != nil {
return 0, err
}
var n int64
if err := val.Get(&n); err != nil {
return 0, err
}
return n, nil
}
// Subscribe returns a range-over-func iterator that long-polls for new items.
// Iterate with:
//
// for item, err := range c.Subscribe(ctx, opts) {
// if err != nil { ... }
// // use item
// }
//
// Breaking out of the loop or cancelling ctx stops the subscription and tears
// down the poll loop. Each yielded item carries the raw *commonpb.Payload in
// Data; decode it with your data converter. The iterator ends cleanly when the
// workflow reaches a terminal state, and automatically follows continue-as-new
// chains.
func (c *Client) Subscribe(ctx context.Context, opts SubscribeOptions) iter.Seq2[WorkflowStreamItem, error] {
return func(yield func(WorkflowStreamItem, error) bool) {
pollCooldown := opts.PollCooldown
if pollCooldown <= 0 {
pollCooldown = defaultPollCooldown
}
topics := opts.Topics
if topics == nil {
topics = []string{}
}
offset := opts.FromOffset
for {
if err := ctx.Err(); err != nil {
yield(WorkflowStreamItem{}, err)
return
}
var result PollResult
handle, err := c.c.UpdateWorkflow(ctx, client.UpdateWorkflowOptions{
WorkflowID: c.workflowID,
UpdateName: PollUpdateName,
Args: []any{PollInput{Topics: topics, FromOffset: offset}},
WaitForStage: client.WorkflowUpdateStageCompleted,
})
if err == nil {
err = handle.Get(ctx, &result)
}
if err != nil {
var appErr *temporal.ApplicationError
if errors.As(err, &appErr) && appErr.Type() == ErrTypeTruncatedOffset {
// Fell behind truncation; restart from the beginning of
// whatever still exists.
offset = 0
continue
}
// The workflow may have continued-as-new or completed between
// polls. Follow the chain, exit cleanly on a terminal state,
// otherwise surface the error.
if followed := c.followContinueAsNew(ctx); followed {
continue
}
if c.isTerminal(ctx) {
return
}
yield(WorkflowStreamItem{}, err)
return
}
for _, wi := range result.Items {
payload, derr := decodePayloadWire(wi.Data)
if derr != nil {
yield(WorkflowStreamItem{}, derr)
return
}
if !yield(WorkflowStreamItem{Topic: wi.Topic, Data: payload, Offset: wi.Offset}, nil) {
return
}
}
offset = result.NextOffset
if !result.MoreReady {
select {
case <-time.After(pollCooldown):
case <-ctx.Done():
yield(WorkflowStreamItem{}, ctx.Err())
return
}
}
}
}
}
// followContinueAsNew reports whether the target workflow continued-as-new.
// Because polls use an empty run ID they always address the latest run, so the
// caller only needs to retry.
func (c *Client) followContinueAsNew(ctx context.Context) bool {
if !c.followCAN {
return false
}
desc, err := c.c.DescribeWorkflowExecution(ctx, c.workflowID, "")
if err != nil {
return false
}
return desc.GetWorkflowExecutionInfo().GetStatus() == enumspb.WORKFLOW_EXECUTION_STATUS_CONTINUED_AS_NEW
}
func (c *Client) isTerminal(ctx context.Context) bool {
desc, err := c.c.DescribeWorkflowExecution(ctx, c.workflowID, "")
if err != nil {
return false
}
switch desc.GetWorkflowExecutionInfo().GetStatus() {
case enumspb.WORKFLOW_EXECUTION_STATUS_COMPLETED,
enumspb.WORKFLOW_EXECUTION_STATUS_FAILED,
enumspb.WORKFLOW_EXECUTION_STATUS_CANCELED,
enumspb.WORKFLOW_EXECUTION_STATUS_TERMINATED,
enumspb.WORKFLOW_EXECUTION_STATUS_TIMED_OUT:
return true
}
return false
}