@@ -14,14 +14,15 @@ limitations under the License.
14
14
package compstore
15
15
16
16
import (
17
- "fmt"
18
-
19
17
subapi "github.com/dapr/dapr/pkg/apis/subscriptions/v2alpha1"
20
18
runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
21
19
rtpubsub "github.com/dapr/dapr/pkg/runtime/pubsub"
20
+ "github.com/dapr/kit/logger"
22
21
"github.com/dapr/kit/ptr"
23
22
)
24
23
24
+ var log = logger .NewLogger ("dapr.runtime.compstore.subscriptions" )
25
+
25
26
type DeclarativeSubscription struct {
26
27
Comp * subapi.Subscription
27
28
* NamedSubscription
@@ -30,7 +31,8 @@ type DeclarativeSubscription struct {
30
31
type NamedSubscription struct {
31
32
// Name is the optional name of the subscription. If not set, the name of the
32
33
// component is used.
33
- Name * string
34
+ Name * string
35
+ ConnectionID rtpubsub.ConnectionID
34
36
rtpubsub.Subscription
35
37
}
36
38
@@ -40,7 +42,7 @@ type subscriptions struct {
40
42
// declarativesList used to track order of declarative subscriptions for
41
43
// processing priority.
42
44
declarativesList []string
43
- streams map [string ]* DeclarativeSubscription
45
+ streams map [string ][] * DeclarativeSubscription
44
46
}
45
47
46
48
// MetadataSubscription is a temporary wrapper for rtpubsub.Subscription to add a Type attribute to be used in GetMetadata
@@ -75,17 +77,19 @@ func (c *ComponentStore) AddDeclarativeSubscription(comp *subapi.Subscription, s
75
77
c .subscriptions .declarativesList = append (c .subscriptions .declarativesList , comp .Name )
76
78
}
77
79
78
- func (c * ComponentStore ) AddStreamSubscription (comp * subapi.Subscription ) error {
80
+ func (c * ComponentStore ) AddStreamSubscription (comp * subapi.Subscription , connectionID rtpubsub.ConnectionID ) error {
81
+ log .Warn ("Lock AddStreamSubscription" )
79
82
c .lock .Lock ()
80
- defer c . lock . Unlock ()
81
- if _ , ok := c . subscriptions . streams [ comp . Name ]; ok {
82
- return fmt . Errorf ( "streamer already subscribed to pubsub %q topic %q" , comp . Spec . Pubsubname , comp . Spec . Topic )
83
- }
83
+ defer func () {
84
+ c . lock . Unlock ()
85
+ log . Warn ( "Unlock AddStreamSubscription defer" )
86
+ }()
84
87
85
- c . subscriptions . streams [ comp . Name ] = & DeclarativeSubscription {
88
+ sub : = & DeclarativeSubscription {
86
89
Comp : comp ,
87
90
NamedSubscription : & NamedSubscription {
88
- Name : ptr .Of (comp .Name ),
91
+ Name : ptr .Of (comp .Name ),
92
+ ConnectionID : connectionID ,
89
93
Subscription : rtpubsub.Subscription {
90
94
PubsubName : comp .Spec .Pubsubname ,
91
95
Topic : comp .Spec .Topic ,
@@ -95,15 +99,28 @@ func (c *ComponentStore) AddStreamSubscription(comp *subapi.Subscription) error
95
99
},
96
100
},
97
101
}
102
+ c .subscriptions .streams [comp .Name ] = append (c .subscriptions .streams [comp .Name ], sub )
98
103
99
104
return nil
100
105
}
101
106
102
- func (c * ComponentStore ) DeleteStreamSubscription (names ... string ) {
107
+ func (c * ComponentStore ) DeleteStreamSubscription (comp * subapi.Subscription ) {
108
+ log .Warn ("Lock DeleteStreamSubscription" )
103
109
c .lock .Lock ()
104
- defer c .lock .Unlock ()
105
- for _ , name := range names {
106
- delete (c .subscriptions .streams , name )
110
+ defer func () {
111
+ c .lock .Unlock ()
112
+ log .Warn ("Unlock DeleteStreamSubscription defer" )
113
+ }()
114
+ streamingSubscriptions , ok := c .subscriptions .streams [comp .Name ]
115
+ if ok && len (streamingSubscriptions ) > 0 {
116
+ for i , sub := range streamingSubscriptions {
117
+ if sub .Comp == comp {
118
+ c .subscriptions .streams [comp .Name ] = append (c .subscriptions .streams [comp .Name ][:i ], c .subscriptions .streams [comp .Name ][i + 1 :]... )
119
+ }
120
+ }
121
+ }
122
+ if len (c .subscriptions .streams [comp .Name ]) == 0 {
123
+ delete (c .subscriptions .streams , comp .Name )
107
124
}
108
125
}
109
126
@@ -155,17 +172,12 @@ func (c *ComponentStore) ListTypedSubscriptions() []TypedSubscription {
155
172
subs = append (subs , typedSub )
156
173
}
157
174
}
158
- for i := range c .subscriptions .streams {
159
- sub := c .subscriptions .streams [i ].Subscription
160
- typedSub := TypedSubscription {
161
- Subscription : sub ,
162
- Type : runtimev1pb .PubsubSubscriptionType_STREAMING ,
163
- }
164
- key := sub .PubsubName + "||" + sub .Topic
165
- if j , ok := taken [key ]; ok {
166
- subs [j ] = typedSub
167
- } else {
168
- taken [key ] = len (subs )
175
+ for _ , streamingSubs := range c .subscriptions .streams {
176
+ for _ , sub := range streamingSubs {
177
+ typedSub := TypedSubscription {
178
+ Subscription : sub .Subscription ,
179
+ Type : runtimev1pb .PubsubSubscriptionType_STREAMING ,
180
+ }
169
181
subs = append (subs , typedSub )
170
182
}
171
183
}
@@ -215,9 +227,11 @@ func (c *ComponentStore) ListSubscriptionsStreamByPubSub(name string) []*NamedSu
215
227
defer c .lock .RUnlock ()
216
228
217
229
var subs []* NamedSubscription
218
- for _ , sub := range c .subscriptions .streams {
219
- if sub .Subscription .PubsubName == name {
220
- subs = append (subs , sub .NamedSubscription )
230
+ for _ , subscriptions := range c .subscriptions .streams {
231
+ for _ , sub := range subscriptions {
232
+ if sub .Subscription .PubsubName == name {
233
+ subs = append (subs , sub .NamedSubscription )
234
+ }
221
235
}
222
236
}
223
237
@@ -235,12 +249,14 @@ func (c *ComponentStore) GetDeclarativeSubscription(name string) (*DeclarativeSu
235
249
return nil , false
236
250
}
237
251
238
- func (c * ComponentStore ) GetStreamSubscription (key string ) (* NamedSubscription , bool ) {
252
+ func (c * ComponentStore ) GetStreamSubscription (subscription * subapi. Subscription ) (* NamedSubscription , bool ) {
239
253
c .lock .RLock ()
240
254
defer c .lock .RUnlock ()
241
- for i , sub := range c .subscriptions .streams {
242
- if sub .Comp .Name == key {
243
- return c .subscriptions .streams [i ].NamedSubscription , true
255
+ for _ , subscriptions := range c .subscriptions .streams {
256
+ for _ , sub := range subscriptions {
257
+ if sub .Comp == subscription {
258
+ return sub .NamedSubscription , true
259
+ }
244
260
}
245
261
}
246
262
return nil , false
0 commit comments