From 5d598c13f6d1064787957b363901cd59bed50f34 Mon Sep 17 00:00:00 2001 From: Kleissner Date: Wed, 29 Dec 2021 05:47:17 +0100 Subject: [PATCH] webapi: Move search jobs into API context --- webapi/API.go | 5 +++++ webapi/Search Dispatch.go | 2 +- webapi/Search Job.go | 27 ++++++++++++--------------- 3 files changed, 18 insertions(+), 16 deletions(-) diff --git a/webapi/API.go b/webapi/API.go index 47ef627..9021380 100644 --- a/webapi/API.go +++ b/webapi/API.go @@ -29,6 +29,10 @@ type WebapiInstance struct { Router *mux.Router AllowKeyInParam []string // List of paths that accept the API key as &k= parameter + // search jobs + allJobs map[uuid.UUID]*SearchJob + allJobsMutex sync.RWMutex + // download info downloads map[uuid.UUID]*downloadInfo downloadsMutex sync.RWMutex @@ -56,6 +60,7 @@ func Start(Backend *core.Backend, ListenAddresses []string, UseSSL bool, Certifi backend: Backend, Router: mux.NewRouter(), AllowKeyInParam: []string{"/file/read", "/file/view"}, + allJobs: make(map[uuid.UUID]*SearchJob), downloads: make(map[uuid.UUID]*downloadInfo), } diff --git a/webapi/Search Dispatch.go b/webapi/Search Dispatch.go index 2b29085..c509f4b 100644 --- a/webapi/Search Dispatch.go +++ b/webapi/Search Dispatch.go @@ -19,7 +19,7 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob) Filter := input.ToSearchFilter() // create the search job - job = CreateSearchJob(api.backend, Timeout, input.MaxResults, Filter) + job = api.CreateSearchJob(Timeout, input.MaxResults, Filter) // todo: create actual search clients! job.Status = SearchStatusLive diff --git a/webapi/Search Job.go b/webapi/Search Job.go index 9301a30..a6b4e73 100644 --- a/webapi/Search Job.go +++ b/webapi/Search Job.go @@ -31,6 +31,7 @@ type SearchFilter struct { // SearchJob is a collection of search jobs type SearchJob struct { backend *core.Backend + api *WebapiInstance // input settings id uuid.UUID // The job id @@ -82,8 +83,8 @@ const ( // CreateSearchJob creates a new search job and adds it to the lookup list. // Timeout and MaxResults must be set and must not be 0. -func CreateSearchJob(Backend *core.Backend, Timeout time.Duration, MaxResults int, Filter SearchFilter) (job *SearchJob) { - job = &SearchJob{backend: Backend} +func (api *WebapiInstance) CreateSearchJob(Timeout time.Duration, MaxResults int, Filter SearchFilter) (job *SearchJob) { + job = &SearchJob{backend: api.backend, api: api} job.Status = SearchStatusNotStarted job.id = uuid.New() job.timeout = Timeout @@ -96,9 +97,9 @@ func CreateSearchJob(Backend *core.Backend, Timeout time.Duration, MaxResults in job.stats.fileFormat = make(map[uint16]int) // add to the list of jobs - allJobsMutex.Lock() - allJobs[job.id] = job - allJobsMutex.Unlock() + api.allJobsMutex.Lock() + api.allJobs[job.id] = job + api.allJobsMutex.Unlock() return } @@ -318,16 +319,12 @@ func (job *SearchJob) isFileReceived(id uuid.UUID) (exists bool) { } // ---- job list management ---- -var ( - allJobs map[uuid.UUID]*SearchJob = make(map[uuid.UUID]*SearchJob) - allJobsMutex sync.RWMutex -) // Remove removes the structure from the list. Terminate should be called before. Unless the search is manually removed, it stays forever in the list. func (job *SearchJob) Remove() { - allJobsMutex.Lock() - delete(allJobs, job.id) // delete is safe to call multiple times, so auto-removal and manual one are fine and need no syncing - allJobsMutex.Unlock() + job.api.allJobsMutex.Lock() + delete(job.api.allJobs, job.id) // delete is safe to call multiple times, so auto-removal and manual one are fine and need no syncing + job.api.allJobsMutex.Unlock() } // RemoveDefer removes the search job after a given time after all searches are terminated. This can be used for automated time delayed removal. Do not create additional search clients after deferal removing. @@ -344,9 +341,9 @@ func (job *SearchJob) RemoveDefer(Duration time.Duration) { // JobLookup looks up a job. Returns nil if not found. func JobLookup(id uuid.UUID) (job *SearchJob) { - allJobsMutex.RLock() - job = allJobs[id] - allJobsMutex.RUnlock() + job.api.allJobsMutex.RLock() + job = job.api.allJobs[id] + job.api.allJobsMutex.RUnlock() return job }