mirror of
https://github.com/PeernetOfficial/core.git
synced 2026-07-16 18:37:51 +01:00
webapi: Fix bug in search job.
This commit is contained in:
@@ -26,7 +26,7 @@ func (api *WebapiInstance) dispatchSearch(input SearchRequest) (job *SearchJob)
|
||||
|
||||
go job.localSearch(api, input.Term)
|
||||
|
||||
job.RemoveDefer(job.timeout + time.Minute*10)
|
||||
api.RemoveJobDefer(job, job.timeout+time.Minute*10)
|
||||
|
||||
return job
|
||||
}
|
||||
@@ -55,10 +55,10 @@ resultLoop:
|
||||
}
|
||||
}
|
||||
|
||||
if bytes.Equal(file.NodeID, job.backend.SelfNodeID()) {
|
||||
if bytes.Equal(file.NodeID, api.backend.SelfNodeID()) {
|
||||
// Indicates data from the current user.
|
||||
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
|
||||
} else if peer := job.backend.NodelistLookup(file.NodeID); peer != nil {
|
||||
} else if peer := api.backend.NodelistLookup(file.NodeID); peer != nil {
|
||||
// add the tags 'Shared By Count' and 'Shared By GeoIP'
|
||||
file.Tags = append(file.Tags, blockchain.TagFromNumber(blockchain.TagSharedByCount, 1))
|
||||
if latitude, longitude, valid := api.Peer2GeoIP(peer); valid {
|
||||
|
||||
@@ -11,7 +11,6 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/PeernetOfficial/core"
|
||||
"github.com/PeernetOfficial/core/blockchain"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -30,9 +29,6 @@ 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
|
||||
timeout time.Duration // timeout set for all searches
|
||||
@@ -84,7 +80,7 @@ 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 (api *WebapiInstance) CreateSearchJob(Timeout time.Duration, MaxResults int, Filter SearchFilter) (job *SearchJob) {
|
||||
job = &SearchJob{backend: api.backend, api: api}
|
||||
job = &SearchJob{}
|
||||
job.Status = SearchStatusNotStarted
|
||||
job.id = uuid.New()
|
||||
job.timeout = Timeout
|
||||
@@ -320,30 +316,30 @@ func (job *SearchJob) isFileReceived(id uuid.UUID) (exists bool) {
|
||||
|
||||
// ---- job list management ----
|
||||
|
||||
// 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() {
|
||||
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()
|
||||
// RemoveJob removes the job structure from the list. Terminate should be called before. Unless the search is manually removed, it stays forever in the list.
|
||||
func (api *WebapiInstance) RemoveJob(job *SearchJob) {
|
||||
api.allJobsMutex.Lock()
|
||||
delete(api.allJobs, job.id) // delete is safe to call multiple times, so auto-removal and manual one are fine and need no syncing
|
||||
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.
|
||||
func (job *SearchJob) RemoveDefer(Duration time.Duration) {
|
||||
func (api *WebapiInstance) RemoveJobDefer(job *SearchJob, Duration time.Duration) {
|
||||
go func() {
|
||||
// for _, client := range job.clients {
|
||||
// <-client.TerminateSignal
|
||||
// }
|
||||
|
||||
<-time.After(Duration)
|
||||
job.Remove()
|
||||
api.RemoveJob(job)
|
||||
}()
|
||||
}
|
||||
|
||||
// JobLookup looks up a job. Returns nil if not found.
|
||||
func JobLookup(id uuid.UUID) (job *SearchJob) {
|
||||
job.api.allJobsMutex.RLock()
|
||||
job = job.api.allJobs[id]
|
||||
job.api.allJobsMutex.RUnlock()
|
||||
func (api *WebapiInstance) JobLookup(id uuid.UUID) (job *SearchJob) {
|
||||
api.allJobsMutex.RLock()
|
||||
job = api.allJobs[id]
|
||||
api.allJobsMutex.RUnlock()
|
||||
|
||||
return job
|
||||
}
|
||||
|
||||
@@ -98,9 +98,9 @@ func (api *WebapiInstance) apiSearch(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Terminate previous searches, if their IDs were supplied. This allows terminating the old search immediately without making a separate /search/terminate request.
|
||||
for _, terminate := range input.TerminateID {
|
||||
if job := JobLookup(terminate); job != nil {
|
||||
if job := api.JobLookup(terminate); job != nil {
|
||||
job.Terminate()
|
||||
job.Remove()
|
||||
api.RemoveJob(job)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ func (api *WebapiInstance) apiSearchResult(w http.ResponseWriter, r *http.Reques
|
||||
}
|
||||
|
||||
// find the job ID
|
||||
job := JobLookup(jobID)
|
||||
job := api.JobLookup(jobID)
|
||||
if job == nil {
|
||||
EncodeJSON(api.backend, w, r, SearchResult{Status: 2})
|
||||
return
|
||||
@@ -215,7 +215,7 @@ func (api *WebapiInstance) apiSearchResultStream(w http.ResponseWriter, r *http.
|
||||
useLimit := err == nil
|
||||
|
||||
// look up the job
|
||||
job := JobLookup(jobID)
|
||||
job := api.JobLookup(jobID)
|
||||
if job == nil {
|
||||
EncodeJSON(api.backend, w, r, SearchResult{Status: 2})
|
||||
return
|
||||
@@ -299,7 +299,7 @@ func (api *WebapiInstance) apiSearchTerminate(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
|
||||
// look up the job
|
||||
job := JobLookup(jobID)
|
||||
job := api.JobLookup(jobID)
|
||||
if job == nil {
|
||||
http.Error(w, "", http.StatusNotFound)
|
||||
return
|
||||
@@ -307,7 +307,7 @@ func (api *WebapiInstance) apiSearchTerminate(w http.ResponseWriter, r *http.Req
|
||||
|
||||
// terminate and remove it from the list
|
||||
job.Terminate()
|
||||
job.Remove()
|
||||
api.RemoveJob(job)
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -327,7 +327,7 @@ func (api *WebapiInstance) apiSearchStatistic(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
|
||||
// find the job ID
|
||||
job := JobLookup(jobID)
|
||||
job := api.JobLookup(jobID)
|
||||
if job == nil {
|
||||
EncodeJSON(api.backend, w, r, SearchStatistic{Status: 2})
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user