Skip to content

Commit 9e8fc26

Browse files
committed
Modernize
1 parent 2a2d39c commit 9e8fc26

File tree

13 files changed

+48
-55
lines changed

13 files changed

+48
-55
lines changed

actor.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@ type Actor interface {
2424
// to start, its type will be equal to its name
2525
// unless its changed:
2626
//
27-
// start := NewActorStart("worker")
27+
// start := NewActorStart("worker")
2828
//
2929
// Format names can also be used for more complicated
3030
// names, just remember to override the type:
3131
//
32-
// start := NewActorStart("worker-%d-group-%d", i, j)
33-
// start.Type = "worker"
34-
//
35-
func NewActorStart(name string, v ...interface{}) *ActorStart {
32+
// start := NewActorStart("worker-%d-group-%d", i, j)
33+
// start.Type = "worker"
34+
func NewActorStart(name string, v ...any) *ActorStart {
3635
fullName := name
3736
if len(v) > 0 {
3837
fullName = fmt.Sprintf(name, v...)

cfg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// Logger hides the logging function Printf behind a simple
99
// interface so libraries such as logrus can be used.
1010
type Logger interface {
11-
Printf(string, ...interface{})
11+
Printf(string, ...any)
1212
}
1313

1414
// ClientCfg where the only required argument is Namespace,

client.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"context"
66
"errors"
77
"fmt"
8+
"maps"
89
"math/rand"
910
"strconv"
1011
"strings"
@@ -32,7 +33,7 @@ import (
3233
//
3334
// Register(MyMsg{}) // Correct
3435
// Register(&MyMsg{}) // Incorrect
35-
func Register(v interface{}) error {
36+
func Register(v any) error {
3637
return codec.Register(v)
3738
}
3839

@@ -152,7 +153,7 @@ func (c *Client) Close() error {
152153
}
153154

154155
// Request a response for the given message. The context can be used to control cancelation or timeouts.
155-
func (c *Client) Request(ctx context.Context, receiver string, msg interface{}) (interface{}, error) {
156+
func (c *Client) Request(ctx context.Context, receiver string, msg any) (any, error) {
156157
if c == nil {
157158
return nil, ErrNilClient
158159
}
@@ -382,15 +383,15 @@ func (c *Client) handleGRPCErrs(ctx context.Context, err error, nsReceiver strin
382383
return false
383384
}
384385

385-
func (c *Client) logf(format string, v ...interface{}) {
386+
func (c *Client) logf(format string, v ...any) {
386387
if c.cfg.Logger != nil {
387388
c.cfg.Logger.Printf(format, v...)
388389
}
389390
}
390391

391392
// BroadcastC (broadcast) a message to all members in a Group. The context can be used to control
392393
// cancellations or timeouts
393-
func (c *Client) Broadcast(ctx context.Context, g *Group, msg interface{}) (BroadcastResult, error) {
394+
func (c *Client) Broadcast(ctx context.Context, g *Group, msg any) (BroadcastResult, error) {
394395
if c == nil {
395396
return nil, ErrNilClient
396397
}
@@ -403,7 +404,7 @@ func (c *Client) Broadcast(ctx context.Context, g *Group, msg interface{}) (Broa
403404
return c.broadcast(cont, cancel, g, msg)
404405
}
405406

406-
func (c *Client) broadcast(ctx context.Context, cancel context.CancelFunc, g *Group, msg interface{}) (BroadcastResult, error) {
407+
func (c *Client) broadcast(ctx context.Context, cancel context.CancelFunc, g *Group, msg any) (BroadcastResult, error) {
407408
res := make(BroadcastResult)
408409
receivers := g.Members()
409410

@@ -495,15 +496,13 @@ type BroadcastResult map[string]*Result
495496
// Result stores the result of a Request
496497
type Result struct {
497498
Err error
498-
Val interface{}
499+
Val any
499500
}
500501

501502
// Add combines two BroadcastResults, by overwriting previous
502503
// results if they exist
503504
func (b BroadcastResult) Add(other BroadcastResult) {
504-
for k, v := range other {
505-
b[k] = v
506-
}
505+
maps.Copy(b, other)
507506
}
508507

509508
// statName of interesting statistic to track

client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ func TestClientBroadcast(t *testing.T) {
217217

218218
// start up the actors
219219
const numActors = 2
220-
for i := 0; i < numActors; i++ {
220+
for i := range numActors {
221221
startEchoActor(fmt.Sprintf("echo-%d", i))
222222
}
223223

@@ -232,7 +232,7 @@ func TestClientBroadcast(t *testing.T) {
232232
} else if len(res) != numActors {
233233
t.Fatal("expected response")
234234
}
235-
for i := 0; i < numActors; i++ {
235+
for i := range numActors {
236236
actor := fmt.Sprintf("echo-%d", i)
237237
r, ok := res[actor]
238238
if !ok {

codec/registry.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ var (
2222

2323
var (
2424
mu = &sync.RWMutex{}
25-
registry = map[string]interface{}{}
25+
registry = map[string]any{}
2626
)
2727

2828
// Register a type for marshalling and unmarshalling.
2929
// The type must currently implement proto.Message.
30-
func Register(v interface{}) error {
30+
func Register(v any) error {
3131
mu.Lock()
3232
defer mu.Unlock()
3333

@@ -45,9 +45,9 @@ func Register(v interface{}) error {
4545
name := TypeName(v)
4646
registry[name] = v
4747
// TODO(aj) Temporary migration solution for go module upgrade
48-
if strings.HasPrefix(name, "github.com/lytics/lio/vendor/") {
48+
if after, ok0 := strings.CutPrefix(name, "github.com/lytics/lio/vendor/"); ok0 {
4949
// If we're the pre go.mod, register the other namespace
50-
otherName := strings.TrimPrefix(name, "github.com/lytics/lio/vendor/")
50+
otherName := after
5151
registry[otherName] = v
5252
} else {
5353
// If we're using go.mod, register the old namespace
@@ -59,7 +59,7 @@ func Register(v interface{}) error {
5959

6060
// Marshal the value into bytes. The function returns
6161
// the type name, the bytes, or an error.
62-
func Marshal(v interface{}) (string, []byte, error) {
62+
func Marshal(v any) (string, []byte, error) {
6363
mu.RLock()
6464
defer mu.RUnlock()
6565

@@ -78,7 +78,7 @@ func Marshal(v interface{}) (string, []byte, error) {
7878

7979
// Unmarshal the bytes into a value whos type is given,
8080
// or return an error.
81-
func Unmarshal(buf []byte, name string) (interface{}, error) {
81+
func Unmarshal(buf []byte, name string) (any, error) {
8282
mu.RLock()
8383
defer mu.RUnlock()
8484

@@ -96,7 +96,7 @@ func Unmarshal(buf []byte, name string) (interface{}, error) {
9696

9797
// TypeName of a value. This name is used in the registry
9898
// to distinguish types.
99-
func TypeName(v interface{}) string {
99+
func TypeName(v any) string {
100100
rt := reflect.TypeOf(v)
101101
pkg := rt.PkgPath()
102102
name := rt.Name()
@@ -108,12 +108,12 @@ func TypeName(v interface{}) string {
108108
return pkg + "/" + name
109109
}
110110

111-
func protoMarshal(v interface{}) ([]byte, error) {
111+
func protoMarshal(v any) ([]byte, error) {
112112
pb := v.(proto.Message)
113113
return proto.Marshal(pb)
114114
}
115115

116-
func protoUnmarshal(buf []byte, v interface{}) error {
116+
func protoUnmarshal(buf []byte, v any) error {
117117
pb := v.(proto.Message)
118118
return proto.Unmarshal(buf, pb)
119119
}

mailbox.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package grid
22

33
import (
44
"errors"
5+
"maps"
56
"sync"
67
)
78

@@ -63,9 +64,7 @@ func (r *mailboxRegistry) R() map[string]*GRPCMailbox {
6364
r.mu.RLock()
6465
defer r.mu.RUnlock()
6566
out := make(map[string]*GRPCMailbox, len(r.r))
66-
for k, v := range r.r {
67-
out[k] = v
68-
}
67+
maps.Copy(out, r.r)
6968
return out
7069
}
7170

mailbox_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ func TestMailboxRegistryGetSetDeleteSize(t *testing.T) {
9898
func TestMailboxRegistryR(t *testing.T) {
9999

100100
for _, n := range []int{0, 1, 2} {
101-
n := n
102101
t.Run(strconv.Itoa(n), func(t *testing.T) {
103102

104103
r := newMailboxRegistry()
@@ -107,7 +106,7 @@ func TestMailboxRegistryR(t *testing.T) {
107106
assert.Empty(t, ms)
108107

109108
msl := make([]*GRPCMailbox, n)
110-
for i := 0; i < n; i++ {
109+
for i := range n {
111110
name := strconv.Itoa(i)
112111
m := GRPCMailbox{name: name}
113112
msl[i] = &m
@@ -140,15 +139,13 @@ func TestMailboxRegistryConcurrent(t *testing.T) {
140139
numKeysSet := []int{1}
141140

142141
for _, numWorkers := range numWorkersSet {
143-
numWorkers := numWorkers
144142
for _, numKeys := range numKeysSet {
145-
numKeys := numKeys
146143
t.Run(fmt.Sprintf("%v-%v", numWorkers, numKeys), func(t *testing.T) {
147144

148145
r := newMailboxRegistry()
149146

150147
var wg sync.WaitGroup
151-
for i := 0; i < numKeys; i++ {
148+
for i := range numKeys {
152149
name := strconv.Itoa(i)
153150

154151
// Add methods you want to test here
@@ -169,10 +166,9 @@ func TestMailboxRegistryConcurrent(t *testing.T) {
169166
},
170167
}
171168

172-
for j := 0; j < numWorkers; j++ {
169+
for range numWorkers {
173170
wg.Add(len(ops))
174171
for _, op := range ops {
175-
op := op
176172
go func() {
177173
defer wg.Done()
178174
op()

registry/registry.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
// interface so libraries such as logrus can be used.
2020
// Copied from package grid to avoid interndependencies.
2121
type Logger interface {
22-
Printf(string, ...interface{})
22+
Printf(string, ...any)
2323
}
2424

2525
var (
@@ -543,7 +543,7 @@ func (rr *Registry) Deregister(c context.Context, key string) error {
543543
return nil
544544
}
545545

546-
func (rr *Registry) logf(format string, v ...interface{}) {
546+
func (rr *Registry) logf(format string, v ...any) {
547547
if rr.Logger != nil {
548548
rr.Logger.Printf(format, v...)
549549
}

registry/registry_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ func TestRegisterDeregisterWhileNotStarted(t *testing.T) {
278278
func TestRegisterTwiceNotAllowed(t *testing.T) {
279279
_, r, _ := bootstrap(t, start)
280280

281-
for i := 0; i < 2; i++ {
281+
for i := range 2 {
282282
ctx, cancel := timeoutContext()
283283
defer cancel()
284284
err := r.Register(ctx, "test-registration-twice-b")

request.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ var (
2121
// Request which must receive an ack or response.
2222
type Request interface {
2323
Context() context.Context
24-
Msg() interface{}
24+
Msg() any
2525
Ack() error
26-
Respond(msg interface{}) error
26+
Respond(msg any) error
2727
}
2828

2929
// newRequest state for use in the server. This actually converts
3030
// between the "context" and "golang.org/x/net/context" types of
3131
// Context so that method signatures are satisfied.
32-
func newRequest(ctx context.Context, msg interface{}) *request {
32+
func newRequest(ctx context.Context, msg any) *request {
3333
return &request{
3434
ctx: ctx,
3535
msg: msg,
@@ -40,7 +40,7 @@ func newRequest(ctx context.Context, msg interface{}) *request {
4040

4141
type request struct {
4242
mu sync.Mutex
43-
msg interface{}
43+
msg any
4444
ctx context.Context
4545
failure chan error
4646
response chan *Delivery
@@ -53,7 +53,7 @@ func (req *request) Context() context.Context {
5353
}
5454

5555
// Msg of the request.
56-
func (req *request) Msg() interface{} {
56+
func (req *request) Msg() any {
5757
return req.msg
5858
}
5959

@@ -64,7 +64,7 @@ func (req *request) Ack() error {
6464
}
6565

6666
// Respond to request with a message.
67-
func (req *request) Respond(msg interface{}) error {
67+
func (req *request) Respond(msg any) error {
6868
req.mu.Lock()
6969
defer req.mu.Unlock()
7070

0 commit comments

Comments
 (0)