Skip to content

~/labs/medix

medix

A lightweight Mediator pattern implementation with pipeline behaviors.

A Go implementation of the Mediator pattern with request/response, notifications (pub/sub) and pipeline behaviors. Requires Go 1.21 or later, for generics.

Install

go get github.com/oshturhq/medix

Features

  • Request/response: one request, one handler, dispatched with Send
  • Handler factories for a new handler instance per request
  • Notifications: one notification, many handlers, broadcast with Publish
  • Pipeline behaviors that wrap every request handler, for logging, timing or validation
  • Thread-safe registration; Send and Publish are safe for concurrent use
  • Context cancellation is respected

Example

// Register handler
medix.RegisterRequestHandler[GetUserRequest, GetUserResponse](&GetUserHandler{})

// Send request
resp, err := medix.Send[GetUserRequest, GetUserResponse](context.Background(), GetUserRequest{ID: 1})