-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflyt.go
More file actions
1384 lines (1262 loc) · 38.3 KB
/
flyt.go
File metadata and controls
1384 lines (1262 loc) · 38.3 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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package flyt is a minimalist workflow framework for Go, inspired by Pocket Flow.
// It provides a simple graph-based abstraction for orchestrating tasks.
//
// Thread Safety:
// When using concurrent batch operations, ensure that your Node implementations
// are thread-safe. The framework provides SharedStore for safe concurrent access
// to shared data.
//
// Example:
//
// // Define a simple node
// type PrintNode struct {
// *flyt.BaseNode
// }
//
// func (n *PrintNode) Exec(ctx context.Context, prepResult any) (any, error) {
// fmt.Println("Hello from node!")
// return nil, nil
// }
//
// // Create and run a flow
// node := &PrintNode{BaseNode: flyt.NewBaseNode()}
// shared := flyt.NewSharedStore()
//
// ctx := context.Background()
// action, err := flyt.Run(ctx, node, shared)
// if err != nil {
// log.Fatal(err)
// }
package flyt
import (
"context"
"encoding/json"
"fmt"
"reflect"
"sync"
"time"
)
// SharedStore provides thread-safe access to shared data across nodes in a flow.
// It acts as a key-value store that can be safely accessed by multiple goroutines
// during concurrent batch processing.
//
// Example:
//
// shared := flyt.NewSharedStore()
// shared.Set("user_id", 123)
// shared.Set("config", map[string]any{"timeout": 30})
//
// if val, ok := shared.Get("user_id"); ok {
// userID := val.(int)
// // Use userID
// }
type SharedStore struct {
mu sync.RWMutex
data map[string]any
}
// NewSharedStore creates a new thread-safe shared store.
// The store is initialized empty and ready for use.
func NewSharedStore() *SharedStore {
return &SharedStore{
data: make(map[string]any),
}
}
// Get retrieves a value from the store by key.
// Returns the value and true if the key exists, or nil and false if not found.
// This method is safe for concurrent access.
func (s *SharedStore) Get(key string) (any, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
val, ok := s.data[key]
return val, ok
}
// Set stores a value in the store with the given key.
// If the key already exists, its value is overwritten.
// This method is safe for concurrent access.
func (s *SharedStore) Set(key string, value any) {
s.mu.Lock()
defer s.mu.Unlock()
s.data[key] = value
}
// GetAll returns a copy of all data in the store.
// The returned map is a shallow copy and can be safely modified
// without affecting the store's internal data.
// This method is safe for concurrent access.
func (s *SharedStore) GetAll() map[string]any {
s.mu.RLock()
defer s.mu.RUnlock()
copy := make(map[string]any, len(s.data))
for k, v := range s.data {
copy[k] = v
}
return copy
}
// Merge merges another map into the store.
// Existing keys are overwritten with values from the provided map.
// If the provided map is nil, this method does nothing.
// This method is safe for concurrent access.
func (s *SharedStore) Merge(data map[string]any) {
if data == nil {
return
}
s.mu.Lock()
defer s.mu.Unlock()
for k, v := range data {
s.data[k] = v
}
}
// Has checks if a key exists in the store.
// This method is safe for concurrent access.
func (s *SharedStore) Has(key string) bool {
s.mu.RLock()
defer s.mu.RUnlock()
_, ok := s.data[key]
return ok
}
// Delete removes a key from the store.
// This method is safe for concurrent access.
func (s *SharedStore) Delete(key string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.data, key)
}
// Clear removes all keys from the store.
// This method is safe for concurrent access.
func (s *SharedStore) Clear() {
s.mu.Lock()
defer s.mu.Unlock()
s.data = make(map[string]any)
}
// Keys returns all keys in the store.
// The returned slice is a snapshot and can be safely modified
// without affecting the store's internal data.
// This method is safe for concurrent access.
func (s *SharedStore) Keys() []string {
s.mu.RLock()
defer s.mu.RUnlock()
keys := make([]string, 0, len(s.data))
for k := range s.data {
keys = append(keys, k)
}
return keys
}
// Len returns the number of items in the store.
// This method is safe for concurrent access.
func (s *SharedStore) Len() int {
s.mu.RLock()
defer s.mu.RUnlock()
return len(s.data)
}
// GetString retrieves a string value from the store.
// Returns empty string if the key doesn't exist or the value is not a string.
// This method is safe for concurrent access.
func (s *SharedStore) GetString(key string) string {
val, ok := s.Get(key)
if !ok {
return ""
}
str, _ := val.(string)
return str
}
// GetStringOr retrieves a string value from the store.
// Returns the provided default value if the key doesn't exist or the value is not a string.
// This method is safe for concurrent access.
func (s *SharedStore) GetStringOr(key string, defaultVal string) string {
val, ok := s.Get(key)
if !ok {
return defaultVal
}
str, ok := val.(string)
if !ok {
return defaultVal
}
return str
}
// GetInt retrieves an int value from the store.
// Returns 0 if the key doesn't exist or the value cannot be converted to int.
// Supports conversion from int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, and float types.
// This method is safe for concurrent access.
func (s *SharedStore) GetInt(key string) int {
return s.GetIntOr(key, 0)
}
// GetIntOr retrieves an int value from the store.
// Returns the provided default value if the key doesn't exist or the value cannot be converted to int.
// Supports conversion from various numeric types.
// This method is safe for concurrent access.
func (s *SharedStore) GetIntOr(key string, defaultVal int) int {
val, ok := s.Get(key)
if !ok {
return defaultVal
}
switch v := val.(type) {
case int:
return v
case int8:
return int(v)
case int16:
return int(v)
case int32:
return int(v)
case int64:
return int(v)
case uint:
return int(v)
case uint8:
return int(v)
case uint16:
return int(v)
case uint32:
return int(v)
case uint64:
return int(v)
case float32:
return int(v)
case float64:
return int(v)
default:
return defaultVal
}
}
// GetFloat64 retrieves a float64 value from the store.
// Returns 0.0 if the key doesn't exist or the value cannot be converted to float64.
// Supports conversion from int, float32, and other numeric types.
// This method is safe for concurrent access.
func (s *SharedStore) GetFloat64(key string) float64 {
return s.GetFloat64Or(key, 0.0)
}
// GetFloat64Or retrieves a float64 value from the store.
// Returns the provided default value if the key doesn't exist or the value cannot be converted to float64.
// Supports conversion from various numeric types.
// This method is safe for concurrent access.
func (s *SharedStore) GetFloat64Or(key string, defaultVal float64) float64 {
val, ok := s.Get(key)
if !ok {
return defaultVal
}
switch v := val.(type) {
case float64:
return v
case float32:
return float64(v)
case int:
return float64(v)
case int8:
return float64(v)
case int16:
return float64(v)
case int32:
return float64(v)
case int64:
return float64(v)
case uint:
return float64(v)
case uint8:
return float64(v)
case uint16:
return float64(v)
case uint32:
return float64(v)
case uint64:
return float64(v)
default:
return defaultVal
}
}
// GetBool retrieves a bool value from the store.
// Returns false if the key doesn't exist or the value is not a bool.
// This method is safe for concurrent access.
func (s *SharedStore) GetBool(key string) bool {
return s.GetBoolOr(key, false)
}
// GetBoolOr retrieves a bool value from the store.
// Returns the provided default value if the key doesn't exist or the value is not a bool.
// This method is safe for concurrent access.
func (s *SharedStore) GetBoolOr(key string, defaultVal bool) bool {
val, ok := s.Get(key)
if !ok {
return defaultVal
}
b, ok := val.(bool)
if !ok {
return defaultVal
}
return b
}
// GetSlice retrieves a []any slice from the store.
// Returns nil if the key doesn't exist or the value is not a slice.
// This method is safe for concurrent access.
func (s *SharedStore) GetSlice(key string) []any {
return s.GetSliceOr(key, nil)
}
// GetSliceOr retrieves a []any slice from the store.
// Returns the provided default value if the key doesn't exist or the value is not a slice.
// Uses ToSlice to convert various slice types to []any.
// This method is safe for concurrent access.
func (s *SharedStore) GetSliceOr(key string, defaultVal []any) []any {
val, ok := s.Get(key)
if !ok {
return defaultVal
}
// Use ToSlice for conversion which handles various slice types
if val == nil {
return defaultVal
}
// Check if it's already []any
if slice, ok := val.([]any); ok {
return slice
}
// Try to convert using reflection
result := ToSlice(val)
if len(result) == 1 && result[0] == val {
// ToSlice wrapped a non-slice value, so this wasn't actually a slice
return defaultVal
}
return result
}
// GetMap retrieves a map[string]any from the store.
// Returns nil if the key doesn't exist or the value is not a map[string]any.
// This method is safe for concurrent access.
func (s *SharedStore) GetMap(key string) map[string]any {
return s.GetMapOr(key, nil)
}
// GetMapOr retrieves a map[string]any from the store.
// Returns the provided default value if the key doesn't exist or the value is not a map[string]any.
// This method is safe for concurrent access.
func (s *SharedStore) GetMapOr(key string, defaultVal map[string]any) map[string]any {
val, ok := s.Get(key)
if !ok {
return defaultVal
}
m, ok := val.(map[string]any)
if !ok {
return defaultVal
}
return m
}
// Bind binds a value from the store to a struct using JSON marshaling/unmarshaling.
// This allows for easy conversion of complex types stored in the SharedStore.
// The destination must be a pointer to the target struct.
// Returns an error if the key is not found or binding fails.
// This method is safe for concurrent access.
//
// Example:
//
// type User struct {
// ID int `json:"id"`
// Name string `json:"name"`
// }
// var user User
// err := shared.Bind("user", &user)
func (s *SharedStore) Bind(key string, dest any) error {
val, ok := s.Get(key)
if !ok {
return fmt.Errorf("key %q not found in shared store", key)
}
// Check if dest is a pointer
rv := reflect.ValueOf(dest)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return fmt.Errorf("destination must be a non-nil pointer")
}
// If val is already the correct type, assign directly
valType := reflect.TypeOf(val)
destType := rv.Type().Elem()
if valType == destType {
rv.Elem().Set(reflect.ValueOf(val))
return nil
}
// Otherwise use JSON as intermediate format
jsonBytes, err := json.Marshal(val)
if err != nil {
return fmt.Errorf("failed to marshal value: %w", err)
}
if err := json.Unmarshal(jsonBytes, dest); err != nil {
return fmt.Errorf("failed to unmarshal to destination: %w", err)
}
return nil
}
// MustBind is like Bind but panics if binding fails.
// Use this only when binding failure should be considered a programming error.
// This method is safe for concurrent access.
//
// Example:
//
// var config Config
// shared.MustBind("config", &config) // Panics if binding fails
func (s *SharedStore) MustBind(key string, dest any) {
if err := s.Bind(key, dest); err != nil {
panic(fmt.Sprintf("SharedStore.MustBind failed: %v", err))
}
}
// Action represents the next action to take after a node executes.
// Actions are used to determine flow control in workflows, allowing
// nodes to direct execution to different paths based on their results.
//
// Example:
//
// const (
// ActionSuccess Action = "success"
// ActionRetry Action = "retry"
// ActionFail Action = "fail"
// )
type Action string
const (
// DefaultAction is the default action if none is specified.
// Flows use this when a node doesn't explicitly return an action.
DefaultAction Action = "default"
)
// Node is the interface that all nodes must implement.
//
// Important: Nodes should not be shared across concurrent flow executions.
// If you need to run the same logic concurrently, create separate node instances.
type Node interface {
// Prep reads and preprocesses data from shared store
Prep(ctx context.Context, shared *SharedStore) (any, error)
// Exec executes the main logic with optional retries
Exec(ctx context.Context, prepResult any) (any, error)
// Post processes results and writes back to shared store
Post(ctx context.Context, shared *SharedStore, prepResult, execResult any) (Action, error)
}
// BaseNode provides a base implementation of the Node interface.
// It includes common functionality like retry configuration and default
// implementations of Prep, Exec, and Post methods that can be overridden.
//
// BaseNode is designed to be embedded in custom node implementations:
//
// type MyNode struct {
// *flyt.BaseNode
// // custom fields
// }
//
// func (n *MyNode) Exec(ctx context.Context, prepResult any) (any, error) {
// // custom implementation
// }
type BaseNode struct {
mu sync.RWMutex
maxRetries int
wait time.Duration
// Batch configuration
batchConcurrency int // 0 = sequential, >0 = concurrent with limit
batchErrorHandling string // "stop", "continue"
}
// NewBaseNode creates a new BaseNode with the provided options.
// By default, maxRetries is set to 1 (no retries) and wait is 0.
//
// Example:
//
// node := flyt.NewBaseNode(
// flyt.WithMaxRetries(3),
// flyt.WithWait(time.Second),
// )
func NewBaseNode(opts ...NodeOption) *BaseNode {
n := &BaseNode{
maxRetries: 1,
wait: 0,
}
for _, opt := range opts {
opt(n)
}
return n
}
// NodeOption is a function that configures a BaseNode.
// Options can be passed to NewBaseNode to customize its behavior.
type NodeOption func(*BaseNode)
// WithMaxRetries sets the maximum number of retries for the Exec phase.
// The default is 1 (no retries). Setting this to a value greater than 1
// enables automatic retry on Exec failures.
//
// Example:
//
// node := flyt.NewBaseNode(flyt.WithMaxRetries(3))
func WithMaxRetries(retries int) NodeOption {
return func(n *BaseNode) {
n.maxRetries = retries
}
}
// WithWait sets the wait duration between retries.
// This only applies when maxRetries is greater than 1.
// The default is 0 (no wait between retries).
//
// Example:
//
// node := flyt.NewBaseNode(
// flyt.WithMaxRetries(3),
// flyt.WithWait(time.Second * 2),
// )
func WithWait(wait time.Duration) NodeOption {
return func(n *BaseNode) {
n.wait = wait
}
}
// WithBatchConcurrency sets the concurrency level for batch processing.
// 0 means sequential processing, >0 means concurrent with the specified limit.
//
// Example:
//
// node := flyt.NewBatchNode(flyt.WithBatchConcurrency(10))
func WithBatchConcurrency(n int) NodeOption {
return func(node *BaseNode) {
node.batchConcurrency = n
}
}
// WithBatchErrorHandling sets the error handling strategy for batch processing.
// If continueOnError is true, processing continues even if some items fail.
// If false, processing stops on the first error.
//
// Example:
//
// node := flyt.NewBatchNode(flyt.WithBatchErrorHandling(true))
func WithBatchErrorHandling(continueOnError bool) NodeOption {
return func(node *BaseNode) {
if continueOnError {
node.batchErrorHandling = "continue"
} else {
node.batchErrorHandling = "stop"
}
}
}
// GetMaxRetries returns the maximum number of retries configured for this node.
// This method is thread-safe.
func (n *BaseNode) GetMaxRetries() int {
n.mu.RLock()
defer n.mu.RUnlock()
return n.maxRetries
}
// GetWait returns the wait duration between retries configured for this node.
// This method is thread-safe.
func (n *BaseNode) GetWait() time.Duration {
n.mu.RLock()
defer n.mu.RUnlock()
return n.wait
}
// GetBatchConcurrency returns the batch concurrency level configured for this node.
// This method is thread-safe.
func (n *BaseNode) GetBatchConcurrency() int {
n.mu.RLock()
defer n.mu.RUnlock()
return n.batchConcurrency
}
// GetBatchErrorHandling returns the batch error handling strategy configured for this node.
// This method is thread-safe.
func (n *BaseNode) GetBatchErrorHandling() string {
n.mu.RLock()
defer n.mu.RUnlock()
if n.batchErrorHandling == "" {
return "continue" // default
}
return n.batchErrorHandling
}
// Prep is the default prep implementation that returns nil.
// Override this method in your node implementation to read and preprocess
// data from the SharedStore before execution.
func (n *BaseNode) Prep(ctx context.Context, shared *SharedStore) (any, error) {
return nil, nil
}
// Exec is the default exec implementation that returns nil.
// This method should be overridden in your node implementation to provide
// the main processing logic.
func (n *BaseNode) Exec(ctx context.Context, prepResult any) (any, error) {
return nil, nil
}
// Post is the default post implementation that returns DefaultAction.
// Override this method in your node implementation to process results
// and determine the next action in the flow.
func (n *BaseNode) Post(ctx context.Context, shared *SharedStore, prepResult, execResult any) (Action, error) {
return DefaultAction, nil
}
// ExecFallback handles errors after all retries are exhausted.
// The default implementation simply returns the error.
// Override this method to provide custom fallback behavior.
func (n *BaseNode) ExecFallback(prepResult any, err error) (any, error) {
return nil, err
}
// RetryableNode is a node that supports automatic retries on Exec failures.
// Nodes implementing this interface can specify retry behavior through
// GetMaxRetries and GetWait methods.
type RetryableNode interface {
Node
GetMaxRetries() int
GetWait() time.Duration
}
// FallbackNode is a node that supports custom fallback behavior on error.
// When all retries are exhausted, the ExecFallback method is called to
// provide an alternative result or handle the error gracefully.
type FallbackNode interface {
ExecFallback(prepResult any, err error) (any, error)
}
// Run executes a node with the standard prep->exec->post lifecycle.
// This is the main entry point for executing individual nodes.
//
// The execution flow is:
// 1. Prep: Read and preprocess data from SharedStore
// 2. Exec: Execute main logic with automatic retries if configured
// 3. Post: Process results and write back to SharedStore
//
// If the node implements RetryableNode, the Exec phase will automatically
// retry on failure according to the configured settings.
//
// If the node implements FallbackNode and all retries fail, ExecFallback
// is called to provide alternative handling.
//
// Parameters:
// - ctx: Context for cancellation and timeouts
// - node: The node to execute
// - shared: SharedStore for data exchange
//
// Returns:
// - Action: The next action to take in the flow
// - error: Any error that occurred during execution
//
// Example:
//
// node := &MyNode{BaseNode: flyt.NewBaseNode()}
// shared := flyt.NewSharedStore()
// shared.Set("input", "data")
//
// action, err := flyt.Run(ctx, node, shared)
// if err != nil {
// log.Fatal(err)
// }
func Run(ctx context.Context, node Node, shared *SharedStore) (Action, error) {
// Check if this is a BatchNode
if _, ok := node.(*BatchNode); ok {
return runBatch(ctx, node, shared)
}
if batchBuilder, ok := node.(*BatchNodeBuilder); ok {
return runBatch(ctx, batchBuilder.BatchNode, shared)
}
// Check context before each phase
if err := ctx.Err(); err != nil {
return "", fmt.Errorf("run: context cancelled: %w", err)
}
// Prep phase
prepResult, err := node.Prep(ctx, shared)
if err != nil {
return "", fmt.Errorf("run: prep failed: %w", err)
}
// Check context again
if err := ctx.Err(); err != nil {
return "", fmt.Errorf("run: context cancelled after prep: %w", err)
}
// Get retry settings if available
var maxRetries = 1
var wait time.Duration = 0
if retryable, ok := node.(RetryableNode); ok {
maxRetries = retryable.GetMaxRetries()
wait = retryable.GetWait()
}
// Exec phase with retries
var execResult any
var execErr error
for attempt := 0; attempt < maxRetries; attempt++ {
// Check context before retry
if err := ctx.Err(); err != nil {
return "", fmt.Errorf("run: context cancelled during retry: %w", err)
}
if attempt > 0 && wait > 0 {
select {
case <-time.After(wait):
// Continue with retry
case <-ctx.Done():
return "", fmt.Errorf("run: context cancelled during wait: %w", ctx.Err())
}
}
execResult, execErr = node.Exec(ctx, prepResult)
if execErr == nil {
break
}
}
// Handle exec failure
if execErr != nil {
if fallback, ok := node.(FallbackNode); ok {
execResult, execErr = fallback.ExecFallback(prepResult, execErr)
}
if execErr != nil {
return "", fmt.Errorf("run: exec failed after %d retries: %w", maxRetries, execErr)
}
}
// Post phase
action, err := node.Post(ctx, shared, prepResult, execResult)
if err != nil {
return "", fmt.Errorf("run: post failed: %w", err)
}
if action == "" {
action = DefaultAction
}
return action, nil
}
// Flow represents a workflow of connected nodes.
// A Flow is itself a Node, allowing flows to be nested within other flows.
// Nodes are connected via actions, creating a directed graph of execution.
//
// Example:
//
// // Create nodes
// validateNode := &ValidateNode{BaseNode: flyt.NewBaseNode()}
// processNode := &ProcessNode{BaseNode: flyt.NewBaseNode()}
// errorNode := &ErrorNode{BaseNode: flyt.NewBaseNode()}
//
// // Create flow
// flow := flyt.NewFlow(validateNode)
// flow.Connect(validateNode, ActionSuccess, processNode)
// flow.Connect(validateNode, ActionFail, errorNode)
//
// // Or with chaining:
// flow.Connect(validateNode, ActionSuccess, processNode)
// .Connect(validateNode, ActionFail, errorNode)
//
// // Run flow
// err := flow.Run(ctx, shared)
type Flow struct {
*BaseNode
start Node
transitions map[Node]map[Action]Node
}
// NewFlow creates a new Flow with a start node.
// The flow begins execution at the start node and follows
// transitions based on the actions returned by each node.
//
// Parameters:
// - start: The first node to execute in the flow
//
// Example:
//
// flow := flyt.NewFlow(startNode)
func NewFlow(start Node) *Flow {
return &Flow{
BaseNode: NewBaseNode(),
start: start,
transitions: make(map[Node]map[Action]Node),
}
}
// Connect adds a transition from one node to another based on an action.
// When the 'from' node returns the specified action, execution continues
// with the 'to' node. Multiple actions can be connected from a single node.
// Returns the flow instance for method chaining.
//
// Parameters:
// - from: The source node
// - action: The action that triggers this transition
// - to: The destination node
//
// Example:
//
// flow.Connect(nodeA, "success", nodeB)
// flow.Connect(nodeA, "retry", nodeA) // Self-loop for retry
// flow.Connect(nodeA, "fail", errorNode)
//
// Example with chaining:
//
// flow.Connect(nodeA, "success", nodeB)
// .Connect(nodeB, "success", finalNode)
// .Connect(nodeB, "fail", errorNode)
func (f *Flow) Connect(from Node, action Action, to Node) *Flow {
if f.transitions[from] == nil {
f.transitions[from] = make(map[Action]Node)
}
f.transitions[from][action] = to
return f
}
// Run executes the flow starting from the start node.
// This is a convenience method that wraps the standard Run function.
// The flow executes nodes in sequence based on their action transitions
// until no more transitions are available or an error occurs.
//
// Parameters:
// - ctx: Context for cancellation and timeouts
// - shared: SharedStore for data exchange between nodes
//
// Returns:
// - error: Any error that occurred during flow execution
func (f *Flow) Run(ctx context.Context, shared *SharedStore) error {
// Use the standard Run function to execute this flow as a node
_, err := Run(ctx, f, shared)
return err
}
// Prep implements Node interface for Flow
func (f *Flow) Prep(ctx context.Context, shared *SharedStore) (any, error) {
// Pass the shared store to Exec
return shared, nil
}
// Exec orchestrates the flow execution by running nodes in sequence
func (f *Flow) Exec(ctx context.Context, prepResult any) (any, error) {
// Extract shared store from prepResult
shared, ok := prepResult.(*SharedStore)
if !ok {
return nil, fmt.Errorf("flow: exec failed: invalid prepResult type %T, expected *SharedStore", prepResult)
}
if f.start == nil {
return nil, fmt.Errorf("flow: exec failed: no start node configured")
}
current := f.start
var lastAction Action
for current != nil {
// Check context
if err := ctx.Err(); err != nil {
return nil, fmt.Errorf("flow: exec cancelled: %w", err)
}
// Run the current node using the standard Run function
action, err := Run(ctx, current, shared)
if err != nil {
return nil, err
}
lastAction = action
// Find next node based on action
if transitions, ok := f.transitions[current]; ok {
if next, ok := transitions[action]; ok {
current = next
} else {
// No transition for this action, flow ends
break
}
} else {
// No transitions defined for this node, flow ends
break
}
}
// Return the last action as the result
return lastAction, nil
}
// Post implements Node interface for Flow
func (f *Flow) Post(ctx context.Context, shared *SharedStore, prepResult, execResult any) (Action, error) {
// Return the last action from the flow execution
if action, ok := execResult.(Action); ok {
return action, nil
}
return DefaultAction, nil
}
// WorkerPool manages concurrent task execution with a fixed number of workers.
// It provides a simple way to limit concurrency and execute tasks in parallel.
// WorkerPool is used internally by batch processing nodes but can also be
// used directly for custom concurrent operations.
//
// Example:
//
// pool := flyt.NewWorkerPool(5)
// defer pool.Close()
//
// for _, item := range items {
// item := item // capture loop variable
// pool.Submit(func() {
// // Process item
// })
// }
//
// pool.Wait()
type WorkerPool struct {
workers int
tasks chan func()
wg sync.WaitGroup
done chan struct{}
}
// NewWorkerPool creates a new worker pool with the specified number of workers.
// If workers is less than or equal to 0, it defaults to 1.
// The pool starts workers immediately and is ready to accept tasks.
//
// Parameters:
// - workers: Number of concurrent workers
//
// Returns:
// - *WorkerPool: A new worker pool ready for use
//
// Remember to call Close() when done to clean up resources.
func NewWorkerPool(workers int) *WorkerPool {
if workers <= 0 {
workers = 1
}
p := &WorkerPool{
workers: workers,
tasks: make(chan func(), workers*2),
done: make(chan struct{}),
}
// Start workers
for i := 0; i < workers; i++ {
go p.worker()
}
return p
}
func (p *WorkerPool) worker() {
for {
select {
case task, ok := <-p.tasks:
if !ok {
return
}
task()
case <-p.done:
return
}
}
}
// Submit submits a task to the pool for execution.
// The task will be executed by one of the available workers.
// This method blocks if all workers are busy and the task buffer is full.
//
// Parameters:
// - task: Function to execute
func (p *WorkerPool) Submit(task func()) {
p.wg.Add(1)
p.tasks <- func() {
defer p.wg.Done()
task()
}
}
// Wait waits for all submitted tasks to complete.