Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Request timeout configurable #141

Merged
merged 3 commits into from
Jan 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ build:
go build -o bin/metrics examples/metrics/main.go
go build -o bin/chatserver examples/chat/server/main.go
go build -o bin/chatclient examples/chat/client/main.go
go build -o bin/cluster examples/cluster/member_1/main.go
go build -o bin/cluster examples/cluster/member_2/main.go
go build -o bin/cluster_member_1 examples/cluster/member_1/main.go
go build -o bin/cluster_member_2 examples/cluster/member_2/main.go

bench:
go run ./_bench/.
Expand Down
2 changes: 1 addition & 1 deletion cluster/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (a *Agent) activate(kind, id, region string) *actor.PID {
// Remote activation

// TODO: topology hash
resp, err := a.cluster.engine.Request(activatorPID, req, requestTimeout).Result()
resp, err := a.cluster.engine.Request(activatorPID, req, a.cluster.requestTimeout).Result()
if err != nil {
slog.Error("failed activation request", "err", err)
return nil
Expand Down
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.Millisecond * 50

// 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