github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/context.go (about) 1 package actor 2 3 import ( 4 "log/slog" 5 "time" 6 7 "github.com/asynkron/protoactor-go/ctxext" 8 ) 9 10 // Context contains contextual information for actors 11 type Context interface { 12 infoPart 13 basePart 14 messagePart 15 senderPart 16 receiverPart 17 spawnerPart 18 stopperPart 19 extensionPart 20 } 21 22 type ExtensionContext interface { 23 extensionPart 24 } 25 26 type SenderContext interface { 27 infoPart 28 senderPart 29 messagePart 30 } 31 32 type ReceiverContext interface { 33 infoPart 34 receiverPart 35 messagePart 36 extensionPart 37 } 38 39 type SpawnerContext interface { 40 infoPart 41 spawnerPart 42 } 43 44 type extensionPart interface { 45 Get(id ctxext.ContextExtensionID) ctxext.ContextExtension 46 Set(ext ctxext.ContextExtension) 47 } 48 49 type infoPart interface { 50 // Parent returns the PID for the current actors parent 51 Parent() *PID 52 53 // Self returns the PID for the current actor 54 Self() *PID 55 56 // Actor returns the actor associated with this context 57 Actor() Actor 58 59 ActorSystem() *ActorSystem 60 61 Logger() *slog.Logger 62 } 63 64 type basePart interface { 65 // ReceiveTimeout returns the current timeout 66 ReceiveTimeout() time.Duration 67 68 // Children returns a slice of the actors children 69 Children() []*PID 70 71 // Respond sends a response to the current `Sender` 72 // If the Sender is nil, the actor will panic 73 Respond(response interface{}) 74 75 // Stash stashes the current message on a stack for reprocessing when the actor restarts 76 Stash() 77 78 // Watch registers the actor as a monitor for the specified PID 79 Watch(pid *PID) 80 81 // Unwatch unregisters the actor as a monitor for the specified PID 82 Unwatch(pid *PID) 83 84 // SetReceiveTimeout sets the inactivity timeout, after which a ReceiveTimeout message will be sent to the actor. 85 // A duration of less than 1ms will disable the inactivity timer. 86 // 87 // If a message is received before the duration d, the timer will be reset. If the message conforms to 88 // the NotInfluenceReceiveTimeout interface, the timer will not be reset 89 SetReceiveTimeout(d time.Duration) 90 91 CancelReceiveTimeout() 92 93 // Forward forwards current message to the given PID 94 Forward(pid *PID) 95 96 ReenterAfter(f *Future, continuation func(res interface{}, err error)) 97 } 98 99 type messagePart interface { 100 // Message returns the current message to be processed 101 Message() interface{} 102 103 // MessageHeader returns the meta information for the currently processed message 104 MessageHeader() ReadonlyMessageHeader 105 } 106 107 type senderPart interface { 108 // Sender returns the PID of actor that sent currently processed message 109 Sender() *PID 110 111 // Send sends a message to the given PID 112 Send(pid *PID, message interface{}) 113 114 // Request sends a message to the given PID 115 Request(pid *PID, message interface{}) 116 117 // RequestWithCustomSender sends a message to the given PID and also provides a Sender PID 118 RequestWithCustomSender(pid *PID, message interface{}, sender *PID) 119 120 // RequestFuture sends a message to a given PID and returns a Future 121 RequestFuture(pid *PID, message interface{}, timeout time.Duration) *Future 122 } 123 124 type receiverPart interface { 125 Receive(envelope *MessageEnvelope) 126 } 127 128 type spawnerPart interface { 129 // Spawn starts a new child actor based on props and named with a unique id 130 Spawn(props *Props) *PID 131 132 // SpawnPrefix starts a new child actor based on props and named using a prefix followed by a unique id 133 SpawnPrefix(props *Props, prefix string) *PID 134 135 // SpawnNamed starts a new child actor based on props and named using the specified name 136 // 137 // ErrNameExists will be returned if id already exists 138 // 139 // Please do not use name sharing same pattern with system actors, for example "YourPrefix$1", "Remote$1", "future$1" 140 SpawnNamed(props *Props, id string) (*PID, error) 141 } 142 143 type stopperPart interface { 144 // Stop will stop actor immediately regardless of existing user messages in mailbox. 145 Stop(pid *PID) 146 147 // StopFuture will stop actor immediately regardless of existing user messages in mailbox, and return its future. 148 StopFuture(pid *PID) *Future 149 150 // Poison will tell actor to stop after processing current user messages in mailbox. 151 Poison(pid *PID) 152 153 // PoisonFuture will tell actor to stop after processing current user messages in mailbox, and return its future. 154 PoisonFuture(pid *PID) *Future 155 }