@@ -21,27 +21,29 @@ package messaging
21
21
import (
22
22
"context"
23
23
"errors"
24
- "os "
24
+ "fmt "
25
25
"regexp"
26
26
"strconv"
27
27
"time"
28
28
29
29
asb "github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus"
30
30
"github.com/Azure/azure-sdk-for-go/sdk/messaging/azservicebus/admin"
31
31
"github.com/google/uuid"
32
- "github.com/sirupsen/logrus"
33
32
logger "github.com/wso2/product-microgateway/adapter/pkg/loggers"
34
33
)
35
34
36
- // TODO: (erandi) when refactoring, refactor organization purge flow as well
37
- var bindingKeys = [] string { tokenRevocation , notification , stepQuotaThreshold , stepQuotaReset }
38
-
39
- const orgPurgeEnabled = "ORG_PURGE_ENABLED"
40
-
41
- // Subscription stores the meta data of a specific subscription
35
+ // Subscription stores the metadata of a specific subscription
36
+ // TopicName: the topic name of the subscription
37
+ // SubscriptionName: the name of the subscription
38
+ // ConnectionString: the connection string of the service bus
39
+ // ClientOptions: the client options for initiating the client
40
+ // ReconnectInterval: the interval to wait before reconnecting
42
41
type Subscription struct {
43
- topicName string
44
- subscriptionName string
42
+ TopicName string
43
+ SubscriptionName string
44
+ ConnectionString string
45
+ ClientOptions * asb.ClientOptions
46
+ ReconnectInterval time.Duration
45
47
}
46
48
47
49
var (
@@ -63,107 +65,76 @@ func init() {
63
65
AzureStepQuotaThresholdChannel = make (chan []byte )
64
66
AzureStepQuotaResetChannel = make (chan []byte )
65
67
AzureOrganizationPurgeChannel = make (chan []byte )
66
-
67
- // Temporarily disable reacting organization Purge
68
- orgPurgeEnabled , envParseErr := strconv .ParseBool (os .Getenv (orgPurgeEnabled ))
69
-
70
- if envParseErr == nil {
71
- if orgPurgeEnabled {
72
- bindingKeys = append (bindingKeys , organizationPurge )
73
- }
74
- }
75
68
}
76
69
77
70
// InitiateBrokerConnectionAndValidate to initiate connection and validate azure service bus constructs to
78
71
// further process
79
- func InitiateBrokerConnectionAndValidate (connectionString string , clientOptions * asb.ClientOptions , componentName string , reconnectRetryCount int ,
80
- reconnectInterval time.Duration , subscriptionIdleTimeDuration string ) ([]Subscription , error ) {
81
- subscriptionMetaDataList := make ([]Subscription , 0 )
72
+ func InitiateBrokerConnectionAndValidate (connectionString string , topic string , clientOptions * asb.ClientOptions , componentName string , reconnectRetryCount int ,
73
+ reconnectInterval time.Duration , subscriptionIdleTimeDuration string ) (* Subscription , error ) {
82
74
subProps := & admin.SubscriptionProperties {
83
75
AutoDeleteOnIdle : & subscriptionIdleTimeDuration ,
84
76
}
85
77
_ , err := asb .NewClientFromConnectionString (connectionString , clientOptions )
86
78
87
79
if err == nil {
88
- if logger .LoggerMsg .IsLevelEnabled (logrus .DebugLevel ) {
89
- logger .LoggerMsg .Debugf ("ASB client initialized for connection url: %s" , maskSharedAccessKey (connectionString ))
90
- }
80
+ logger .LoggerMsg .Debugf ("ASB client initialized for connection url: %s" , maskSharedAccessKey (connectionString ))
91
81
92
82
for j := 0 ; j < reconnectRetryCount || reconnectRetryCount == - 1 ; j ++ {
93
- err = nil
94
- subscriptionMetaDataList , err = retrieveSubscriptionMetadata (subscriptionMetaDataList ,
95
- connectionString , componentName , subProps )
83
+ sub , err := retrieveSubscriptionMetadataForTopic (connectionString , topic ,
84
+ clientOptions , componentName , subProps , reconnectInterval )
96
85
if err != nil {
97
86
logError (reconnectRetryCount , reconnectInterval , err )
98
- subscriptionMetaDataList = nil
99
87
time .Sleep (reconnectInterval )
100
88
continue
101
89
}
102
- return subscriptionMetaDataList , err
103
- }
104
- if err != nil {
105
- logger .LoggerMsg .Errorf ("%v. Retry attempted %d times." , err , reconnectRetryCount )
106
- return subscriptionMetaDataList , err
90
+ return sub , err
107
91
}
108
- } else {
109
- // any error which comes to this point is because the connection url is not up to the expected format
110
- // hence not retrying
111
- logger .LoggerMsg .Errorf ("Error occurred while trying to create ASB client using the connection url %s, err: %v" ,
112
- connectionString , err )
92
+ return nil , fmt .Errorf ("failed to create subscription for topic %s" , topic )
113
93
}
114
- return subscriptionMetaDataList , err
94
+ logger .LoggerMsg .Errorf ("Error occurred while trying to create ASB client using the connection url %s, err: %v" ,
95
+ maskSharedAccessKey (connectionString ), err )
96
+ return nil , err
115
97
}
116
98
117
- // InitiateConsumers to pass event consumption
118
- func InitiateConsumers (connectionString string , clientOptions * asb.ClientOptions , subscriptionMetaDataList []Subscription , reconnectInterval time.Duration ) {
119
- for _ , subscriptionMetaData := range subscriptionMetaDataList {
120
- go func (subscriptionMetaData Subscription ) {
121
- startBrokerConsumer (connectionString , clientOptions , subscriptionMetaData , reconnectInterval )
122
- }(subscriptionMetaData )
123
- }
99
+ // InitiateConsumer to start the broker consumer in a separate go routine
100
+ func InitiateConsumer (sub * Subscription , consumerType string ) {
101
+ go startBrokerConsumer (sub , consumerType )
124
102
}
125
103
126
- func retrieveSubscriptionMetadata (metaDataList []Subscription , connectionString string , componentName string ,
127
- opts * admin.SubscriptionProperties ) ([]Subscription , error ) {
128
- parentContext := context .Background ()
104
+ func retrieveSubscriptionMetadataForTopic (connectionString string , topicName string , clientOptions * asb.ClientOptions ,
105
+ componentName string , opts * admin.SubscriptionProperties , reconnectInterval time.Duration ) (* Subscription , error ) {
106
+ ctx , cancel := context .WithCancel (context .Background ())
107
+ defer cancel ()
129
108
adminClient , clientErr := admin .NewClientFromConnectionString (connectionString , nil )
130
109
if clientErr != nil {
131
110
logger .LoggerMsg .Errorf ("Error occurred while trying to create ASB admin client using the connection url %s" , connectionString )
132
111
return nil , clientErr
133
112
}
134
113
135
- for _ , key := range bindingKeys {
136
- var errorValue error
137
- subscriptionMetaData := Subscription {
138
- topicName : key ,
139
- subscriptionName : "" ,
140
- }
141
- // we are creating a unique subscription for each adapter starts. Unused subscriptions will be deleted after
142
- // idle for three days
143
- uniqueID := uuid .New ()
144
-
145
- // in ASB, subscription names can contain letters, numbers, periods (.), hyphens (-), and
146
- // underscores (_), up to 50 characters. Subscription names are also case-insensitive.
147
- var subscriptionName = componentName + "_" + uniqueID .String () + "_sub"
148
- var subscriptionCreationError error
149
- func () {
150
- ctx , cancel := context .WithCancel (parentContext )
151
- defer cancel ()
152
- _ , subscriptionCreationError = adminClient .CreateSubscription (ctx , key , subscriptionName , & admin.CreateSubscriptionOptions {
153
- Properties : opts ,
154
- })
155
- }()
156
- if subscriptionCreationError != nil {
157
- errorValue = errors .New ("Error occurred while trying to create subscription " + subscriptionName + " in ASB for topic name " +
158
- key + "." + subscriptionCreationError .Error ())
159
- return metaDataList , errorValue
160
- }
161
- logger .LoggerMsg .Debugf ("Subscription %s created." , subscriptionName )
162
- subscriptionMetaData .subscriptionName = subscriptionName
163
- subscriptionMetaData .topicName = key
164
- metaDataList = append (metaDataList , subscriptionMetaData )
114
+ // we are creating a unique subscription for each adapter starts. Unused subscriptions will be deleted after
115
+ // idle for three days
116
+
117
+ // in ASB, subscription names can contain letters, numbers, periods (.), hyphens (-), and
118
+ // underscores (_), up to 50 characters. Subscription names are also case-insensitive.
119
+ subscriptionName := fmt .Sprintf ("%s_%s_sub" , componentName , uuid .New ().String ())
120
+ _ , err := adminClient .CreateSubscription (ctx , topicName , subscriptionName , & admin.CreateSubscriptionOptions {
121
+ Properties : opts ,
122
+ })
123
+
124
+ if err != nil {
125
+ return nil , errors .New ("Error occurred while trying to create subscription " + subscriptionName + " in ASB for topic name " +
126
+ topicName + "." + err .Error ())
165
127
}
166
- return metaDataList , nil
128
+
129
+ logger .LoggerMsg .Debugf ("Subscription %s created." , subscriptionName )
130
+
131
+ return & Subscription {
132
+ TopicName : topicName ,
133
+ SubscriptionName : subscriptionName ,
134
+ ConnectionString : connectionString ,
135
+ ClientOptions : clientOptions ,
136
+ ReconnectInterval : reconnectInterval ,
137
+ }, nil
167
138
}
168
139
169
140
func logError (reconnectRetryCount int , reconnectInterval time.Duration , errVal error ) {
0 commit comments