fix(GODT-2802): Ensure cpc reply does not block if never read

This commit is contained in:
Leander Beernaert
2023-07-28 14:52:46 +02:00
parent 32448063dc
commit c4b75c6f34

View File

@ -20,6 +20,7 @@ package cpc
import (
"context"
"errors"
"time"
)
var ErrInvalidReplyType = errors.New("reply type does not match")
@ -41,6 +42,12 @@ func (r *Request) Value() any {
func (r *Request) Reply(ctx context.Context, value any, err error) {
defer close(r.reply)
// There is a chance when `A` sends a request to `B` that the `A`'s context is cancelled before `B` is able to reply.
// This can cause `B` to wait forever. To avoid that situation we use a context with a deadline. This is safe
// since `A` is blocked waiting on our reply, and if the above happens, there's nothing left to do.
ctx, cancel := context.WithDeadline(ctx, time.Now().Add(10*time.Second))
defer cancel()
select {
case <-ctx.Done():
case r.reply <- reply{