Go 1.27 enables GOEXPERIMENT=jsonv2 by default: encoding/json is now
backed by the json/v2 machinery, and github.com/go-json-experiment/json
compiles as a thin alias of the standard library's encoding/json/v2.
Several tag options and behaviors we relied on did not make the cut for
the final Go 1.27 API, breaking tailscaled at runtime and four packages'
tests. This change adapts to the final API while keeping the wire format
byte-for-byte identical on all Go versions.
First, the `format` tag option was demoted to experimental. Its mere
presence in a struct tag now makes marshaling and unmarshaling fail at
runtime. tailcfg.SSHAction.SessionDuration had `format:nano` (added in
a2dc517d7 to pin the v1 representation), so on Go 1.27 any netmap
containing an SSH policy failed to decode, breaking every PollNetMap.
Remove the option here and in net/speedtest; time.Duration still
marshals as int64 nanoseconds under encoding/json on all Go versions
(Go 1.27's v1 mode sets FormatDurationAsNano by default), so old
clients and servers are unaffected. Add a regression test locking in
the exact wire format.
Consequently, invert the cmd/vet jsontags rule: it previously required
an explicit `format` tag on time.Duration fields, which is now exactly
wrong. It now rejects any `format` tag option, which would have caught
this bug in CI.
Second, the `inline` tag option was renamed to `embed`. The standard
library silently ignores `inline`, while the pinned go-json-experiment
module (used on Go 1.26) only knows `inline`. Specify both options in
types/prefs and logtail; each implementation ignores the option it does
not know, producing identical output. Drop `inline` once we require
Go 1.27.
Third, encoding/json (v1) now dispatches to MarshalJSONTo and
UnmarshalJSONFrom methods and its v1 options flow into nested
jsonv2.MarshalEncode calls. Types whose v1 methods deliberately
routed through jsonv2 for v2 semantics (types/opt.Value, the
types/prefs preference types) would silently change wire format
(e.g. nil slices becoming null). Pin jsonv2.DefaultOptionsV2 in
their jsonv2 methods so the representation is the same regardless
of the entry point.
Finally, json.Marshal costs one more allocation under Go 1.27,
tripping the types/logger.AsJSON alloc test. Switch its fmt.Formatter
to jsonv2.MarshalWrite with explicit v1 options, which writes directly
to the fmt.State: one allocation on both toolchains with unchanged
output. Depaware files pick up the go-json-experiment/json/v1 options
shim as a new dependency of types/logger.
With this change, go test ./... passes with both Go 1.26.5 and
go1.27rc2.
Updates #20220
Fixes #20528
Fixes #20254
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I694c7d57fd81e55a579c579e9be10032bca569d4
88 lines
2.4 KiB
Go
88 lines
2.4 KiB
Go
// Copyright (c) Tailscale Inc & contributors
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package speedtest contains both server and client code for
|
|
// running speedtests between tailscale nodes.
|
|
package speedtest
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
blockSize = 2 * 1024 * 1024 // size of the block of data to send
|
|
MinDuration = 5 * time.Second // minimum duration for a test
|
|
DefaultDuration = MinDuration // default duration for a test
|
|
MaxDuration = 30 * time.Second // maximum duration for a test
|
|
version = 2 // value used when comparing client and server versions
|
|
increment = time.Second // increment to display results for, in seconds
|
|
minInterval = 10 * time.Millisecond // minimum interval length for a result to be included
|
|
DefaultPort = 20333
|
|
)
|
|
|
|
// config is the initial message sent to the server, that contains information on how to
|
|
// conduct the test.
|
|
type config struct {
|
|
Version int `json:"version"`
|
|
TestDuration time.Duration `json:"time"` // int64 nanoseconds; no jsonv2 format tag (tailscale/tailscale#20528)
|
|
Direction Direction `json:"direction"`
|
|
}
|
|
|
|
// configResponse is the response to the testConfig message. If the server has an
|
|
// error with the config, the Error variable will hold that error value.
|
|
type configResponse struct {
|
|
Error string `json:"error,omitempty"`
|
|
}
|
|
|
|
// This represents the Result of a speedtest within a specific interval
|
|
type Result struct {
|
|
Bytes int // number of bytes sent/received during the interval
|
|
IntervalStart time.Time // start of the interval
|
|
IntervalEnd time.Time // end of the interval
|
|
Total bool // if true, this result struct represents the entire test, rather than a segment of the test
|
|
}
|
|
|
|
func (r Result) MBitsPerSecond() float64 {
|
|
return r.MegaBits() / r.IntervalEnd.Sub(r.IntervalStart).Seconds()
|
|
}
|
|
|
|
func (r Result) MegaBytes() float64 {
|
|
return float64(r.Bytes) / 1000000.0
|
|
}
|
|
|
|
func (r Result) MegaBits() float64 {
|
|
return r.MegaBytes() * 8.0
|
|
}
|
|
|
|
func (r Result) Interval() time.Duration {
|
|
return r.IntervalEnd.Sub(r.IntervalStart)
|
|
}
|
|
|
|
type Direction int
|
|
|
|
const (
|
|
Download Direction = iota
|
|
Upload
|
|
)
|
|
|
|
func (d Direction) String() string {
|
|
switch d {
|
|
case Upload:
|
|
return "upload"
|
|
case Download:
|
|
return "download"
|
|
default:
|
|
return ""
|
|
}
|
|
}
|
|
|
|
func (d *Direction) Reverse() {
|
|
switch *d {
|
|
case Upload:
|
|
*d = Download
|
|
case Download:
|
|
*d = Upload
|
|
default:
|
|
}
|
|
}
|