Skip to content

Commit

Permalink
more docs and changed activation config
Browse files Browse the repository at this point in the history
  • Loading branch information
anthdm committed Jan 1, 2024
1 parent f35d96e commit d14a3db
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 19 deletions.
45 changes: 34 additions & 11 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ func (config Config) WithRegion(region string) Config {
return config
}

// Cluster...
// Cluster allows you to write distributed actors. It combines Engine, Remote, and
// Provider which allows members of the cluster to send messages to eachother in a
// self discovering environment.
type Cluster struct {
config Config
engine *actor.Engine
Expand All @@ -113,6 +115,7 @@ type Cluster struct {
kinds []kind
}

// New returns a new cluster given a Config.
func New(config Config) (*Cluster, error) {
if config.engine == nil {
remote := remote.New(config.listenAddr, nil)
Expand Down Expand Up @@ -157,23 +160,42 @@ func (c *Cluster) Spawn(p actor.Producer, id string, opts ...actor.OptFunc) *act
return pid
}

// TODO: Doc this when its more usefull.
type ActivationConfig struct {
// if empty, a unique identifier will be generated.
ID string
Region string
id string
region string
}

// NewActivationConfig returns a new default config.
func NewActivationConfig() ActivationConfig {
return ActivationConfig{
id: fmt.Sprintf("%d", rand.Intn(math.MaxInt)),
region: "default",
}
}

// WithID set's the id of the actor that will be activated on the cluster.
//
// Defaults to a random identifier.
func (config ActivationConfig) WithID(id string) ActivationConfig {
config.id = id
return config
}

// WithRegion set's the region on where this actor (potentially) will be spawned
//
// Defaults to a "default".
func (config ActivationConfig) WithRegion(region string) ActivationConfig {
config.region = region
return config
}

// Activate actives the given actor kind with an optional id. If there is no id
// given, the engine will create an unique id automatically.
func (c *Cluster) Activate(kind string, config *ActivationConfig) *actor.PID {
if config == nil {
config = &ActivationConfig{}
}
func (c *Cluster) Activate(kind string, config ActivationConfig) *actor.PID {
msg := activate{
kind: kind,
id: config.ID,
region: config.Region,
id: config.id,
region: config.region,
}
resp, err := c.engine.Request(c.agentPID, msg, c.config.requestTimeout).Result()
if err != nil {
Expand Down Expand Up @@ -248,6 +270,7 @@ func (c *Cluster) HasKind(name string) bool {
return false
}

// TODO: Weird
func (c *Cluster) GetActivated(id string) *actor.PID {
resp, err := c.engine.Request(c.agentPID, getActive{id: id}, c.config.requestTimeout).Result()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func TestActivate(t *testing.T) {
if msg.Member.ID == "B" {
// Because c1 doesnt have player registered locally we can only spawned
// the player on c2
pid := c1.Activate("player", &ActivationConfig{ID: "1"})
pid := c1.Activate("player", NewActivationConfig().WithID("1"))
assert.True(t, pid.Equals(expectedPID))
}
wg.Done()
Expand Down Expand Up @@ -172,7 +172,7 @@ func TestDeactivate(t *testing.T) {
switch msg := c.Message().(type) {
case MemberJoinEvent:
if msg.Member.ID == "B" {
pid := c1.Activate("player", &ActivationConfig{ID: "1"})
pid := c1.Activate("player", NewActivationConfig().WithID("1"))
assert.True(t, pid.Equals(expectedPID))
}
case ActivationEvent:
Expand Down
12 changes: 6 additions & 6 deletions examples/cluster/member_1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ func main() {
switch msg := ctx.Message().(type) {
case cluster.MemberJoinEvent:
if msg.Member.ID == "B" {
msg := &cluster.ActivationConfig{
ID: "bob",
Region: "us-west",
}
playerPID := c.Activate("playerSession", msg)
ctx.Send(playerPID, &remote.TestMessage{Data: []byte("hello from member 1")})
config := cluster.NewActivationConfig().
WithID("bob").
WithRegion("us-west")
playerPID := c.Activate("playerSession", config)
msg := &remote.TestMessage{Data: []byte("hello from member 1")}
ctx.Send(playerPID, msg)
}
}
}, "event")
Expand Down

0 comments on commit d14a3db

Please sign in to comment.