Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api.SearchLogQueryHandler thread safety #1006

Merged
merged 2 commits into from Aug 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 14 additions & 4 deletions pkg/api/entries.go
Expand Up @@ -338,7 +338,9 @@ func SearchLogQueryHandler(params entries.SearchLogQueryParams) middleware.Respo
}

code := http.StatusBadRequest
for _, e := range params.Entry.Entries() {
entries := params.Entry.Entries()
searchHashesChan := make(chan []byte, len(entries))
for _, e := range entries {
e := e // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
entry, err := types.NewEntry(e)
Expand All @@ -353,14 +355,18 @@ func SearchLogQueryHandler(params entries.SearchLogQueryParams) middleware.Respo
}
hasher := rfc6962.DefaultHasher
leafHash := hasher.HashLeaf(leaf)
searchHashes = append(searchHashes, leafHash)
searchHashesChan <- leafHash
return nil
})
}

if err := g.Wait(); err != nil {
return handleRekorAPIError(params, code, err, err.Error())
}
close(searchHashesChan)
for hash := range searchHashesChan {
searchHashes = append(searchHashes, hash)
}

searchByHashResults := make([]*trillian.GetEntryAndProofResponse, len(searchHashes))
g, _ = errgroup.WithContext(httpReqCtx)
Expand Down Expand Up @@ -399,22 +405,26 @@ func SearchLogQueryHandler(params entries.SearchLogQueryParams) middleware.Respo

if len(params.Entry.LogIndexes) > 0 {
g, _ := errgroup.WithContext(httpReqCtx)

resultPayloadChan := make(chan models.LogEntry, len(params.Entry.LogIndexes))
for _, logIndex := range params.Entry.LogIndexes {
logIndex := logIndex // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
logEntry, err := retrieveLogEntryByIndex(httpReqCtx, int(swag.Int64Value(logIndex)))
if err != nil {
return err
}
resultPayload = append(resultPayload, logEntry)
resultPayloadChan <- logEntry
return nil
})
}

if err := g.Wait(); err != nil {
return handleRekorAPIError(params, http.StatusInternalServerError, fmt.Errorf("grpc error: %w", err), trillianUnexpectedResult)
}
close(resultPayloadChan)
for result := range resultPayloadChan {
resultPayload = append(resultPayload, result)
}
}

return entries.NewSearchLogQueryOK().WithPayload(resultPayload)
Expand Down