Skip to content

Commit

Permalink
refactor: use the math/rand/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
Tochemey committed Jul 17, 2024
1 parent 8c10921 commit b6ff0e9
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 13 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,9 @@ Go-Akt comes shipped with the following routers:
* `Round-Robin router`: This type of router send messages to its routee in a round-robin way. For n messages sent through the router, each actor is forwarded one message.
Only routees that are alive are considered when the router receives a message.

The routers are just actors that can be spawned like any other actors. Once the given router has started it will spawn its routees.
Once a router dies all its routees die with it.

### Events Stream

To receive some system events and act on them for some particular business cases, you just need to call the actor system `Subscribe`.
Expand Down
7 changes: 2 additions & 5 deletions client/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
package client

import (
"math/rand"
"math/rand/v2"
"sync"
"time"
)

// Random helps pick a node at random
type Random struct {
locker *sync.Mutex
nodes []*Node
rnd *rand.Rand
}

var _ Balancer = (*Random)(nil)
Expand All @@ -44,7 +42,6 @@ func NewRandom() *Random {
return &Random{
nodes: make([]*Node, 0),
locker: &sync.Mutex{},
rnd: rand.New(rand.NewSource(time.Now().UTC().UnixNano())), //nolint:gosec
}
}

Expand All @@ -59,5 +56,5 @@ func (x *Random) Set(nodes ...*Node) {
func (x *Random) Next() *Node {
x.locker.Lock()
defer x.locker.Unlock()
return x.nodes[x.rnd.Intn(len(x.nodes))]
return x.nodes[rand.IntN(len(x.nodes))]
}
2 changes: 1 addition & 1 deletion router/broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (
// When a routee is not alive the router removes it from its set of routeesMap.
// When the last routee stops the router itself stops.
type Broadcaster struct {
// list of routeesMap
// list of routees
routeesMap map[string]actors.PID
routees []actors.Actor
}
Expand Down
9 changes: 3 additions & 6 deletions router/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ package router
import (
"context"
"fmt"
"math/rand"
"time"
"math/rand/v2"

"github.com/tochemey/goakt/v2/actors"
"github.com/tochemey/goakt/v2/goaktpb"
Expand All @@ -37,10 +36,9 @@ import (
// Random defines a Random router.
// The router selects a routee at random when a message is sent through the router.
type Random struct {
// list of routeeRefs
// list of routees
routeesMap map[string]actors.PID
routees []actors.Actor
rnd *rand.Rand
}

// enforce compilation error
Expand All @@ -58,7 +56,6 @@ func NewRandom(routees ...actors.Actor) *Random {

// PreStart pre-starts the actor.
func (x *Random) PreStart(context.Context) error {
x.rnd = rand.New(rand.NewSource(time.Now().UTC().UnixNano())) //nolint:gosec
return nil
}

Expand Down Expand Up @@ -120,6 +117,6 @@ func (x *Random) broadcast(ctx actors.ReceiveContext) {
ctx.Err(err)
}

routee := routees[x.rnd.Intn(len(routees))]
routee := routees[rand.IntN(len(routees))]
ctx.Tell(routee, msg)
}
1 change: 0 additions & 1 deletion router/round_robin.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
// then for n messages sent through the router, each actor is forwarded one message.
type RoundRobin struct {
// list of routees
// list of routeeRefs
routeesMap map[string]actors.PID
routees []actors.Actor
next uint32
Expand Down

0 comments on commit b6ff0e9

Please sign in to comment.