github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/middleware/propagator/middlewarepropagation.go (about)

     1  package propagator
     2  
     3  import (
     4  	"github.com/asynkron/protoactor-go/actor"
     5  )
     6  
     7  type MiddlewarePropagator struct {
     8  	spawnMiddleware    []actor.SpawnMiddleware
     9  	senderMiddleware   []actor.SenderMiddleware
    10  	receiverMiddleware []actor.ReceiverMiddleware
    11  	contextDecorators  []actor.ContextDecorator
    12  }
    13  
    14  func New() *MiddlewarePropagator {
    15  	return &MiddlewarePropagator{}
    16  }
    17  
    18  func (propagator *MiddlewarePropagator) WithItselfForwarded() *MiddlewarePropagator {
    19  	return propagator.WithSpawnMiddleware(propagator.SpawnMiddleware)
    20  }
    21  
    22  func (propagator *MiddlewarePropagator) WithSpawnMiddleware(middleware ...actor.SpawnMiddleware) *MiddlewarePropagator {
    23  	propagator.spawnMiddleware = append(propagator.spawnMiddleware, middleware...)
    24  	return propagator
    25  }
    26  
    27  func (propagator *MiddlewarePropagator) WithSenderMiddleware(middleware ...actor.SenderMiddleware) *MiddlewarePropagator {
    28  	propagator.senderMiddleware = append(propagator.senderMiddleware, middleware...)
    29  	return propagator
    30  }
    31  
    32  func (propagator *MiddlewarePropagator) WithReceiverMiddleware(middleware ...actor.ReceiverMiddleware) *MiddlewarePropagator {
    33  	propagator.receiverMiddleware = append(propagator.receiverMiddleware, middleware...)
    34  	return propagator
    35  }
    36  
    37  func (propagator *MiddlewarePropagator) WithContextDecorator(decorators ...actor.ContextDecorator) *MiddlewarePropagator {
    38  	propagator.contextDecorators = append(propagator.contextDecorators, decorators...)
    39  	return propagator
    40  }
    41  
    42  func (propagator *MiddlewarePropagator) SpawnMiddleware(next actor.SpawnFunc) actor.SpawnFunc {
    43  	return func(actorSystem *actor.ActorSystem, id string, props *actor.Props, parentContext actor.SpawnerContext) (pid *actor.PID, e error) {
    44  		if propagator.spawnMiddleware != nil {
    45  			props = props.Configure(actor.WithSpawnMiddleware(propagator.spawnMiddleware...))
    46  		}
    47  		if propagator.senderMiddleware != nil {
    48  			props = props.Configure(actor.WithSenderMiddleware(propagator.senderMiddleware...))
    49  		}
    50  		if propagator.receiverMiddleware != nil {
    51  			props = props.Configure(actor.WithReceiverMiddleware(propagator.receiverMiddleware...))
    52  		}
    53  		if propagator.contextDecorators != nil {
    54  			props = props.Configure(actor.WithContextDecorator(propagator.contextDecorators...))
    55  		}
    56  		pid, err := next(actorSystem, id, props, parentContext)
    57  		return pid, err
    58  	}
    59  }