Documentation ¶
Overview ¶
Package gchan contains helpers for common operations with channels. The helpers use consistent log formatting to save some boilerplate where used.
Index ¶
- func RecvC[T any](ctx context.Context, log *slog.Logger, in <-chan T, canceledDuring string) (val T, received bool)
- func RecvCLogBlocked[T any](ctx context.Context, log *slog.Logger, in <-chan T, during string, ...) (val T, received bool)
- func ReqResp[T, U any](ctx context.Context, log *slog.Logger, reqChan chan<- T, reqValue T, ...) (respVal U, ok bool)
- func SendC[T any](ctx context.Context, log *slog.Logger, out chan<- T, val T, ...) (sent bool)
- func SendCLogBlocked[T any](ctx context.Context, log *slog.Logger, out chan<- T, val T, during string, ...) (sent bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func RecvC ¶
func RecvC[T any](ctx context.Context, log *slog.Logger, in <-chan T, canceledDuring string) (val T, received bool)
RecvC selects between ctx.Done and receiving from in. If ctx is canceled before the receive from in completes, RecvC logs the message "Context canceled while " + canceledDuring, and it returns the zero value of T and reports false. Otherwise, the received value is returned and the function reports true.
func RecvCLogBlocked ¶
func RecvCLogBlocked[T any]( ctx context.Context, log *slog.Logger, in <-chan T, during string, tolerableBlockDuration time.Duration, ) (val T, received bool)
RecvCLogBlocked behaves similar to RecvC but logs if the receive is blocked longer than tolerableBlockDuration. In that case, it logs the total blocked duration when the receive eventually completes or when ctx is canceled.
If the receive completes successfully within tolerableBlockDuration, nothing is logged, matching RecvC's behavior.
This is useful for test helpers but generally should be avoided in production code.
func ReqResp ¶
func ReqResp[T, U any]( ctx context.Context, log *slog.Logger, reqChan chan<- T, reqValue T, respChan <-chan U, reqRespType string, ) (respVal U, ok bool)
ReqResp performs a blocking send of reqValue to reqChan, then waits to receive a value from respChan. If ctx is canceled during either operation, it returns the zero value of U and returns false.
This is a useful shorthand for synchronous request-responses.
func SendC ¶
func SendC[T any](ctx context.Context, log *slog.Logger, out chan<- T, val T, canceledDuring string) (sent bool)
SendC selects between ctx.Done and sending val to out. If ctx is canceled before the send to out completes, SendC logs the message "Context canceled while " + canceledDuring, and it reports false. Otherwise, val is successfully sent to out, and the function reports true.
func SendCLogBlocked ¶
func SendCLogBlocked[T any]( ctx context.Context, log *slog.Logger, out chan<- T, val T, during string, tolerableBlockDuration time.Duration, ) (sent bool)
SendCLogBlocked behaves similar to SendC but logs if the send is blocked longer than tolerableBlockDuration. In that case, it logs the total blocked duration when the send eventually completes or when ctx is canceled.
If the send completes successfully within tolerableBlockDuration, nothing is logged, matching SendC's behavior.
This is useful for test helpers but generally should be avoided in production code.
Types ¶
This section is empty.