Skip to content

Commit

Permalink
docs: update README to include autoRenewAbortController configuration…
Browse files Browse the repository at this point in the history
… options

nodeshift#861
  • Loading branch information
WillianAgostini committed Oct 28, 2024
1 parent 3d3bd18 commit e7fea55
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,44 @@ breaker.fire(abortController.signal)
.catch(console.error);
```

### Auto Renew AbortController

The `autoRenewAbortController` option allows the automatic renewal of the `AbortController` when the circuit breaker transitions into the `halfOpen` or `closed` states. This feature ensures that the `AbortController` can be reused properly for ongoing requests without manual intervention.

```javascript
const CircuitBreaker = require('opossum');
const http = require('http');

function asyncFunctionThatCouldFail(abortSignal, x, y) {
return new Promise((resolve, reject) => {
http.get(
'http://httpbin.org/delay/10',
{ signal: abortSignal },
(res) => {
if(res.statusCode < 300) {
resolve(res.statusCode);
return;
}

reject(res.statusCode);
}
);
});
}

const abortController = new AbortController();
const options = {
autoRenewAbortController: true,
timeout: 3000, // If our function takes longer than 3 seconds, trigger a failure
};
const breaker = new CircuitBreaker(asyncFunctionThatCouldFail, options);

const signal = breaker.getSignal();
breaker.fire(signal)
.then(console.log)
.catch(console.error);
```

### Fallback

You can also provide a fallback function that will be executed in the
Expand Down

0 comments on commit e7fea55

Please sign in to comment.