chore(lint): modernize Go (#7536)

Use modern Go constructs through the modernize analyzer from the
golang.org/x/tools package.

Signed-off-by: Ville Vesilehto <ville@vesilehto.fi>
This commit is contained in:
Ville Vesilehto
2025-09-10 23:08:27 +03:00
committed by GitHub
parent afdd41a266
commit 39abf5aeba
48 changed files with 150 additions and 193 deletions

View File

@@ -22,7 +22,7 @@ type Cache struct {
// shard is a cache with random eviction.
type shard struct {
items map[uint64]interface{}
items map[uint64]any
size int
sync.RWMutex
@@ -30,10 +30,7 @@ type shard struct {
// New returns a new cache.
func New(size int) *Cache {
ssize := size / shardSize
if ssize < 4 {
ssize = 4
}
ssize := max(size/shardSize, 4)
c := &Cache{}
@@ -46,13 +43,13 @@ func New(size int) *Cache {
// Add adds a new element to the cache. If the element already exists it is overwritten.
// Returns true if an existing element was evicted to make room for this element.
func (c *Cache) Add(key uint64, el interface{}) bool {
func (c *Cache) Add(key uint64, el any) bool {
shard := key & (shardSize - 1)
return c.shards[shard].Add(key, el)
}
// Get looks up element index under key.
func (c *Cache) Get(key uint64) (interface{}, bool) {
func (c *Cache) Get(key uint64) (any, bool) {
shard := key & (shardSize - 1)
return c.shards[shard].Get(key)
}
@@ -73,18 +70,18 @@ func (c *Cache) Len() int {
}
// Walk walks each shard in the cache.
func (c *Cache) Walk(f func(map[uint64]interface{}, uint64) bool) {
func (c *Cache) Walk(f func(map[uint64]any, uint64) bool) {
for _, s := range &c.shards {
s.Walk(f)
}
}
// newShard returns a new shard with size.
func newShard(size int) *shard { return &shard{items: make(map[uint64]interface{}), size: size} }
func newShard(size int) *shard { return &shard{items: make(map[uint64]any), size: size} }
// Add adds element indexed by key into the cache. Any existing element is overwritten
// Returns true if an existing element was evicted to make room for this element.
func (s *shard) Add(key uint64, el interface{}) bool {
func (s *shard) Add(key uint64, el any) bool {
eviction := false
s.Lock()
if len(s.items) >= s.size {
@@ -119,7 +116,7 @@ func (s *shard) Evict() {
}
// Get looks up the element indexed under key.
func (s *shard) Get(key uint64) (interface{}, bool) {
func (s *shard) Get(key uint64) (any, bool) {
s.RLock()
el, found := s.items[key]
s.RUnlock()
@@ -135,7 +132,7 @@ func (s *shard) Len() int {
}
// Walk walks the shard for each element the function f is executed while holding a write lock.
func (s *shard) Walk(f func(map[uint64]interface{}, uint64) bool) {
func (s *shard) Walk(f func(map[uint64]any, uint64) bool) {
s.RLock()
items := make([]uint64, len(s.items))
i := 0

View File

@@ -63,7 +63,7 @@ func TestCacheWalk(t *testing.T) {
exp[i] = 1
}
got := make([]int, 10*2)
c.Walk(func(items map[uint64]interface{}, key uint64) bool {
c.Walk(func(items map[uint64]any, key uint64) bool {
got[key] = items[key].(int)
return true
})
@@ -78,7 +78,7 @@ func BenchmarkCache(b *testing.B) {
b.ReportAllocs()
c := New(4)
for range b.N {
for b.Loop() {
c.Add(1, 1)
c.Get(1)
}

View File

@@ -62,8 +62,7 @@ func BenchmarkMinimalTTL(b *testing.B) {
utc := time.Now().UTC()
mt, _ := response.Typify(m, utc)
b.ResetTimer()
for range b.N {
for b.Loop() {
dur := MinimalTTL(m, mt)
if dur != 1000*time.Second {
b.Fatalf("Wrong MinimalTTL %d, expected %d", dur, 1000*time.Second)

View File

@@ -10,8 +10,8 @@ import (
)
// DefaultEnv returns the default set of custom state variables and functions available to for use in expression evaluation.
func DefaultEnv(ctx context.Context, state *request.Request) map[string]interface{} {
return map[string]interface{}{
func DefaultEnv(ctx context.Context, state *request.Request) map[string]any {
return map[string]any{
"incidr": func(ipStr, cidrStr string) (bool, error) {
ip := net.ParseIP(ipStr)
if ip == nil {

View File

@@ -9,16 +9,16 @@ import (
// A usage example is, the external plugin k8s_event will replicate log prints to Kubernetes events.
type Listener interface {
Name() string
Debug(plugin string, v ...interface{})
Debugf(plugin string, format string, v ...interface{})
Info(plugin string, v ...interface{})
Infof(plugin string, format string, v ...interface{})
Warning(plugin string, v ...interface{})
Warningf(plugin string, format string, v ...interface{})
Error(plugin string, v ...interface{})
Errorf(plugin string, format string, v ...interface{})
Fatal(plugin string, v ...interface{})
Fatalf(plugin string, format string, v ...interface{})
Debug(plugin string, v ...any)
Debugf(plugin string, format string, v ...any)
Info(plugin string, v ...any)
Infof(plugin string, format string, v ...any)
Warning(plugin string, v ...any)
Warningf(plugin string, format string, v ...any)
Error(plugin string, v ...any)
Errorf(plugin string, format string, v ...any)
Fatal(plugin string, v ...any)
Fatalf(plugin string, format string, v ...any)
}
type listeners struct {
@@ -60,7 +60,7 @@ func DeregisterListener(old Listener) error {
return nil
}
func (ls *listeners) debug(plugin string, v ...interface{}) {
func (ls *listeners) debug(plugin string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Debug(plugin, v...)
@@ -68,7 +68,7 @@ func (ls *listeners) debug(plugin string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) debugf(plugin string, format string, v ...interface{}) {
func (ls *listeners) debugf(plugin string, format string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Debugf(plugin, format, v...)
@@ -76,7 +76,7 @@ func (ls *listeners) debugf(plugin string, format string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) info(plugin string, v ...interface{}) {
func (ls *listeners) info(plugin string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Info(plugin, v...)
@@ -84,7 +84,7 @@ func (ls *listeners) info(plugin string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) infof(plugin string, format string, v ...interface{}) {
func (ls *listeners) infof(plugin string, format string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Infof(plugin, format, v...)
@@ -92,7 +92,7 @@ func (ls *listeners) infof(plugin string, format string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) warning(plugin string, v ...interface{}) {
func (ls *listeners) warning(plugin string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Warning(plugin, v...)
@@ -100,7 +100,7 @@ func (ls *listeners) warning(plugin string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) warningf(plugin string, format string, v ...interface{}) {
func (ls *listeners) warningf(plugin string, format string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Warningf(plugin, format, v...)
@@ -108,7 +108,7 @@ func (ls *listeners) warningf(plugin string, format string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) error(plugin string, v ...interface{}) {
func (ls *listeners) error(plugin string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Error(plugin, v...)
@@ -116,7 +116,7 @@ func (ls *listeners) error(plugin string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) errorf(plugin string, format string, v ...interface{}) {
func (ls *listeners) errorf(plugin string, format string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Errorf(plugin, format, v...)
@@ -124,7 +124,7 @@ func (ls *listeners) errorf(plugin string, format string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) fatal(plugin string, v ...interface{}) {
func (ls *listeners) fatal(plugin string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Fatal(plugin, v...)
@@ -132,7 +132,7 @@ func (ls *listeners) fatal(plugin string, v ...interface{}) {
ls.RUnlock()
}
func (ls *listeners) fatalf(plugin string, format string, v ...interface{}) {
func (ls *listeners) fatalf(plugin string, format string, v ...any) {
ls.RLock()
for _, l := range ls.listeners {
l.Fatalf(plugin, format, v...)

View File

@@ -80,42 +80,42 @@ func (l *mockListener) Name() string {
return l.name
}
func (l *mockListener) Debug(plugin string, v ...interface{}) {
func (l *mockListener) Debug(plugin string, v ...any) {
log(debug, l.name+" mocked debug")
}
func (l *mockListener) Debugf(plugin string, format string, v ...interface{}) {
func (l *mockListener) Debugf(plugin string, format string, v ...any) {
log(debug, l.name+" mocked debug")
}
func (l *mockListener) Info(plugin string, v ...interface{}) {
func (l *mockListener) Info(plugin string, v ...any) {
log(info, l.name+" mocked info")
}
func (l *mockListener) Infof(plugin string, format string, v ...interface{}) {
func (l *mockListener) Infof(plugin string, format string, v ...any) {
log(info, l.name+" mocked info")
}
func (l *mockListener) Warning(plugin string, v ...interface{}) {
func (l *mockListener) Warning(plugin string, v ...any) {
log(warning, l.name+" mocked warning")
}
func (l *mockListener) Warningf(plugin string, format string, v ...interface{}) {
func (l *mockListener) Warningf(plugin string, format string, v ...any) {
log(warning, l.name+" mocked warning")
}
func (l *mockListener) Error(plugin string, v ...interface{}) {
func (l *mockListener) Error(plugin string, v ...any) {
log(err, l.name+" mocked error")
}
func (l *mockListener) Errorf(plugin string, format string, v ...interface{}) {
func (l *mockListener) Errorf(plugin string, format string, v ...any) {
log(err, l.name+" mocked error")
}
func (l *mockListener) Fatal(plugin string, v ...interface{}) {
func (l *mockListener) Fatal(plugin string, v ...any) {
log(fatal, l.name+" mocked fatal")
}
func (l *mockListener) Fatalf(plugin string, format string, v ...interface{}) {
func (l *mockListener) Fatalf(plugin string, format string, v ...any) {
log(fatal, l.name+" mocked fatal")
}

View File

@@ -40,18 +40,18 @@ func (d *d) Value() bool {
}
// logf calls log.Printf prefixed with level.
func logf(level, format string, v ...interface{}) {
func logf(level, format string, v ...any) {
golog.Print(level, fmt.Sprintf(format, v...))
}
// log calls log.Print prefixed with level.
func log(level string, v ...interface{}) {
func log(level string, v ...any) {
golog.Print(level, fmt.Sprint(v...))
}
// Debug is equivalent to log.Print(), but prefixed with "[DEBUG] ". It only outputs something
// if D is true.
func Debug(v ...interface{}) {
func Debug(v ...any) {
if !D.Value() {
return
}
@@ -60,7 +60,7 @@ func Debug(v ...interface{}) {
// Debugf is equivalent to log.Printf(), but prefixed with "[DEBUG] ". It only outputs something
// if D is true.
func Debugf(format string, v ...interface{}) {
func Debugf(format string, v ...any) {
if !D.Value() {
return
}
@@ -68,30 +68,30 @@ func Debugf(format string, v ...interface{}) {
}
// Info is equivalent to log.Print, but prefixed with "[INFO] ".
func Info(v ...interface{}) { log(info, v...) }
func Info(v ...any) { log(info, v...) }
// Infof is equivalent to log.Printf, but prefixed with "[INFO] ".
func Infof(format string, v ...interface{}) { logf(info, format, v...) }
func Infof(format string, v ...any) { logf(info, format, v...) }
// Warning is equivalent to log.Print, but prefixed with "[WARNING] ".
func Warning(v ...interface{}) { log(warning, v...) }
func Warning(v ...any) { log(warning, v...) }
// Warningf is equivalent to log.Printf, but prefixed with "[WARNING] ".
func Warningf(format string, v ...interface{}) { logf(warning, format, v...) }
func Warningf(format string, v ...any) { logf(warning, format, v...) }
// Error is equivalent to log.Print, but prefixed with "[ERROR] ".
func Error(v ...interface{}) { log(err, v...) }
func Error(v ...any) { log(err, v...) }
// Errorf is equivalent to log.Printf, but prefixed with "[ERROR] ".
func Errorf(format string, v ...interface{}) { logf(err, format, v...) }
func Errorf(format string, v ...any) { logf(err, format, v...) }
// Fatal is equivalent to log.Print, but prefixed with "[FATAL] ", and calling
// os.Exit(1).
func Fatal(v ...interface{}) { log(fatal, v...); os.Exit(1) }
func Fatal(v ...any) { log(fatal, v...); os.Exit(1) }
// Fatalf is equivalent to log.Printf, but prefixed with "[FATAL] ", and calling
// os.Exit(1)
func Fatalf(format string, v ...interface{}) { logf(fatal, format, v...); os.Exit(1) }
func Fatalf(format string, v ...any) { logf(fatal, format, v...); os.Exit(1) }
// Discard sets the log output to /dev/null.
func Discard() { golog.SetOutput(io.Discard) }

View File

@@ -14,16 +14,16 @@ type P struct {
// I.e [INFO] plugin/<name>: message.
func NewWithPlugin(name string) P { return P{"plugin/" + name + ": "} }
func (p P) logf(level, format string, v ...interface{}) {
func (p P) logf(level, format string, v ...any) {
log(level, p.plugin, fmt.Sprintf(format, v...))
}
func (p P) log(level string, v ...interface{}) {
func (p P) log(level string, v ...any) {
log(level+p.plugin, v...)
}
// Debug logs as log.Debug.
func (p P) Debug(v ...interface{}) {
func (p P) Debug(v ...any) {
if !D.Value() {
return
}
@@ -32,7 +32,7 @@ func (p P) Debug(v ...interface{}) {
}
// Debugf logs as log.Debugf.
func (p P) Debugf(format string, v ...interface{}) {
func (p P) Debugf(format string, v ...any) {
if !D.Value() {
return
}
@@ -41,50 +41,50 @@ func (p P) Debugf(format string, v ...interface{}) {
}
// Info logs as log.Info.
func (p P) Info(v ...interface{}) {
func (p P) Info(v ...any) {
ls.info(p.plugin, v...)
p.log(info, v...)
}
// Infof logs as log.Infof.
func (p P) Infof(format string, v ...interface{}) {
func (p P) Infof(format string, v ...any) {
ls.infof(p.plugin, format, v...)
p.logf(info, format, v...)
}
// Warning logs as log.Warning.
func (p P) Warning(v ...interface{}) {
func (p P) Warning(v ...any) {
ls.warning(p.plugin, v...)
p.log(warning, v...)
}
// Warningf logs as log.Warningf.
func (p P) Warningf(format string, v ...interface{}) {
func (p P) Warningf(format string, v ...any) {
ls.warningf(p.plugin, format, v...)
p.logf(warning, format, v...)
}
// Error logs as log.Error.
func (p P) Error(v ...interface{}) {
func (p P) Error(v ...any) {
ls.error(p.plugin, v...)
p.log(err, v...)
}
// Errorf logs as log.Errorf.
func (p P) Errorf(format string, v ...interface{}) {
func (p P) Errorf(format string, v ...any) {
ls.errorf(p.plugin, format, v...)
p.logf(err, format, v...)
}
// Fatal logs as log.Fatal and calls os.Exit(1).
func (p P) Fatal(v ...interface{}) {
func (p P) Fatal(v ...any) {
ls.fatal(p.plugin, v...)
p.log(fatal, v...)
os.Exit(1)
}
// Fatalf logs as log.Fatalf and calls os.Exit(1).
func (p P) Fatalf(format string, v ...interface{}) {
func (p P) Fatalf(format string, v ...any) {
ls.fatalf(p.plugin, format, v...)
p.logf(fatal, format, v...)
os.Exit(1)

View File

@@ -123,10 +123,7 @@ func (p *Proxy) Connect(ctx context.Context, state request.Request, opts Options
}
// Set buffer size correctly for this client.
pc.c.UDPSize = uint16(state.Size())
if pc.c.UDPSize < 512 {
pc.c.UDPSize = 512
}
pc.c.UDPSize = max(uint16(state.Size()), 512)
pc.c.SetWriteDeadline(time.Now().Add(maxTimeout))
// records the origin Id before upstream.

View File

@@ -256,7 +256,7 @@ func loadFormat(s string) replacer {
// bufPool stores pointers to scratch buffers.
var bufPool = sync.Pool{
New: func() interface{} {
New: func() any {
return make([]byte, 0, 256)
},
}

View File

@@ -272,11 +272,10 @@ func BenchmarkReplacer(b *testing.B) {
r.AuthenticatedData = true
state := request.Request{W: w, Req: r}
b.ResetTimer()
b.ReportAllocs()
replacer := New()
for range b.N {
for b.Loop() {
replacer.Replace(context.TODO(), state, nil, "{type} {name} {size}")
}
}
@@ -295,15 +294,14 @@ func BenchmarkReplacer_CommonLogFormat(b *testing.B) {
replacer := New()
ctxt := context.TODO()
b.ResetTimer()
b.ReportAllocs()
for range b.N {
for b.Loop() {
replacer.Replace(ctxt, state, w, CommonLogFormat)
}
}
func BenchmarkParseFormat(b *testing.B) {
for range b.N {
for b.Loop() {
parseFormat(CommonLogFormat)
}
}

View File

@@ -23,7 +23,7 @@ import "sync"
// call is an in-flight or completed Do call
type call struct {
wg sync.WaitGroup
val interface{}
val any
err error
}
@@ -38,7 +38,7 @@ type Group struct {
// sure that only one execution is in-flight for a given key at a
// time. If a duplicate comes in, the duplicate caller waits for the
// original to complete and receives the same results.
func (g *Group) Do(key uint64, fn func() (interface{}, error)) (interface{}, error) {
func (g *Group) Do(key uint64, fn func() (any, error)) (any, error) {
g.mu.Lock()
if g.m == nil {
g.m = make(map[uint64]*call)

View File

@@ -27,7 +27,7 @@ import (
func TestDo(t *testing.T) {
var g Group
v, err := g.Do(1, func() (interface{}, error) {
v, err := g.Do(1, func() (any, error) {
return "bar", nil
})
if got, want := fmt.Sprintf("%v (%T)", v, v), "bar (string)"; got != want {
@@ -41,7 +41,7 @@ func TestDo(t *testing.T) {
func TestDoErr(t *testing.T) {
var g Group
someErr := errors.New("some error")
v, err := g.Do(1, func() (interface{}, error) {
v, err := g.Do(1, func() (any, error) {
return nil, someErr
})
if err != someErr {
@@ -56,7 +56,7 @@ func TestDoDupSuppress(t *testing.T) {
var g Group
c := make(chan string)
var calls int32
fn := func() (interface{}, error) {
fn := func() (any, error) {
atomic.AddInt32(&calls, 1)
return <-c, nil
}