https://github.com/commenthol/fetchover
A fetch implementation with failover
https://github.com/commenthol/fetchover
Last synced: 5 months ago
JSON representation
A fetch implementation with failover
- Host: GitHub
- URL: https://github.com/commenthol/fetchover
- Owner: commenthol
- License: mit
- Created: 2024-06-23T08:23:20.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-06-23T08:32:16.000Z (about 2 years ago)
- Last Synced: 2025-08-29T23:27:21.269Z (10 months ago)
- Language: JavaScript
- Size: 13.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fetchover
A fetch implementation with extensible failover strategies.
Supports node and browsers.
## Usage
```js
import { fetchOver, SimpleFailover } from 'fetchover'
// define your failover servers
const servers = [
'https://fallback1.server',
'https://fallback2.server'
]
// choose a failover implemention (or create your own)
const failover = new SimpleFailover({ servers, timeout: 5e3 })
const fetch = fetchOver({ failover, strategy: 'sequential' })
// just fetch as always (with optional timeout)
const res = await fetch('https://target.server', { timeout: 10e3 })
```
Different strategies are available.
- `strategy='sequential'` (default) the "fallback" servers are called one after
another in sequence.
- `strategy='random'` is similar but the order of "fallback" servers being
called is random.
- `strategy='parallel'` all servers are called in parallel. The response of the
first to succeed is returned, the other requests are aborted.
⚠️ *Note: Take care to cope with multiple writes, e.g. using*
*optimistic-locking in your DB.*
# Failover
For customizing the behavior of the failover scenarios different `failover`
adapters are offered.
## SimpleFailover
The `SimpleFailover` mechanism will issue the request (using the same pathname
but different hostname) to different "fallback" servers in case that the
"target" server of the initial request is unavailable.
With `timeout` you can control when the next request after a failure can be
issued, effectively acting as circuit breaker here. Default is 5 seconds.
In case that only the circuit breaker functionality is needed, don't name any
fallback servers.
*type declaration*
```ts
interface SimpleFailoverOptions {
/** list of failover servers */
servers?: string[]
/**
* timeout of consecutive request after failure (circuit breaker)
* @default 5e3
*/
timeout?: number
}
class SimpleFailover implements Failover {
constructor(param: SimpleFailoverOptions);
}
```
## License
[MIT Licensed](./LICENSE)