-
-
Notifications
You must be signed in to change notification settings - Fork 525
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #627 from asynkron/examples
Examples
- Loading branch information
Showing
5 changed files
with
112 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module autorespond | ||
|
||
go 1.16 | ||
|
||
replace github.com/asynkron/protoactor-go => ../.. | ||
|
||
require ( | ||
github.com/asynkron/protoactor-go v0.0.0-00010101000000-000000000000 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"github.com/asynkron/protoactor-go/actor" | ||
"time" | ||
) | ||
|
||
//Auto Response in Proto.Actor is a special kind of message that can create its own response message | ||
//it is received just like any other message by the actor | ||
//but the actor context sees the AutoResponse interface and calls GetAutoReplyMessage() to get the response message | ||
//this is useful if you want to guarantee some form of Ack from an actor. without forcing the developer of the actor to | ||
//use context.Respond manually | ||
|
||
//e.g. ClusterPubSub feature uses this to Ack back to the Topic actor to let it know the message has been received | ||
|
||
type myAutoResponder struct { | ||
name string | ||
} | ||
|
||
func (m myAutoResponder) GetAutoResponse(context actor.Context) interface{} { | ||
|
||
//return some response-message | ||
//you have full access to the actor context | ||
|
||
return &myAutoResponse{ | ||
name: m.name + " " + context.Self().Id, | ||
} | ||
} | ||
|
||
type myAutoResponse struct { | ||
name string | ||
} | ||
|
||
func main() { | ||
system := actor.NewActorSystem() | ||
props := actor.PropsFromFunc(func(ctx actor.Context) {}) | ||
pid := system.Root.Spawn(props) | ||
|
||
res, _ := system.Root.RequestFuture(pid, &myAutoResponder{name: "hello"}, 10*time.Second).Result() | ||
|
||
fmt.Printf("%v", res) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module messagebatch | ||
|
||
go 1.16 | ||
|
||
replace github.com/asynkron/protoactor-go => ../.. | ||
|
||
require ( | ||
github.com/asynkron/goconsole v0.0.0-20160504192649-bfa12eebf716 | ||
github.com/asynkron/protoactor-go v0.0.0-00010101000000-000000000000 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
console "github.com/asynkron/goconsole" | ||
"github.com/asynkron/protoactor-go/actor" | ||
"strconv" | ||
) | ||
|
||
// MessageBatch is a message that is sent to the actor and unpacks its payload in the mailbox | ||
// This allows you to group messages together and send them as a single message | ||
// while processing them as individual messages | ||
// this is used by the Cluster PubSub feature to send a batch of messages and then Ack to the entire batch | ||
// In that specific case, both MessageBatch and AutoRespond are required | ||
|
||
type myMessageBatch struct { | ||
messages []interface{} | ||
} | ||
|
||
func (m myMessageBatch) GetMessages() []interface{} { | ||
return m.messages | ||
} | ||
|
||
func main() { | ||
system := actor.NewActorSystem() | ||
props := actor.PropsFromFunc(func(ctx actor.Context) { | ||
if m, ok := ctx.Message().(string); ok { | ||
fmt.Println(m) | ||
} | ||
}) | ||
pid := system.Root.Spawn(props) | ||
|
||
messages := make([]interface{}, 0) | ||
|
||
for i := 0; i < 100; i++ { | ||
messages = append(messages, "Hello"+strconv.Itoa(i)) | ||
} | ||
|
||
batch := &myMessageBatch{ | ||
messages: messages, | ||
} | ||
system.Root.Send(pid, batch) | ||
|
||
console.ReadLine() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters