actorify.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Package actorify lets you transform a parallel operation into a serialized
  2. // operation via the Actor pattern[1].
  3. //
  4. // [1]: https://en.wikipedia.org/wiki/Actor_model
  5. package actorify
  6. import (
  7. "context"
  8. "errors"
  9. )
  10. func z[Z any]() Z {
  11. var z Z
  12. return z
  13. }
  14. var (
  15. // ErrActorDied is returned when the actor inbox or reply channel was closed.
  16. ErrActorDied = errors.New("actorify: the actor inbox or reply channel was closed")
  17. )
  18. // Handler is a function alias for the underlying logic the Actor should call.
  19. type Handler[Input, Output any] func(ctx context.Context, input Input) (Output, error)
  20. // Actor is a serializing wrapper that runs a function in a background goroutine.
  21. // Whenever the Call method is invoked, a message is sent to the actor's inbox and then
  22. // the callee waits for a response. Depending on how busy the actor is, this may take
  23. // a moment.
  24. type Actor[Input, Output any] struct {
  25. handler Handler[Input, Output]
  26. inbox chan *message[Input, Output]
  27. }
  28. type message[Input, Output any] struct {
  29. ctx context.Context
  30. arg Input
  31. reply chan reply[Output]
  32. }
  33. type reply[Output any] struct {
  34. output Output
  35. err error
  36. }
  37. // New constructs a new Actor and starts its background thread. Cancel the context and you cancel
  38. // the Actor.
  39. func New[Input, Output any](ctx context.Context, handler Handler[Input, Output]) *Actor[Input, Output] {
  40. result := &Actor[Input, Output]{
  41. handler: handler,
  42. inbox: make(chan *message[Input, Output], 32),
  43. }
  44. go result.handle(ctx)
  45. return result
  46. }
  47. func (a *Actor[Input, Output]) handle(ctx context.Context) {
  48. for {
  49. select {
  50. case <-ctx.Done():
  51. close(a.inbox)
  52. return
  53. case msg, ok := <-a.inbox:
  54. if !ok {
  55. if msg.reply != nil {
  56. close(msg.reply)
  57. }
  58. return
  59. }
  60. result, err := a.handler(msg.ctx, msg.arg)
  61. reply := reply[Output]{
  62. output: result,
  63. err: err,
  64. }
  65. msg.reply <- reply
  66. }
  67. }
  68. }
  69. // Call calls the Actor with a given Input and returns the handler's Output.
  70. //
  71. // This only works with unary functions by design. If you need to have more inputs, define
  72. // a struct type to use as a container.
  73. func (a *Actor[Input, Output]) Call(ctx context.Context, input Input) (Output, error) {
  74. replyCh := make(chan reply[Output])
  75. a.inbox <- &message[Input, Output]{
  76. arg: input,
  77. reply: replyCh,
  78. }
  79. select {
  80. case reply, ok := <-replyCh:
  81. if !ok {
  82. return z[Output](), ErrActorDied
  83. }
  84. return reply.output, reply.err
  85. case <-ctx.Done():
  86. return z[Output](), context.Cause(ctx)
  87. }
  88. }