summaryrefslogtreecommitdiffhomepage
path: root/tstime
AgeCommit message (Collapse)AuthorFilesLines
2026-01-23all: remove AUTHORS file and references to itWill Norris10-10/+10
This file was never truly necessary and has never actually been used in the history of Tailscale's open source releases. A Brief History of AUTHORS files --- The AUTHORS file was a pattern developed at Google, originally for Chromium, then adopted by Go and a bunch of other projects. The problem was that Chromium originally had a copyright line only recognizing Google as the copyright holder. Because Google (and most open source projects) do not require copyright assignemnt for contributions, each contributor maintains their copyright. Some large corporate contributors then tried to add their own name to the copyright line in the LICENSE file or in file headers. This quickly becomes unwieldy, and puts a tremendous burden on anyone building on top of Chromium, since the license requires that they keep all copyright lines intact. The compromise was to create an AUTHORS file that would list all of the copyright holders. The LICENSE file and source file headers would then include that list by reference, listing the copyright holder as "The Chromium Authors". This also become cumbersome to simply keep the file up to date with a high rate of new contributors. Plus it's not always obvious who the copyright holder is. Sometimes it is the individual making the contribution, but many times it may be their employer. There is no way for the proejct maintainer to know. Eventually, Google changed their policy to no longer recommend trying to keep the AUTHORS file up to date proactively, and instead to only add to it when requested: https://opensource.google/docs/releasing/authors. They are also clear that: > Adding contributors to the AUTHORS file is entirely within the > project's discretion and has no implications for copyright ownership. It was primarily added to appease a small number of large contributors that insisted that they be recognized as copyright holders (which was entirely their right to do). But it's not truly necessary, and not even the most accurate way of identifying contributors and/or copyright holders. In practice, we've never added anyone to our AUTHORS file. It only lists Tailscale, so it's not really serving any purpose. It also causes confusion because Tailscalars put the "Tailscale Inc & AUTHORS" header in other open source repos which don't actually have an AUTHORS file, so it's ambiguous what that means. Instead, we just acknowledge that the contributors to Tailscale (whoever they are) are copyright holders for their individual contributions. We also have the benefit of using the DCO (developercertificate.org) which provides some additional certification of their right to make the contribution. The source file changes were purely mechanical with: git ls-files | xargs sed -i -e 's/\(Tailscale Inc &\) AUTHORS/\1 contributors/g' Updates #cleanup Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d Signed-off-by: Will Norris <will@tailscale.com>
2025-04-17tstime: add GoDuration which JSON serializes with time.Duration.String (#15726)Joe Tsai2-0/+55
The encoding/json/v2 effort may end up changing the default represention of time.Duration in JSON. See https://go.dev/issue/71631 The GoDuration type allows us to explicitly use the time.Duration.String representation regardless of whether we serialize with v1 or v2 of encoding/json. Updates tailscale/corp#27502 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2024-06-05all: use math/rand/v2 moreMaisem Ali1-26/+2
Updates #11058 Signed-off-by: Maisem Ali <maisem@tailscale.com>
2024-04-16all: use Go 1.22 range-over-intBrad Fitzpatrick4-8/+6
Updates #11058 Change-Id: I35e7ef9b90e83cac04ca93fd964ad00ed5b48430 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2024-01-16tstime/rate: implement Value.{Marshal,Unmarshal}JSON (#8481)Joe Tsai2-0/+64
Implement support for marshaling and unmarshaling a Value. Updates tailscale/corp#8427 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2023-12-20all: cleanup unused code, part 1 (#10661)Andrew Lytvynov1-32/+0
Run `staticcheck` with `U1000` to find unused code. This cleans up about a half of it. I'll do the other half separately to keep PRs manageable. Updates #cleanup Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
2023-10-12tstime: add DefaultClock (#9691)Joe Tsai1-0/+40
In almost every single use of Clock, there is a default behavior we want to use when the interface is nil, which is to use the the standard time package. The Clock interface exists only for testing, and so tests that care about mocking time can adequately plumb the the Clock down the stack and through various data structures. However, the problem with Clock is that there are many situations where we really don't care about mocking time (e.g., measuring execution time for a log message), where making sure that Clock is non-nil is not worth the burden. In fact, in a recent refactoring, the biggest pain point was dealing with nil-interface panics when calling tstime.Clock methods where mocking time wasn't even needed for the relevant tests. This required wasted time carefully reviewing the code to make sure that tstime.Clock was always populated, and even then we're not statically guaranteed to avoid a nil panic. Ideally, what we want are default methods on Go interfaces, but such a language construct does not exist. However, we can emulate that behavior by declaring a concrete type that embeds the interface. If the underlying interface value is nil, it provides some default behavior (i.e., use StdClock). This provides us a nice balance of two goals: * We can plumb tstime.DefaultClock in all relevant places for use with mocking time in the tests that care. * For all other logic that don't care about, we never need to worry about whether tstime.DefaultClock is nil or not. This is especially relevant in production code where we don't want to panic. Longer-term, we may want to perform a large-scale change where we rename Clock to ClockInterface and rename DefaultClock to just Clock. Updates #cleanup Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2023-08-11tstime/mono: remove unsafeBrad Fitzpatrick1-5/+1
This removes the unsafe/linkname and only uses the standard library. It's a bit slower, for now, but https://go.dev/cl/518336 should get us back. On darwin/arm64, without https://go.dev/cl/518336 pkg: tailscale.com/tstime/mono │ before │ after │ │ sec/op │ sec/op vs base │ MonoNow-8 16.20n ± 0% 19.75n ± 0% +21.92% (p=0.000 n=10) TimeNow-8 39.46n ± 0% 39.40n ± 0% -0.16% (p=0.002 n=10) geomean 25.28n 27.89n +10.33% And with it, MonoNow-8 16.34n ± 1% 16.93n ± 0% +3.67% (p=0.001 n=10) TimeNow-8 39.55n ± 15% 38.46n ± 1% -2.76% (p=0.000 n=10) geomean 25.42n 25.52n +0.41% Updates #8839 Updates tailscale/go#70 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2023-07-14tstime: add Since method (#8622)Claire Wang1-0/+8
Updates #8463 Signed-off-by: Claire Wang <claire@tailscale.com>
2023-07-07tstest, tstime: mockable timers and tickersAdrian Dewhurst1-0/+76
This change introduces tstime.Clock which is the start of a mockable interface for use with testing other upcoming code changes. Fixes #8463 Change-Id: I59eabc797828809194575736615535d918242ec4 Signed-off-by: Adrian Dewhurst <adrian@tailscale.com>
2023-06-28tstime/mono: fix Time.Unmarshal (#8480)Joe Tsai2-3/+20
Calling both mono.Now() and time.Now() is slow and leads to unnecessary precision errors. Instead, directly compute mono.Time relative to baseMono and baseWall. This is the opposite calculation as mono.Time.WallTime. Updates tailscale/corp#8427 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2023-03-09tstime/rate: add Value (#7491)Joe Tsai3-6/+427
Add Value, which measures the rate at which an event occurs, exponentially weighted towards recent activity. It is guaranteed to occupy O(1) memory, operate in O(1) runtime, and is safe for concurrent use. Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2023-03-06tstime: rely on stdlib parse functionality (#7482)Joe Tsai2-306/+8
The time.Parse function has been optimized to the point where it is faster than our custom implementation. See upstream changes in: * https://go.dev/cl/429862 * https://go.dev/cl/425197 * https://go.dev/cl/425116 Performance: BenchmarkGoParse3339/Z 38.75 ns/op 0 B/op 0 allocs/op BenchmarkGoParse3339/TZ 54.02 ns/op 0 B/op 0 allocs/op BenchmarkParse3339/Z 40.17 ns/op 0 B/op 0 allocs/op BenchmarkParse3339/TZ 87.06 ns/op 0 B/op 0 allocs/op We can see that the stdlib implementation is now faster. Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2023-03-06tstime: add Sleep (#7480)Joe Tsai1-0/+14
Sleep is an interruptible sleep variation. Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2023-01-27all: update copyright and license headersWill Norris8-24/+16
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
2022-11-16tstime: fix ParseDuration for '6' digit (#6363)Joe Tsai2-3/+7
The cutset provided to strings.TrimRight was missing the digit '6', making it such that we couldn't parse something like "365d". Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2022-11-04all: remove old +build tagsBrad Fitzpatrick1-1/+0
The //go:build syntax was introduced in Go 1.17: https://go.dev/doc/go1.17#build-lines gofmt has kept the +build and go:build lines in sync since then, but enough time has passed. Time to remove them. Done with: perl -i -npe 's,^// \+build.*\n,,' $(git grep -l -F '+build') Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-09-28tstime: add ParseDuration helper functionMihai Parparita2-0/+47
More expressive than time.ParseDuration, also accepting d (days) and w (weeks) literals. Signed-off-by: Mihai Parparita <mihai@tailscale.com>
2022-03-05tstime/mono: fix Before function commentRobert Fritzsche1-1/+1
Signed-off-by: Robert Fritzsche <r.fritzsche@gridx.de>
2022-02-28tstime/rate: deflake TestLongRunningQPS even moreBrad Fitzpatrick1-56/+0
Previous de-flakings: * 8cf1af8a0703c36256fc58e98ddb63b8907848f1 for #3733 * 30458c71c81a3d680aacecafa67fabc1c728c52d for #2727 Fixes #4044 Change-Id: I506cf1ff37bb224f5a9929f1998901e60b24535d Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2022-01-13tstime/rate: deflake TestLongRunningQPSXe/winui-bugreport-without-tailscaledJosh Bleecher Snyder1-3/+4
This test set the bar too high. Just a couple of missed timers was enough to fail. Change the test to more of a sanity check. While we're here, run it for just 1s instead of 5s. Prior to this change, on a 13" M1 MPB, with stress -p 512 ./rate.test -test.run=QPS I saw 90%+ failures. After this change, I'm at 30k runs with no failures yet. Fixes #3733 Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-08-30all: add (*testing.B).ReportAllocs() to every benchmarkEmmanuel T Odeke2-1/+3
This ensures that we can properly track and catch allocation slippages that could otherwise have been missed. Fixes #2748
2021-08-30tstime/rate: deflake TestLongRunningQPSJoe Tsai1-6/+9
This test is highly dependent on the accuracy of OS timers. Reduce the number of failures by decreasing the required accuracy from 0.999 to 0.995. Also, switch from repeated time.Sleep to using a time.Ticker for improved accuracy. Updates #2727 Signed-off-by: Joe Tsai <joetsai@digital-static.net>
2021-08-04tstime/mono: make json.Unmarshal of a zero time.Time yield a zero TimeJosh Bleecher Snyder2-0/+21
This was the proximate cause of #2579. #2582 is a deeper fix, but this will remain as a footgun, so may as well fix it too. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-08-04ipn/ipnstate: move tailscale status "active" determination to tailscaledBrad Fitzpatrick1-4/+9
Fixes #2579 Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2021-07-29tstime/rate: new packageJosh Bleecher Snyder2-0/+335
This is a simplified rate limiter geared for exactly our needs: A fast, mono.Time-based rate limiter for use in tstun. It was generated by stripping down the x/time/rate rate limiter to just our needs and switching it to use mono.Time. It removes one time.Now call per packet. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-07-29tstime/mono: new packageJosh Bleecher Snyder2-0/+151
Package mono provides a fast monotonic time. Its primary advantage is that it is fast: It is approximately twice as fast as time.Now. This is because time.Now uses two clock calls, one for wall time and one for monotonic time. We ask for the current time 4-6 times per network packet. At ~50ns per call to time.Now, that's enough to show up in CPU profiles. Package mono is a first step towards addressing that. It is designed to be a near drop-in replacement for package time. Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2021-01-21tstime: add RandomDurationBetween helperBrad Fitzpatrick2-0/+67
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2020-11-19tstime: add Parse3339B, for byte slicesJosh Bleecher Snyder2-53/+76
Use go4.org/mem for memory safety. A slight performance hit, but a huge performance win for clients who start with a []byte. The perf hit is due largely to the MapHash call, which adds ~25ns. That is necessary to keep the fast path allocation-free. name old time/op new time/op delta GoParse3339/Z-8 281ns ± 1% 283ns ± 2% ~ (p=0.366 n=9+9) GoParse3339/TZ-8 509ns ± 0% 510ns ± 1% ~ (p=0.059 n=9+9) GoParse3339InLocation-8 330ns ± 1% 330ns ± 0% ~ (p=0.802 n=10+6) Parse3339/Z-8 69.3ns ± 1% 74.4ns ± 1% +7.45% (p=0.000 n=9+10) Parse3339/TZ-8 110ns ± 1% 140ns ± 3% +27.42% (p=0.000 n=9+10) ParseInt-8 8.20ns ± 1% 8.17ns ± 1% ~ (p=0.452 n=9+9) name old alloc/op new alloc/op delta GoParse3339/Z-8 0.00B 0.00B ~ (all equal) GoParse3339/TZ-8 160B ± 0% 160B ± 0% ~ (all equal) GoParse3339InLocation-8 0.00B 0.00B ~ (all equal) Parse3339/Z-8 0.00B 0.00B ~ (all equal) Parse3339/TZ-8 0.00B 0.00B ~ (all equal) name old allocs/op new allocs/op delta GoParse3339/Z-8 0.00 0.00 ~ (all equal) GoParse3339/TZ-8 3.00 ± 0% 3.00 ± 0% ~ (all equal) GoParse3339InLocation-8 0.00 0.00 ~ (all equal) Parse3339/Z-8 0.00 0.00 ~ (all equal) Parse3339/TZ-8 0.00 0.00 ~ (all equal) Signed-off-by: Josh Bleecher Snyder <josh@tailscale.com>
2020-04-14tstime: hand-implement parseInt for specific needs of rfc3339 parsing.David Anderson2-4/+44
Makes parsing 4.6x faster. name old time/op new time/op delta ParseInt-12 32.1ns ± 1% 6.9ns ± 2% -78.55% (p=0.000 n=10+9) Signed-off-by: David Anderson <danderson@tailscale.com>
2020-04-06tstime: change an Errorf+return to Fatalf in subtestBrad Fitzpatrick1-2/+1
Forgot to git add this during review. Fail.
2020-04-06tstime: write Parse3339 parse that doesn't use time.ParseBrad Fitzpatrick2-46/+154
It doesn't allocate and it's half the time of time.Parse (which allocates), and 2/3rds the time of time.ParseInLocation (which doesn't). Go with a UTC time: BenchmarkGoParse3339/Z-8 2200995 534 ns/op 0 B/op 0 allocs/op BenchmarkGoParse3339/Z-8 2254816 554 ns/op 0 B/op 0 allocs/op BenchmarkGoParse3339/Z-8 2159504 522 ns/op 0 B/op 0 allocs/op Go allocates with a "-08:00" suffix instead of ending in "Z": BenchmarkGoParse3339/TZ-8 1276491 884 ns/op 144 B/op 3 allocs/op BenchmarkGoParse3339/TZ-8 1355858 942 ns/op 144 B/op 3 allocs/op BenchmarkGoParse3339/TZ-8 1385484 911 ns/op 144 B/op 3 allocs/op Go doesn't allocate if you use time.ParseInLocation, but then you need to parse the string to find the location anyway, so might as well go all the way (below). BenchmarkGoParse3339InLocation-8 1912254 597 ns/op 0 B/op 0 allocs/op BenchmarkGoParse3339InLocation-8 1980043 612 ns/op 0 B/op 0 allocs/op BenchmarkGoParse3339InLocation-8 1891366 612 ns/op 0 B/op 0 allocs/op Parsing RFC3339 ourselves, UTC: BenchmarkParse3339/Z-8 3889220 307 ns/op 0 B/op 0 allocs/op BenchmarkParse3339/Z-8 3718500 309 ns/op 0 B/op 0 allocs/op BenchmarkParse3339/Z-8 3621231 303 ns/op 0 B/op 0 allocs/op Parsing RFC3339 ourselves, with timezone (w/ *time.Location fetched from sync.Map) BenchmarkParse3339/TZ-8 3019612 418 ns/op 0 B/op 0 allocs/op BenchmarkParse3339/TZ-8 2921618 401 ns/op 0 B/op 0 allocs/op BenchmarkParse3339/TZ-8 3031671 408 ns/op 0 B/op 0 allocs/op Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
2020-04-05tstime: add new package for time utilities, starting with Parse3339Brad Fitzpatrick2-0/+162
Go's time.Parse always allocates a FixedZone for time strings not in UTC (ending in "Z"). This avoids that allocation, at the cost of adding a cache. Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>