Skip to content

Commit

Permalink
feat: Retry search with exponential backoff
Browse files Browse the repository at this point in the history
Sometimes the search might return nothing because the indexes are not
ready yet. In such case, the user will get no search result and will
have to retry.
To avoid this, we now wait for the index to be ready by making several
retries, with exponential backoff
  • Loading branch information
paultranvan committed Feb 6, 2025
1 parent 2841ddc commit 5b00b12
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@ import { getIconForSearchResult } from './getIconForSearchResult'

const log = Minilog('🔍 [useFetchResult]')

const searchWithRetry = async (
dataProxy,
searchValue,
{ maxRetries = 5, delay = 500 } = {}
) => {
let currentDelay = delay
// Make several search attemps in case it is not ready yet
for (let attempt = 0; attempt < maxRetries; attempt++) {
const searchResults = await dataProxy.search(searchValue)

if (searchResults) {
// A successful search will return an array, and null otherwise
return searchResults
}
log.info(
`Search attempt ${attempt + 1} failed, retrying in ${currentDelay} ms...`
)
await new Promise(resolve => setTimeout(resolve, currentDelay))
currentDelay *= 2 // Exponential backoff
}

log.error(`Search failed after ${maxRetries} attempts`)
return []
}

export const useFetchResult = searchValue => {
const [state, setState] = useState({
isLoading: true,
Expand All @@ -24,7 +49,7 @@ export const useFetchResult = searchValue => {

setState({ isLoading: true, results: null, searchValue })

const searchResults = await dataProxy.search(searchValue)
const searchResults = await searchWithRetry(dataProxy, searchValue)

const results = searchResults.map(r => {
// Begin Retrocompatibility code, to be removed when following PR is merged: https://github.com/cozy/cozy-web-data-proxy/pull/10
Expand Down
11 changes: 7 additions & 4 deletions packages/cozy-dataproxy-lib/src/search/SearchEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,14 @@ export class SearchEngine {
return index
}

search(query: string, options: SearchOptions | undefined): SearchResult[] {
if (!this.searchIndexes) {
// TODO: What if the indexing is running but not finished yet?
search(
query: string,
options: SearchOptions | undefined
): SearchResult[] | null {
if (!this.searchIndexes || Object.keys(this.searchIndexes).length < 1) {
// The indexing might be running but not finished yet
log.warn('[SEARCH] No search index available')
return []
return null
}

const allResults = this.searchOnIndexes(query, options?.doctypes)
Expand Down

0 comments on commit 5b00b12

Please sign in to comment.