Skip to content

Commit

Permalink
feat: Optimize delete asset memory usage (#85)
Browse files Browse the repository at this point in the history
* invoke removal of cancel func after deletion from ES

* remove redundant cancel func storage

* added support for sync.Map to cancel and delete function within context

* resolved comments

---------

Co-authored-by: Sumeet Rai <[email protected]>
  • Loading branch information
sumslim and Sumeet Rai authored Sep 30, 2024
1 parent c62d999 commit f2972f1
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions core/asset/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package asset
import (
"context"
"fmt"
"sync"
"time"

"github.com/google/uuid"
Expand All @@ -20,9 +21,8 @@ type Service struct {
worker Worker
logger log.Logger
config Config
cancelFnList []func()

assetOpCounter metric.Int64Counter
cancelFnMap *sync.Map
assetOpCounter metric.Int64Counter
}

//go:generate mockery --name=Worker -r --case underscore --with-expecter --structname Worker --filename worker_mock.go --output=./mocks
Expand Down Expand Up @@ -58,15 +58,17 @@ func NewService(deps ServiceDeps) (service *Service, cancel func()) {
worker: deps.Worker,
logger: deps.Logger,
config: deps.Config,
cancelFnList: make([]func(), 0),

assetOpCounter: assetOpCounter,
cancelFnMap: new(sync.Map),
assetOpCounter: assetOpCounter,
}

return newService, func() {
for i := range newService.cancelFnList {
newService.cancelFnList[i]()
}
newService.cancelFnMap.Range(func(_, value interface{}) bool {
if cancelFn, ok := value.(func()); ok {
cancelFn()
}
return true
})
}
}

Expand Down Expand Up @@ -154,8 +156,13 @@ func (s *Service) DeleteAssets(ctx context.Context, request DeleteAssetsRequest)

if !request.DryRun && total > 0 {
newCtx, cancel := context.WithTimeout(context.Background(), s.config.DeleteAssetsTimeout)
s.cancelFnList = append(s.cancelFnList, cancel)
go s.executeDeleteAssets(newCtx, deleteSQLExpr)
cancelID := uuid.New().String()
s.cancelFnMap.Store(cancelID, cancel)
go func(id string) {
s.executeDeleteAssets(newCtx, deleteSQLExpr)
cancel()
s.cancelFnMap.Delete(id)
}(cancelID)
}

return uint32(total), nil
Expand Down

0 comments on commit f2972f1

Please sign in to comment.