Skip to content

Commit

Permalink
Docs 2 - passing arguments to actor creation. (#102)
Browse files Browse the repository at this point in the history
* make the links a bit nicer.
document how to pass arguments to actor creation.

closes: #96

* typo
  • Loading branch information
perbu authored Dec 9, 2023
1 parent edb94a8 commit 7b29e83
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 8 deletions.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Finally, lets send a message to the actor.
This will send a message to the actor. Hollywood will route the message to the correct actor. The actor will then print
a message to the console.

The [examples](https://github.com/anthdm/hollywood/tree/master/examples) folder is the best place to learn and
The **[examples](https://examples)** folder is the best place to learn and
explore Hollywood further.


Expand All @@ -147,6 +147,32 @@ tunable options you can provide.
e.Spawn(newFoo, "myactorname")
```

### Passing arguments to the constructor

Sometimes you'll want to pass arguments to the actor constructor. This can be done by using a closure. There is
an example of this in the [request example](examples/request). Let's look at the code.

The default constructor will look something like this:
```go
func newNameResponder() actor.Receiver {
return &nameResponder{name: "noname"}
}
```
To build a new actor with a name you can do the following:
```go
func newCustomNameResponder(name string) actor.Producer {
return func() actor.Receiver {
return &nameResponder{name}
}
}
```
You can then spawn the actor with the following code:
```go
pid := engine.Spawn(newCustomNameResponder("anthony"), "name-responder")
```



### With custom configuration
```go
e.Spawn(newFoo, "myactorname",
Expand All @@ -156,6 +182,11 @@ tunable options you can provide.
)
)
```
The options should be pretty self explanatory. You can set the maximum number of restarts, which tells the engine
how many times the given actor should be restarted in case of panic, the size of the inbox, which sets a limit on how
and unprocessed messages the inbox can hold before it will start to block, and finally you can set a list of tags.
Tags are used to filter actors when you want to send a message to a group of actors.

### As a stateless function
Actors without state can be spawned as a function, because its quick and simple.
```go
Expand Down Expand Up @@ -224,7 +255,7 @@ addr is a string with the format "host:port".
You can add custom middleware to your Receivers. This can be useful for storing metrics, saving and loading data for
your Receivers on `actor.Started` and `actor.Stopped`.

For examples on how to implement custom middleware, check out the middleware folder in the ***[examples](https://github.com/anthdm/hollywood/tree/master/examples/middleware)***
For examples on how to implement custom middleware, check out the middleware folder in the ***[examples](examples/middleware)***

## Logging

Expand Down
38 changes: 32 additions & 6 deletions examples/request/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,39 @@ import (
)

type (
nameRequest struct{}

nameRequest struct{}
nameResponse struct {
name string
}
)

type nameResponder struct{}
type nameResponder struct {
name string
}

func newNameResponder() actor.Receiver {
return &nameResponder{}

return &nameResponder{name: "noname"}
}

func newCustomNameResponder(name string) actor.Producer {
return func() actor.Receiver {
return &nameResponder{name}
}
}

func (r *nameResponder) Receive(ctx *actor.Context) {
switch ctx.Message().(type) {
case *nameRequest:
ctx.Respond(&nameResponse{name: "anthdm"})
ctx.Respond(&nameResponse{r.name})
}
}

// main is the entry point of the application.
// it creates an actor engine and spawns two new actors.
// the first actor is spawned with a default name and the second
// actor is spawned with a custom name, showing you how to pass custom
// arguments to your actors.
func main() {
e, err := actor.NewEngine()
if err != nil {
Expand All @@ -44,5 +57,18 @@ func main() {
if err != nil {
log.Fatal(err)
}
fmt.Println(res)
if name, ok := res.(*nameResponse); ok {
fmt.Println("received name:", name.name)
}

// Spawn a new responder with a custom name.
pid = e.Spawn(newCustomNameResponder("anthdm"), "custom_responder")
resp = e.Request(pid, &nameRequest{}, time.Millisecond)
res, err = resp.Result()
if err != nil {
log.Fatal(err)
}
if name, ok := res.(*nameResponse); ok {
fmt.Println("received name:", name.name)
}
}

0 comments on commit 7b29e83

Please sign in to comment.