Skip to content

Commit 43c23b8

Browse files
committed
Merge branch 'master' into non_generic
2 parents c4fd59a + 1c9aebd commit 43c23b8

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
148150
Example:
149151

150152
```go
@@ -216,6 +218,49 @@ for val := range resultChannel {
216218
fmt.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

221266
Example:

0 commit comments

Comments
 (0)