Skip to content

Commit e479668

Browse files
committed
refactor to reuse code and match other plugins
Signed-off-by: Enrique Lacal <enrique.lacal@kaleido.io>
1 parent 94c0a36 commit e479668

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

internal/blockchain/fabric/eventstream.go

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -156,39 +156,25 @@ func (s *streamManager) getSubscriptions(ctx context.Context) (subs []*subscript
156156
return subs, nil
157157
}
158158

159-
func (s *streamManager) getSubscription(ctx context.Context, subID string) (sub *subscription, err error) {
159+
func (s *streamManager) getSubscription(ctx context.Context, subID string, okNotFound bool) (sub *subscription, err error) {
160160
res, err := s.client.R().
161161
SetContext(ctx).
162162
SetResult(&sub).
163163
Get(fmt.Sprintf("/subscriptions/%s", subID))
164164
if err != nil || !res.IsSuccess() {
165+
if okNotFound && res.StatusCode() == 404 {
166+
return nil, nil
167+
}
165168
return nil, ffresty.WrapRestErr(ctx, res, err, coremsgs.MsgFabconnectRESTErr)
166169
}
167170
return sub, nil
168171
}
169172

170-
func (s *streamManager) checkSubscriptionExistence(ctx context.Context, subID string) (found bool, err error) {
171-
sub := &subscription{}
172-
res, err := s.client.R().
173-
SetContext(ctx).
174-
SetResult(&sub).
175-
Get(fmt.Sprintf("/subscriptions/%s", subID))
176-
177-
if err != nil || !res.IsSuccess() {
178-
if res.StatusCode() == 404 {
179-
return false, nil
180-
}
181-
return false, ffresty.WrapRestErr(ctx, res, err, coremsgs.MsgFabconnectRESTErr)
182-
}
183-
184-
return true, nil
185-
}
186-
187-
func (s *streamManager) getSubscriptionName(ctx context.Context, subID string) (string, error) {
173+
func (s *streamManager) getSubscriptionName(ctx context.Context, subID string, okNotFound bool) (string, error) {
188174
if cachedValue := s.cache.GetString("sub:" + subID); cachedValue != "" {
189175
return cachedValue, nil
190176
}
191-
sub, err := s.getSubscription(ctx, subID)
177+
sub, err := s.getSubscription(ctx, subID, okNotFound)
192178
if err != nil {
193179
return "", err
194180
}

internal/blockchain/fabric/fabric.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ func (f *Fabric) buildEventLocationString(chaincode string) string {
396396

397397
func (f *Fabric) processContractEvent(ctx context.Context, events common.EventsToDispatch, msgJSON fftypes.JSONObject) (err error) {
398398
subID := msgJSON.GetString("subId")
399-
subName, err := f.streams.getSubscriptionName(ctx, subID)
399+
subName, err := f.streams.getSubscriptionName(ctx, subID, false)
400400
if err != nil {
401401
return err // this is a problem - we should be able to find the listener that dispatched this to us
402402
}
@@ -999,8 +999,12 @@ func (f *Fabric) DeleteContractListener(ctx context.Context, subscription *core.
999999
func (f *Fabric) GetContractListenerStatus(ctx context.Context, namespace, subID string, okNotFound bool) (bool, interface{}, core.ContractListenerStatus, error) {
10001000
// Fabconnect does not currently provide any additional status info for listener subscriptions.
10011001
// But we check for existence of the subscription
1002-
found, err := f.streams.checkSubscriptionExistence(ctx, subID)
1003-
return found, nil, core.ContractListenerStatusUnknown, err
1002+
sub, err := f.streams.getSubscription(ctx, subID, okNotFound)
1003+
if err != nil || sub == nil {
1004+
return false, nil, core.ContractListenerStatusUnknown, err
1005+
}
1006+
1007+
return true, nil, core.ContractListenerStatusUnknown, err
10041008
}
10051009

10061010
func (f *Fabric) GetFFIParamValidator(ctx context.Context) (fftypes.FFIParamValidator, error) {

0 commit comments

Comments
 (0)