Skip to content

Commit

Permalink
api.SearchLogQueryHandler thread safety (#1006)
Browse files Browse the repository at this point in the history
* use channels instead of appending in goroutines

Signed-off-by: Ceridwen Driskill <cdriskill@google.com>

* lint

Signed-off-by: Ceridwen Driskill <cdriskill@google.com>

Signed-off-by: Ceridwen Driskill <cdriskill@google.com>
  • Loading branch information
Ceridwen Driskill committed Aug 26, 2022
1 parent 3a3df56 commit 53ebe00
Showing 1 changed file with 14 additions and 4 deletions.
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.UnmarshalEntry(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

0 comments on commit 53ebe00

Please sign in to comment.