-
Notifications
You must be signed in to change notification settings - Fork 587
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5156 from voxel51/improv/mask-path
improve overlay rendering performance
- Loading branch information
Showing
6 changed files
with
222 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
import { fetchWithLinearBackoff } from "./decorated-fetch"; | ||
|
||
describe("fetchWithLinearBackoff", () => { | ||
beforeEach(() => { | ||
vi.clearAllMocks(); | ||
vi.useRealTimers(); | ||
}); | ||
|
||
it("should return response when fetch succeeds on first try", async () => { | ||
const mockResponse = new Response("Success", { status: 200 }); | ||
global.fetch = vi.fn().mockResolvedValue(mockResponse); | ||
|
||
const response = await fetchWithLinearBackoff("http://fiftyone.ai"); | ||
|
||
expect(response).toBe(mockResponse); | ||
expect(global.fetch).toHaveBeenCalledTimes(1); | ||
expect(global.fetch).toHaveBeenCalledWith("http://fiftyone.ai"); | ||
}); | ||
|
||
it("should retry when fetch fails and eventually succeed", async () => { | ||
const mockResponse = new Response("Success", { status: 200 }); | ||
global.fetch = vi | ||
.fn() | ||
.mockRejectedValueOnce(new Error("Network Error")) | ||
.mockResolvedValue(mockResponse); | ||
|
||
const response = await fetchWithLinearBackoff("http://fiftyone.ai"); | ||
|
||
expect(response).toBe(mockResponse); | ||
expect(global.fetch).toHaveBeenCalledTimes(2); | ||
}); | ||
|
||
it("should throw an error after max retries when fetch fails every time", async () => { | ||
global.fetch = vi.fn().mockRejectedValue(new Error("Network Error")); | ||
|
||
await expect( | ||
fetchWithLinearBackoff("http://fiftyone.ai", 3, 10) | ||
).rejects.toThrowError(new RegExp("Max retries for fetch reached")); | ||
|
||
expect(global.fetch).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it("should throw an error when response is not ok", async () => { | ||
const mockResponse = new Response("Not Found", { status: 500 }); | ||
global.fetch = vi.fn().mockResolvedValue(mockResponse); | ||
|
||
await expect( | ||
fetchWithLinearBackoff("http://fiftyone.ai", 5, 10) | ||
).rejects.toThrow("HTTP error: 500"); | ||
|
||
expect(global.fetch).toHaveBeenCalledTimes(5); | ||
}); | ||
|
||
it("should throw an error when response is a 4xx, like 404", async () => { | ||
const mockResponse = new Response("Not Found", { status: 404 }); | ||
global.fetch = vi.fn().mockResolvedValue(mockResponse); | ||
|
||
await expect( | ||
fetchWithLinearBackoff("http://fiftyone.ai", 5, 10) | ||
).rejects.toThrow("Non-retryable HTTP error: 404"); | ||
|
||
expect(global.fetch).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it("should apply linear backoff between retries", async () => { | ||
const mockResponse = new Response("Success", { status: 200 }); | ||
global.fetch = vi | ||
.fn() | ||
.mockRejectedValueOnce(new Error("Network Error")) | ||
.mockRejectedValueOnce(new Error("Network Error")) | ||
.mockResolvedValue(mockResponse); | ||
|
||
vi.useFakeTimers(); | ||
|
||
const fetchPromise = fetchWithLinearBackoff("http://fiftyone.ai", 5, 10); | ||
|
||
// advance timers to simulate delays | ||
// after first delay | ||
await vi.advanceTimersByTimeAsync(100); | ||
// after scond delay | ||
await vi.advanceTimersByTimeAsync(200); | ||
|
||
const response = await fetchPromise; | ||
|
||
expect(response).toBe(mockResponse); | ||
expect(global.fetch).toHaveBeenCalledTimes(3); | ||
|
||
vi.useRealTimers(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const DEFAULT_MAX_RETRIES = 10; | ||
const DEFAULT_BASE_DELAY = 200; | ||
// list of HTTP status codes that are client errors (4xx) and should not be retried | ||
const NON_RETRYABLE_STATUS_CODES = [400, 401, 403, 404, 405, 422]; | ||
|
||
class NonRetryableError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = "NonRetryableError"; | ||
} | ||
} | ||
|
||
export const fetchWithLinearBackoff = async ( | ||
url: string, | ||
retries = DEFAULT_MAX_RETRIES, | ||
delay = DEFAULT_BASE_DELAY | ||
) => { | ||
for (let i = 0; i < retries; i++) { | ||
try { | ||
const response = await fetch(url); | ||
if (response.ok) { | ||
return response; | ||
} else { | ||
if (NON_RETRYABLE_STATUS_CODES.includes(response.status)) { | ||
throw new NonRetryableError( | ||
`Non-retryable HTTP error: ${response.status}` | ||
); | ||
} else { | ||
// retry on other HTTP errors (e.g., 500 Internal Server Error) | ||
throw new Error(`HTTP error: ${response.status}`); | ||
} | ||
} | ||
} catch (e) { | ||
if (e instanceof NonRetryableError) { | ||
// immediately throw | ||
throw e; | ||
} | ||
if (i < retries - 1) { | ||
await new Promise((resolve) => setTimeout(resolve, delay * (i + 1))); | ||
} else { | ||
// max retries reached | ||
throw new Error( | ||
"Max retries for fetch reached (linear backoff), error: " + e | ||
); | ||
} | ||
} | ||
} | ||
return null; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters