File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -145,6 +145,8 @@ fmt.Println(tempString) // tempString would be "1234"
145145
146146## Actor (inspired by Akka/Erlang)
147147
148+ ### Actor common(send/receive/spawn/states)
149+
148150Example:
149151
150152``` go
@@ -216,6 +218,49 @@ for val := range resultChannel {
216218fmt.Println (actual)
217219```
218220
221+ ### Actor Ask (inspired by Akka/Erlang)
222+
223+ ``` go
224+ actorRoot := Actor .New (func (self *ActorDef, input interface {}) {
225+ // Ask cases: ROOT
226+ switch val := input.(type ) {
227+ case *AskDef:
228+ intVal , _ := Maybe.Just (val.Message ).ToInt ()
229+
230+ // NOTE If negative, hanging for testing Ask.timeout
231+ if intVal < 0 {
232+ break
233+ }
234+
235+ val.Reply (intVal * 10 )
236+ break
237+ }
238+ })
239+
240+ // var timer *time.Timer
241+ var timeout time.Duration
242+ timeout = 10 * time.Millisecond
243+
244+ // Normal cases
245+ // Result would be 10
246+ actual, _ = Maybe .Just (Ask.New (1 , nil ).AskOnce (actorRoot)).ToInt ()
247+ // Ask with Timeout
248+ // Result would be 20
249+ actual, _ = Maybe .Just (Ask.New (2 , &timeout).AskOnce (actorRoot)).ToInt ()
250+ // Ask channel
251+ // Result would be 30
252+ ch , timer := Ask .New (3 , &timeout).AskChannel (actorRoot)
253+ actual, _ = Maybe .Just (<- *ch).ToInt ()
254+ close (*ch)
255+ if timer != nil {
256+ timer.Stop ()
257+ }
258+
259+ // Timeout cases
260+ // Result would be 0 (zero value, timeout)
261+ actual, _ = Maybe .Just (Ask.New (-1 , &timeout).AskOnce (actorRoot)).ToInt ()
262+ ```
263+
219264## Compose
220265
221266Example:
You can’t perform that action at this time.
0 commit comments