Skip to content

Commit

Permalink
make timeout configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
AR1011 committed Dec 30, 2023
1 parent 626186d commit 67ac31b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 8 deletions.
20 changes: 12 additions & 8 deletions cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/google/uuid"
)

var requestTimeout = time.Second

// Producer is a function that can produce an actor.Producer.
// Pretty simple, but yet powerfull tool to construct receivers
// depending on Cluster.
Expand All @@ -27,11 +25,13 @@ type Config struct {
ActivationStrategy ActivationStrategy
Engine *actor.Engine
ClusterProvider Producer
RequestTimeout time.Duration
}

type Cluster struct {
id string
region string
id string
region string
requestTimeout time.Duration

provider Producer
engine *actor.Engine
Expand Down Expand Up @@ -61,13 +61,17 @@ func New(cfg Config) (*Cluster, error) {
if len(cfg.Region) == 0 {
cfg.Region = "default"
}
if cfg.RequestTimeout == 0 {
cfg.RequestTimeout = time.Second
}
return &Cluster{
id: cfg.ID,
region: cfg.Region,
provider: cfg.ClusterProvider,
engine: cfg.Engine,
kinds: []kind{},
activationStrategy: cfg.ActivationStrategy,
requestTimeout: cfg.RequestTimeout,
}, nil
}

Expand Down Expand Up @@ -109,7 +113,7 @@ func (c *Cluster) Activate(kind string, config *ActivationConfig) *actor.PID {
id: config.ID,
region: config.Region,
}
resp, err := c.engine.Request(c.agentPID, msg, requestTimeout).Result()
resp, err := c.engine.Request(c.agentPID, msg, c.requestTimeout).Result()
if err != nil {
slog.Error("activation failed", "err", err)
return nil
Expand Down Expand Up @@ -155,7 +159,7 @@ func (c *Cluster) HasKindLocal(name string) bool {

// Members returns all the members that are part of the cluster.
func (c *Cluster) Members() []*Member {
resp, err := c.engine.Request(c.agentPID, getMembers{}, requestTimeout).Result()
resp, err := c.engine.Request(c.agentPID, getMembers{}, c.requestTimeout).Result()
if err != nil {
return []*Member{}
}
Expand All @@ -168,7 +172,7 @@ func (c *Cluster) Members() []*Member {
// HasKind returns true whether the given kind is available for activation on
// the cluster.
func (c *Cluster) HasKind(name string) bool {
resp, err := c.engine.Request(c.agentPID, getKinds{}, requestTimeout).Result()
resp, err := c.engine.Request(c.agentPID, getKinds{}, c.requestTimeout).Result()
if err != nil {
return false
}
Expand All @@ -183,7 +187,7 @@ func (c *Cluster) HasKind(name string) bool {
}

func (c *Cluster) GetActivated(id string) *actor.PID {
resp, err := c.engine.Request(c.agentPID, getActive{id: id}, requestTimeout).Result()
resp, err := c.engine.Request(c.agentPID, getActive{id: id}, c.requestTimeout).Result()
if err != nil {
return nil
}
Expand Down
2 changes: 2 additions & 0 deletions examples/cluster/member_1/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"time"

"github.com/anthdm/hollywood/actor"
"github.com/anthdm/hollywood/cluster"
Expand All @@ -22,6 +23,7 @@ func main() {
Region: "eu-west",
ClusterProvider: cluster.NewSelfManagedProvider(),
ActivationStrategy: shared.RegionBasedActivationStrategy("eu-west"),
RequestTimeout: time.Second,
})
if err != nil {
log.Fatal(err)
Expand Down
2 changes: 2 additions & 0 deletions examples/cluster/member_2/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"log"
"time"

"github.com/anthdm/hollywood/actor"
"github.com/anthdm/hollywood/cluster"
Expand All @@ -26,6 +27,7 @@ func main() {
Region: "us-west",
ClusterProvider: cluster.NewSelfManagedProvider(bootstrapAddr),
ActivationStrategy: shared.RegionBasedActivationStrategy("eu-west"),
RequestTimeout: time.Second,
})
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 67ac31b

Please sign in to comment.