From: elinuxhenrik Date: Thu, 12 Jan 2023 12:28:36 +0000 (+0100) Subject: Improve locking X-Git-Tag: 1.1.0~42 X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=commitdiff_plain;h=6edc6544f698b8f5d923f3b2717ab103b7473dfe;p=nonrtric%2Fplt%2Fsme.git Improve locking Change so that unlocks are always defered. Issue-ID: NONRTRIC-814 Signed-off-by: elinuxhenrik Change-Id: Ie1801e3b6d44c716282637ca8c48f3d8f6540d71 --- diff --git a/capifcore/internal/eventservice/eventservice.go b/capifcore/internal/eventservice/eventservice.go index 21742ef..fd3cce7 100644 --- a/capifcore/internal/eventservice/eventservice.go +++ b/capifcore/internal/eventservice/eventservice.go @@ -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 { diff --git a/capifcore/internal/invokermanagement/invokermanagement.go b/capifcore/internal/invokermanagement/invokermanagement.go index 52b86cd..9025f83 100644 --- a/capifcore/internal/invokermanagement/invokermanagement.go +++ b/capifcore/internal/invokermanagement/invokermanagement.go @@ -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) } diff --git a/capifcore/internal/providermanagement/providermanagement.go b/capifcore/internal/providermanagement/providermanagement.go index f278147..0213e89 100644 --- a/capifcore/internal/providermanagement/providermanagement.go +++ b/capifcore/internal/providermanagement/providermanagement.go @@ -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()