@@ -20,6 +20,7 @@ import (
20
20
"fmt"
21
21
"net/http"
22
22
"strings"
23
+ "sync"
23
24
"time"
24
25
25
26
"go.opentelemetry.io/otel/trace"
@@ -37,6 +38,7 @@ import (
37
38
invokev1 "github.com/dapr/dapr/pkg/messaging/v1"
38
39
runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
39
40
"github.com/dapr/dapr/pkg/resiliency"
41
+ "github.com/dapr/dapr/pkg/runtime/processor/binding/input"
40
42
)
41
43
42
44
func (b * binding ) StartReadingFromBindings (ctx context.Context ) error {
@@ -54,10 +56,16 @@ func (b *binding) StartReadingFromBindings(ctx context.Context) error {
54
56
}
55
57
56
58
// Clean any previous state
57
- for _ , cancel := range b .inputCancels {
58
- cancel ()
59
+ var wg sync.WaitGroup
60
+ wg .Add (len (b .activeInputs ))
61
+ for _ , inp := range b .activeInputs {
62
+ go func (input * input.Input ) {
63
+ input .Stop ()
64
+ wg .Done ()
65
+ }(inp )
59
66
}
60
- b .inputCancels = make (map [string ]context.CancelFunc )
67
+ wg .Wait ()
68
+ clear (b .activeInputs )
61
69
62
70
comps := b .compStore .ListComponents ()
63
71
bindings := make (map [string ]componentsV1alpha1.Component )
@@ -86,31 +94,34 @@ func (b *binding) startInputBinding(comp componentsV1alpha1.Component, binding b
86
94
87
95
m := meta .Properties
88
96
89
- ctx , cancel := context .WithCancel (context .Background ())
90
97
if isBindingOfExplicitDirection (ComponentTypeInput , m ) {
91
98
isSubscribed = true
92
99
} else {
93
- var err error
100
+ ctx , cancel := context .WithTimeout (context .Background (), time .Second * 3 )
101
+ defer cancel ()
94
102
isSubscribed , err = b .isAppSubscribedToBinding (ctx , comp .Name )
95
103
if err != nil {
96
- cancel ()
97
104
return err
98
105
}
99
106
}
100
107
101
108
if ! isSubscribed {
102
109
log .Infof ("app has not subscribed to binding %s." , comp .Name )
103
- cancel ()
104
110
return nil
105
111
}
106
112
107
- if err := b .readFromBinding (ctx , comp .Name , binding ); err != nil {
113
+ input , err := input .Run (input.Options {
114
+ Name : comp .Name ,
115
+ Binding : binding ,
116
+ Handler : b .sendBindingEventToApp ,
117
+ })
118
+ if err != nil {
108
119
log .Errorf ("error reading from input binding %s: %s" , comp .Name , err )
109
- cancel ()
110
- return nil
120
+ return err
111
121
}
112
122
113
- b .inputCancels [comp .Name ] = cancel
123
+ b .activeInputs [comp .Name ] = input
124
+
114
125
return nil
115
126
}
116
127
@@ -125,10 +136,16 @@ func (b *binding) StopReadingFromBindings(forever bool) {
125
136
126
137
b .readingBindings = false
127
138
128
- for _ , cancel := range b .inputCancels {
129
- cancel ()
139
+ var wg sync.WaitGroup
140
+ wg .Add (len (b .activeInputs ))
141
+ for _ , inp := range b .activeInputs {
142
+ go func (input * input.Input ) {
143
+ input .Stop ()
144
+ wg .Done ()
145
+ }(inp )
130
146
}
131
- b .inputCancels = make (map [string ]context.CancelFunc )
147
+ wg .Wait ()
148
+ clear (b .activeInputs )
132
149
}
133
150
134
151
func (b * binding ) sendBatchOutputBindingsParallel (ctx context.Context , to []string , data []byte ) {
@@ -406,26 +423,6 @@ func (b *binding) sendBindingEventToApp(ctx context.Context, bindingName string,
406
423
return appResponseBody , nil
407
424
}
408
425
409
- func (b * binding ) readFromBinding (readCtx context.Context , name string , binding bindings.InputBinding ) error {
410
- return binding .Read (readCtx , func (ctx context.Context , resp * bindings.ReadResponse ) ([]byte , error ) {
411
- if resp == nil {
412
- return nil , nil
413
- }
414
-
415
- start := time .Now ()
416
- b , err := b .sendBindingEventToApp (ctx , name , resp .Data , resp .Metadata )
417
- elapsed := diag .ElapsedSince (start )
418
-
419
- diag .DefaultComponentMonitoring .InputBindingEvent (context .Background (), name , err == nil , elapsed )
420
-
421
- if err != nil {
422
- log .Debugf ("error from app consumer for binding [%s]: %s" , name , err )
423
- return nil , err
424
- }
425
- return b , nil
426
- })
427
- }
428
-
429
426
func (b * binding ) getSubscribedBindingsGRPC (ctx context.Context ) ([]string , error ) {
430
427
conn , err := b .grpc .GetAppClient ()
431
428
if err != nil {
0 commit comments