Skip to content

Commit 4e6cf3d

Browse files
Merge pull request #1258 from dmitryblackwell/microservices-timeout-page
docs(microservices) add microservices-timeout page
2 parents 418642e + c164cdf commit 4e6cf3d

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

content/microservices/basics.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,3 +348,27 @@ export interface RequestContext<T = any> {
348348
```
349349

350350
The `data` property is the message payload sent by the message producer. The `pattern` property is the pattern used to identify an appropriate handler to handle the incoming message.
351+
352+
353+
### Handling timeouts
354+
355+
In distributed systems, sometimes microservices might be down or not available. To avoid infinitely long waiting, you can use Timeouts. A timeout is an incredibly useful pattern when communicating with other services. To apply timeouts to your microservice calls, you can use the `RxJS` timeout operator. If the microservice does not respond to the request within a certain time, an exception is thrown, which can be caught and handled appropriately.
356+
357+
To solve this problem you have to use [rxjs](https://github.com/ReactiveX/rxjs) package. Just use the `timeout` operator in the pipe:
358+
359+
```typescript
360+
@@filename()
361+
this.client
362+
.send<TResult, TInput>(pattern, data)
363+
.pipe(timeout(5000))
364+
.toPromise();
365+
@@switch
366+
this.client
367+
.send(pattern, data)
368+
.pipe(timeout(5000))
369+
.toPromise();
370+
```
371+
372+
> info **Hint** The `timeout` operator is imported from the `rxjs/operators` package.
373+
374+
After 5 seconds, if the microservice isn't responding, it will throw an error.

0 commit comments

Comments
 (0)