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 @@ -140,6 +140,8 @@ fmt.Println(tempString) // tempString would be "1234"
140140
141141## Actor (inspired by Akka/Erlang)
142142
143+ ### Actor common(send/receive/spawn/states)
144+
143145Example:
144146
145147``` go
@@ -211,6 +213,49 @@ for val := range resultChannel {
211213fmt.Println (actual)
212214```
213215
216+ ### Actor Ask (inspired by Akka/Erlang)
217+
218+ ``` go
219+ actorRoot := Actor .New (func (self *ActorDef, input interface {}) {
220+ // Ask cases: ROOT
221+ switch val := input.(type ) {
222+ case *AskDef:
223+ intVal , _ := Maybe.Just (val.Message ).ToInt ()
224+
225+ // NOTE If negative, hanging for testing Ask.timeout
226+ if intVal < 0 {
227+ break
228+ }
229+
230+ val.Reply (intVal * 10 )
231+ break
232+ }
233+ })
234+
235+ // var timer *time.Timer
236+ var timeout time.Duration
237+ timeout = 10 * time.Millisecond
238+
239+ // Normal cases
240+ // Result would be 10
241+ actual, _ = Maybe .Just (Ask.New (1 , nil ).AskOnce (actorRoot)).ToInt ()
242+ // Ask with Timeout
243+ // Result would be 20
244+ actual, _ = Maybe .Just (Ask.New (2 , &timeout).AskOnce (actorRoot)).ToInt ()
245+ // Ask channel
246+ // Result would be 30
247+ ch , timer := Ask .New (3 , &timeout).AskChannel (actorRoot)
248+ actual, _ = Maybe .Just (<- *ch).ToInt ()
249+ close (*ch)
250+ if timer != nil {
251+ timer.Stop ()
252+ }
253+
254+ // Timeout cases
255+ // Result would be 0 (zero value, timeout)
256+ actual, _ = Maybe .Just (Ask.New (-1 , &timeout).AskOnce (actorRoot)).ToInt ()
257+ ```
258+
214259## Compose
215260
216261Example:
You can’t perform that action at this time.
0 commit comments