Improve locking 69/10269/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Thu, 12 Jan 2023 12:28:36 +0000 (13:28 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Thu, 12 Jan 2023 12:28:42 +0000 (13:28 +0100)
Change so that unlocks are always defered.

Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: Ie1801e3b6d44c716282637ca8c48f3d8f6540d71

capifcore/internal/eventservice/eventservice.go
capifcore/internal/invokermanagement/invokermanagement.go
capifcore/internal/providermanagement/providermanagement.go

index 21742ef..fd3cce7 100644 (file)
@@ -88,18 +88,22 @@ func (es *EventService) PostSubscriberIdSubscriptions(ctx echo.Context, subscrib
 }
 
 func (es *EventService) DeleteSubscriberIdSubscriptionsSubscriptionId(ctx echo.Context, subscriberId string, subscriptionId string) error {
-       es.lock.Lock()
 
        log.Debug(es.subscriptions)
        if _, ok := es.subscriptions[subscriptionId]; ok {
-               log.Debug("Deleting subscription", subscriptionId)
-               delete(es.subscriptions, subscriptionId)
+               es.deleteSubscription(subscriptionId)
        }
-       es.lock.Unlock()
 
        return ctx.NoContent(http.StatusNoContent)
 }
 
+func (es *EventService) deleteSubscription(subscriptionId string) {
+       log.Debug("Deleting subscription", subscriptionId)
+       es.lock.Lock()
+       defer es.lock.Unlock()
+       delete(es.subscriptions, subscriptionId)
+}
+
 func getEventSubscriptionFromRequest(ctx echo.Context) (eventsapi.EventSubscription, error) {
        var subscription eventsapi.EventSubscription
        err := ctx.Bind(&subscription)
@@ -211,8 +215,8 @@ func (es *EventService) getSubscriptionId(subscriberId string) string {
 
 func (es *EventService) addSubscription(subId string, subscription eventsapi.EventSubscription) {
        es.lock.Lock()
+       defer es.lock.Unlock()
        es.subscriptions[subId] = subscription
-       es.lock.Unlock()
 }
 
 func (es *EventService) getSubscription(subId string) *eventsapi.EventSubscription {
index 52b86cd..9025f83 100644 (file)
@@ -89,11 +89,11 @@ func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
 }
 
 func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APIList {
+       var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
+       im.lock.Lock()
+       defer im.lock.Unlock()
        invoker, ok := im.onboardedInvokers[invokerId]
        if ok {
-               var apiList invokerapi.APIList = im.publishRegister.GetAllPublishedServices()
-               im.lock.Lock()
-               defer im.lock.Unlock()
                invoker.ApiList = &apiList
                return &apiList
        }
@@ -153,15 +153,21 @@ func getOnboardingSecret(newInvoker invokerapi.APIInvokerEnrolmentDetails) *stri
 
 // Deletes an individual API Invoker.
 func (im *InvokerManager) DeleteOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
-       im.lock.Lock()
-       delete(im.onboardedInvokers, onboardingId)
-       im.lock.Unlock()
+       if _, ok := im.onboardedInvokers[onboardingId]; ok {
+               im.deleteInvoker(onboardingId)
+       }
 
        go im.sendEvent(onboardingId, eventsapi.CAPIFEventAPIINVOKEROFFBOARDED)
 
        return ctx.NoContent(http.StatusNoContent)
 }
 
+func (im *InvokerManager) deleteInvoker(onboardingId string) {
+       im.lock.Lock()
+       defer im.lock.Unlock()
+       delete(im.onboardedInvokers, onboardingId)
+}
+
 // Updates an individual API invoker details.
 func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onboardingId string) error {
        var invoker invokerapi.APIInvokerEnrolmentDetails
@@ -180,9 +186,7 @@ func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onb
        }
 
        if _, ok := im.onboardedInvokers[onboardingId]; ok {
-               im.lock.Lock()
-               im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
-               im.lock.Unlock()
+               im.updateInvoker(invoker)
        } else {
                return sendCoreError(ctx, http.StatusNotFound, "The invoker to update has not been onboarded")
        }
@@ -196,6 +200,12 @@ func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onb
        return nil
 }
 
+func (im *InvokerManager) updateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails) {
+       im.lock.Lock()
+       defer im.lock.Unlock()
+       im.onboardedInvokers[*invoker.ApiInvokerId] = invoker
+}
+
 func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardingId string) error {
        return ctx.NoContent(http.StatusNotImplemented)
 }
index f278147..0213e89 100644 (file)
@@ -126,15 +126,19 @@ func (pm *ProviderManager) DeleteRegistrationsRegistrationId(ctx echo.Context, r
 
        log.Debug(pm.onboardedProviders)
        if _, ok := pm.onboardedProviders[registrationId]; ok {
-               log.Debug("Deleting provider", registrationId)
-               pm.lock.Lock()
-               delete(pm.onboardedProviders, registrationId)
-               pm.lock.Unlock()
+               pm.deleteProvider(registrationId)
        }
 
        return ctx.NoContent(http.StatusNoContent)
 }
 
+func (pm *ProviderManager) deleteProvider(registrationId string) {
+       log.Debug("Deleting provider", registrationId)
+       pm.lock.Lock()
+       defer pm.lock.Unlock()
+       delete(pm.onboardedProviders, registrationId)
+}
+
 func (pm *ProviderManager) PutRegistrationsRegistrationId(ctx echo.Context, registrationId string) error {
        pm.lock.Lock()
        defer pm.lock.Unlock()