| author | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-25 18:49:49 UTC |
| committer | Alberto Bertogli
<albertito@blitiri.com.ar> 2026-10-25 18:49:49 UTC |
| parent | f543d073e582ee940aa552d381351f2e28abc6a8 |
| config/config.go | +8 | -8 |
| config/config_test.go | +4 | -4 |
| ipratelimit/ipratelimit.go | +8 | -7 |
| ipratelimit/ipratelimit_test.go | +2 | -2 |
| nettrace/bench_test.go | +2 | -2 |
| nettrace/disable.go | +2 | -2 |
| nettrace/histogram.go | +1 | -1 |
| nettrace/http_test.go | +1 | -1 |
| nettrace/trace.go | +8 | -9 |
| nettrace/trace_test.go | +3 | -3 |
| reqlog/reqlog.go | +1 | -1 |
| reqlog/reqlog_test.go | +1 | -1 |
| test/util/acmesrv/acmesrv.go | +1 | -1 |
| test/util/exp/exp.go | +2 | -2 |
| test/util/generate_cert/generate_cert.go | +3 | -3 |
| trace/trace.go | +4 | -4 |
| util/util.go | +4 | -4 |
diff --git a/config/config.go b/config/config.go index 9ff73a3..3ba3cb1 100644 --- a/config/config.go +++ b/config/config.go @@ -268,7 +268,7 @@ type Regexp struct { *regexp.Regexp } -func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (re *Regexp) UnmarshalYAML(unmarshal func(any) error) error { var s string if err := unmarshal(&s); err != nil { return err @@ -283,7 +283,7 @@ func (re *Regexp) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } -func (re Regexp) MarshalYAML() (interface{}, error) { +func (re Regexp) MarshalYAML() (any, error) { return re.String(), nil } @@ -295,7 +295,7 @@ type PathRegexp struct { *regexp.Regexp } -func (re *PathRegexp) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (re *PathRegexp) UnmarshalYAML(unmarshal func(any) error) error { var s string if err := unmarshal(&s); err != nil { return err @@ -311,14 +311,14 @@ func (re *PathRegexp) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } -func (re PathRegexp) MarshalYAML() (interface{}, error) { +func (re PathRegexp) MarshalYAML() (any, error) { return re.orig, nil } // Wrapper to simplify URLs in configuration. type URL url.URL -func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (u *URL) UnmarshalYAML(unmarshal func(any) error) error { var s string if err := unmarshal(&s); err != nil { return err @@ -333,7 +333,7 @@ func (u *URL) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } -func (u *URL) MarshalYAML() (interface{}, error) { +func (u *URL) MarshalYAML() (any, error) { if u == nil { return "", nil } @@ -356,7 +356,7 @@ type Rate struct { Period time.Duration } -func (r *Rate) UnmarshalYAML(unmarshal func(interface{}) error) error { +func (r *Rate) UnmarshalYAML(unmarshal func(any) error) error { var s string if err := unmarshal(&s); err != nil { return err @@ -387,6 +387,6 @@ func (r *Rate) UnmarshalYAML(unmarshal func(interface{}) error) error { return nil } -func (r Rate) MarshalYAML() (interface{}, error) { +func (r Rate) MarshalYAML() (any, error) { return fmt.Sprintf("%d/%s", r.Requests, r.Period), nil } diff --git a/config/config_test.go b/config/config_test.go index f57cd83..831cacc 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -386,7 +386,7 @@ func TestRegexp(t *testing.T) { } // Test handling unmarshal error. - err = re.UnmarshalYAML(func(interface{}) error { return unmarshalErr }) + err = re.UnmarshalYAML(func(any) error { return unmarshalErr }) if err != unmarshalErr { t.Errorf("expected unmarshalErr, got %v", err) } @@ -422,7 +422,7 @@ func TestPathRegexp(t *testing.T) { } // Test handling unmarshal error. - err = re.UnmarshalYAML(func(interface{}) error { return unmarshalErr }) + err = re.UnmarshalYAML(func(any) error { return unmarshalErr }) if err != unmarshalErr { t.Errorf("expected unmarshalErr, got %v", err) } @@ -459,7 +459,7 @@ func TestURL(t *testing.T) { } // Test handling unmarshal error. - err = up.UnmarshalYAML(func(interface{}) error { return unmarshalErr }) + err = up.UnmarshalYAML(func(any) error { return unmarshalErr }) if err != unmarshalErr { t.Errorf("expected unmarshalErr, got %v", err) } @@ -507,7 +507,7 @@ func TestRate(t *testing.T) { } // Test handling unmarshal error. - err = r.UnmarshalYAML(func(interface{}) error { return unmarshalErr }) + err = r.UnmarshalYAML(func(any) error { return unmarshalErr }) if err != unmarshalErr { t.Errorf("expected unmarshalErr, got %v", err) } diff --git a/ipratelimit/ipratelimit.go b/ipratelimit/ipratelimit.go index 7937ed8..7fc3808 100644 --- a/ipratelimit/ipratelimit.go +++ b/ipratelimit/ipratelimit.go @@ -30,6 +30,7 @@ import ( "fmt" "math/big" "net" + "strings" "sync" "time" ) @@ -358,21 +359,21 @@ func (l *limiter) debugString(kToIP func(uint64) net.IP) string { l.mu.Lock() defer l.mu.Unlock() - s := "" - s += fmt.Sprintf("Allow: %d / %v\n", l.Requests, l.Period) - s += fmt.Sprintf("Size: %d / %d\n", len(l.m), l.Size) - s += "\n" + var s strings.Builder + s.WriteString(fmt.Sprintf("Allow: %d / %v\n", l.Requests, l.Period)) + s.WriteString(fmt.Sprintf("Size: %d / %d\n", len(l.m), l.Size)) + s.WriteString("\n") k := l.lruFirst for k != 0 { e := l.m[k] ip := kToIP(k) last := sinceMiniTime(time.Now(), e.lastAllowed).Round( time.Millisecond) - s += fmt.Sprintf("%-22s %3d requests left, last allowed %10s ago\n", - ip, e.requestsLeft, last) + s.WriteString(fmt.Sprintf("%-22s %3d requests left, last allowed %10s ago\n", + ip, e.requestsLeft, last)) k = e.lruNext } - return s + return s.String() } // DebugHTML returns a string with debugging information about the limiter, in diff --git a/ipratelimit/ipratelimit_test.go b/ipratelimit/ipratelimit_test.go index 4f85c36..d013ad4 100644 --- a/ipratelimit/ipratelimit_test.go +++ b/ipratelimit/ipratelimit_test.go @@ -56,10 +56,10 @@ func TestIPv6ExtractMasks(t *testing.T) { func as(yes, no int) []bool { r := []bool{} - for i := 0; i < yes; i++ { + for range yes { r = append(r, true) } - for i := 0; i < no; i++ { + for range no { r = append(r, false) } return r diff --git a/nettrace/bench_test.go b/nettrace/bench_test.go index e78da13..e6430e2 100644 --- a/nettrace/bench_test.go +++ b/nettrace/bench_test.go @@ -9,9 +9,9 @@ import ( func runBench(b *testing.B, events int) { nTraces := (b.N + events + 1) / events - for i := 0; i < nTraces; i++ { + for range nTraces { tr := New("bench", "test") - for j := 0; j < events; j++ { + for j := range events { tr.Printf("%d", j) } tr.Finish() diff --git a/nettrace/disable.go b/nettrace/disable.go index d866f1c..19914b3 100644 --- a/nettrace/disable.go +++ b/nettrace/disable.go @@ -21,13 +21,13 @@ func (disabledTrace) SetError() {} func (disabledTrace) Print(s string) {} -func (disabledTrace) Printf(format string, a ...interface{}) {} +func (disabledTrace) Printf(format string, a ...any) {} func (disabledTrace) Error(err error) error { return err } -func (disabledTrace) Errorf(format string, a ...interface{}) error { +func (disabledTrace) Errorf(format string, a ...any) error { return fmt.Errorf(format, a...) } diff --git a/nettrace/histogram.go b/nettrace/histogram.go index 662a887..8d78958 100644 --- a/nettrace/histogram.go +++ b/nettrace/histogram.go @@ -53,7 +53,7 @@ func (h *histogram) Snapshot() *histSnapshot { } var cumCount uint64 - for i := 0; i < nBuckets; i++ { + for i := range nBuckets { cumCount += h.count[i] l := line{ Start: buckets[i], diff --git a/nettrace/http_test.go b/nettrace/http_test.go index 24c82f6..4664179 100644 --- a/nettrace/http_test.go +++ b/nettrace/http_test.go @@ -141,7 +141,7 @@ func TestHTTP(t *testing.T) { func TestHTTPLong(t *testing.T) { // Test a long trace. tr := New("TestHTTPLong", "verbose") - for i := 0; i < 1000; i++ { + for i := range 1000 { tr.Printf("entry #%d", i) } tr.Finish() diff --git a/nettrace/trace.go b/nettrace/trace.go index 42d1809..39f3ea0 100644 --- a/nettrace/trace.go +++ b/nettrace/trace.go @@ -5,6 +5,7 @@ package nettrace // import "blitiri.com.ar/go/gofer/nettrace" import ( "container/ring" "fmt" + "maps" "math/rand/v2" "sort" "strconv" @@ -54,7 +55,7 @@ type Trace interface { Print(s string) // Printf adds a formatted message to the trace. - Printf(format string, a ...interface{}) + Printf(format string, a ...any) // Error logs an error to the trace, and returns it unchanged (for // convenience). @@ -62,7 +63,7 @@ type Trace interface { // Errorf adds a message to the trace, marks it as an error, and returns // an error for it. - Errorf(format string, a ...interface{}) error + Errorf(format string, a ...any) error // Finish marks the trace as complete. // The trace should not be used after calling this method. @@ -244,7 +245,7 @@ func (tr *trace) Print(s string) { } } -func (tr *trace) Printf(format string, a ...interface{}) { +func (tr *trace) Printf(format string, a ...any) { tr.Print(fmt.Sprintf(format, a...)) } @@ -254,7 +255,7 @@ func (tr *trace) Error(err error) error { return err } -func (tr *trace) Errorf(format string, a ...interface{}) error { +func (tr *trace) Errorf(format string, a ...any) error { return tr.Error(fmt.Errorf(format, a...)) } @@ -375,7 +376,7 @@ func (r *traceRing) Len() int { } func (r *traceRing) Do(f func(tr *trace)) { - r.ring.Do(func(x interface{}) { + r.ring.Do(func(x any) { if x == nil { return } @@ -403,7 +404,7 @@ type familyTraces struct { func newFamilyTraces() *familyTraces { ft := &familyTraces{} ft.active = map[id]*trace{} - for i := 0; i < nBuckets; i++ { + for i := range nBuckets { ft.finished[i] = newTraceRing(tracesInBucket) } ft.errors = newTraceRing(tracesInBucket) @@ -533,9 +534,7 @@ func copyFamilies() map[string]*familyTraces { n := map[string]*familyTraces{} familiesMu.Lock() - for f, trs := range families { - n[f] = trs - } + maps.Copy(n, families) familiesMu.Unlock() return n diff --git a/nettrace/trace_test.go b/nettrace/trace_test.go index 5f7fa91..4394b0f 100644 --- a/nettrace/trace_test.go +++ b/nettrace/trace_test.go @@ -39,13 +39,13 @@ func TestLong(t *testing.T) { tr.SetMaxEvents(100) // First 90 events, no drop. - for i := 0; i < 90; i++ { + for i := range 90 { tr.Printf("evt %d", i) } expectEvents(t, tr, 90) // Up to 99, still no drop. - for i := 0; i < 9; i++ { + for i := range 9 { tr.Printf("evt %d", i) } expectEvents(t, tr, 99) @@ -59,7 +59,7 @@ func TestLong(t *testing.T) { expectEvents(t, tr, 101) // Add more events, expect none of them to exceed 101. - for i := 0; i < 9; i++ { + for i := range 9 { tr.Printf("evt %d", i) expectEvents(t, tr, 101) } diff --git a/reqlog/reqlog.go b/reqlog/reqlog.go index 82af810..bc8ab8e 100644 --- a/reqlog/reqlog.go +++ b/reqlog/reqlog.go @@ -183,7 +183,7 @@ func FromContext(ctx context.Context) *Log { return v.(*Log) } -func quoteString(i interface{}) string { +func quoteString(i any) string { if i == nil { return `""` } diff --git a/reqlog/reqlog_test.go b/reqlog/reqlog_test.go index 6fe1f5a..1eb92d1 100644 --- a/reqlog/reqlog_test.go +++ b/reqlog/reqlog_test.go @@ -42,7 +42,7 @@ func TestBadFile(t *testing.T) { func TestQuoteString(t *testing.T) { cases := []struct { - i interface{} + i any e string }{ {nil, `""`}, diff --git a/test/util/acmesrv/acmesrv.go b/test/util/acmesrv/acmesrv.go index 33f23f5..a68cb9b 100644 --- a/test/util/acmesrv/acmesrv.go +++ b/test/util/acmesrv/acmesrv.go @@ -311,7 +311,7 @@ func readPayload(r *http.Request) []byte { return payload } -func decodePayload(r *http.Request, v interface{}) { +func decodePayload(r *http.Request, v any) { payload := readPayload(r) err := json.Unmarshal(payload, v) if err != nil { diff --git a/test/util/exp/exp.go b/test/util/exp/exp.go index 165592d..9fada7d 100644 --- a/test/util/exp/exp.go +++ b/test/util/exp/exp.go @@ -242,12 +242,12 @@ func mkTransport(caCert string, forceLocalhost bool) *http.Transport { return t } -func fatalf(s string, a ...interface{}) { +func fatalf(s string, a ...any) { fmt.Fprintf(os.Stderr, s, a...) os.Exit(1) } -func errorf(s string, a ...interface{}) { +func errorf(s string, a ...any) { fmt.Fprintf(os.Stderr, s, a...) exitCode = 1 } diff --git a/test/util/generate_cert/generate_cert.go b/test/util/generate_cert/generate_cert.go index f05d8b2..330d752 100644 --- a/test/util/generate_cert/generate_cert.go +++ b/test/util/generate_cert/generate_cert.go @@ -29,7 +29,7 @@ var ( "Should this cert be its own CA?") ) -func fatalf(f string, a ...interface{}) { +func fatalf(f string, a ...any) { fmt.Printf(f, a...) os.Exit(1) } @@ -66,8 +66,8 @@ func main() { tmpl.IsCA = true } - hosts := strings.Split(*host, ",") - for _, h := range hosts { + hosts := strings.SplitSeq(*host, ",") + for h := range hosts { if ip := net.ParseIP(h); ip != nil { tmpl.IPAddresses = append(tmpl.IPAddresses, ip) } else { diff --git a/trace/trace.go b/trace/trace.go index 7a3480e..8878afa 100644 --- a/trace/trace.go +++ b/trace/trace.go @@ -41,7 +41,7 @@ func (t *Trace) SetMaxEvents(n int) { } // Printf adds this message to the trace's log. -func (t *Trace) Printf(format string, a ...interface{}) { +func (t *Trace) Printf(format string, a ...any) { t.t.Printf(format, a...) if log.Default.Level <= log.Debug { @@ -51,7 +51,7 @@ func (t *Trace) Printf(format string, a ...interface{}) { } // Errorf adds this message to the trace's log, with an error level. -func (t *Trace) Errorf(format string, a ...interface{}) error { +func (t *Trace) Errorf(format string, a ...any) error { // Note we can't just call t.Error here, as it breaks caller logging. err := fmt.Errorf(format, a...) t.t.SetError() @@ -76,8 +76,8 @@ func (t *Trace) Finish() { // Write so Trace implements io.Writer, which means it can be used as output // for log.Logger. func (t *Trace) Write(p []byte) (n int, err error) { - lines := strings.Split(string(p), "\n") - for _, line := range lines { + lines := strings.SplitSeq(string(p), "\n") + for line := range lines { if strings.TrimSpace(line) == "" { continue } diff --git a/util/util.go b/util/util.go index 50b29c8..7e1c88c 100644 --- a/util/util.go +++ b/util/util.go @@ -161,17 +161,17 @@ func LoadCertsFromDir(certDir string) (*tls.Config, error) { func BidirCopy(src, dst io.ReadWriter) int64 { done := make(chan bool, 2) - var total int64 + var total atomic.Int64 go func() { n, _ := io.Copy(src, dst) - atomic.AddInt64(&total, n) + total.Add(n) done <- true }() go func() { n, _ := io.Copy(dst, src) - atomic.AddInt64(&total, n) + total.Add(n) done <- true }() @@ -180,5 +180,5 @@ func BidirCopy(src, dst io.ReadWriter) int64 { // the conditions to complete it (e.g. by closing one of the sides). <-done - return atomic.LoadInt64(&total) + return total.Load() }