all: Truncate some timestamps (fixes #7457) (#7459)

This truncates times meant for API consumption to second precision,
where fractions won't typically matter or add any value. Exception to
this is timestamps on logs and events, and of course I'm not touching
things like file metadata.

I'm not 100% certain this is an exhaustive change, but it's the things I
found by grepping and following the breadcrumbs from lib/api...

I also considered general-but-ugly solutions, like having the API
serializer itself do reflection magic or even regexps on returned
objects, but decided against it because aurgh...
This commit is contained in:
Jakob Borg 2021-03-12 10:35:10 +01:00 committed by GitHub
parent 4465cdf8bc
commit 4d979a1ce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 17 additions and 16 deletions

View File

@ -10,7 +10,6 @@ import (
"encoding/json" "encoding/json"
"flag" "flag"
"fmt" "fmt"
"github.com/syncthing/syncthing/lib/protocol"
"io" "io"
"io/ioutil" "io/ioutil"
"log" "log"
@ -23,6 +22,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/golang/groupcache/lru" "github.com/golang/groupcache/lru"
"github.com/oschwald/geoip2-golang" "github.com/oschwald/geoip2-golang"
"github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus"
@ -475,7 +476,7 @@ func handleRelayTest(request request) {
updateMetrics(request.relay.uri.Host, *stats, location) updateMetrics(request.relay.uri.Host, *stats, location)
} }
request.relay.Stats = stats request.relay.Stats = stats
request.relay.StatsRetrieved = time.Now() request.relay.StatsRetrieved = time.Now().Truncate(time.Second)
request.relay.Location = location request.relay.Location = location
timer, ok := evictionTimers[request.relay.uri.Host] timer, ok := evictionTimers[request.relay.uri.Host]

View File

@ -89,7 +89,7 @@ func newInternalConn(tc tlsConn, connType connType, priority int) internalConn {
tlsConn: tc, tlsConn: tc,
connType: connType, connType: connType,
priority: priority, priority: priority,
establishedAt: time.Now(), establishedAt: time.Now().Truncate(time.Second),
} }
} }

View File

@ -15,7 +15,7 @@ import (
func (db *Lowlevel) AddOrUpdatePendingDevice(device protocol.DeviceID, name, address string) error { func (db *Lowlevel) AddOrUpdatePendingDevice(device protocol.DeviceID, name, address string) error {
key := db.keyer.GeneratePendingDeviceKey(nil, device[:]) key := db.keyer.GeneratePendingDeviceKey(nil, device[:])
od := ObservedDevice{ od := ObservedDevice{
Time: time.Now().Round(time.Second), Time: time.Now().Truncate(time.Second),
Name: name, Name: name,
Address: address, Address: address,
} }
@ -72,7 +72,7 @@ func (db *Lowlevel) AddOrUpdatePendingFolder(id, label string, device protocol.D
return err return err
} }
of := ObservedFolder{ of := ObservedFolder{
Time: time.Now().Round(time.Second), Time: time.Now().Truncate(time.Second),
Label: label, Label: label,
ReceiveEncrypted: receiveEncrypted, ReceiveEncrypted: receiveEncrypted,
} }

View File

@ -317,7 +317,7 @@ loop:
func (l *logger) Log(t EventType, data interface{}) { func (l *logger) Log(t EventType, data interface{}) {
l.events <- Event{ l.events <- Event{
Time: time.Now(), Time: time.Now(), // intentionally high precision
Type: t, Type: t,
Data: data, Data: data,
// SubscriptionID and GlobalID are set in sendEvent // SubscriptionID and GlobalID are set in sendEvent

View File

@ -337,7 +337,7 @@ func (r *recorder) Clear() {
func (r *recorder) append(l LogLevel, msg string) { func (r *recorder) append(l LogLevel, msg string) {
line := Line{ line := Line{
When: time.Now(), When: time.Now(), // intentionally high precision
Message: msg, Message: msg,
Level: l, Level: l,
} }

View File

@ -100,7 +100,7 @@ func (s *stateTracker) setState(newState folderState) {
} }
s.current = newState s.current = newState
s.changed = time.Now() s.changed = time.Now().Truncate(time.Second)
s.evLogger.Log(events.StateChanged, eventData) s.evLogger.Log(events.StateChanged, eventData)
} }
@ -139,7 +139,7 @@ func (s *stateTracker) setError(err error) {
} }
s.err = err s.err = err
s.changed = time.Now() s.changed = time.Now().Truncate(time.Second)
s.evLogger.Log(events.StateChanged, eventData) s.evLogger.Log(events.StateChanged, eventData)
} }

View File

@ -769,7 +769,7 @@ func (m *model) ConnectionStats() map[string]interface{} {
in, out := protocol.TotalInOut() in, out := protocol.TotalInOut()
res["total"] = ConnectionInfo{ res["total"] = ConnectionInfo{
Statistics: protocol.Statistics{ Statistics: protocol.Statistics{
At: time.Now(), At: time.Now().Truncate(time.Second),
InBytesTotal: in, InBytesTotal: in,
OutBytesTotal: out, OutBytesTotal: out,
}, },

View File

@ -296,7 +296,7 @@ func (c *rawConnection) Start() {
c.pingReceiver() c.pingReceiver()
c.loopWG.Done() c.loopWG.Done()
}() }()
c.startTime = time.Now() c.startTime = time.Now().Truncate(time.Second)
} }
func (c *rawConnection) ID() DeviceID { func (c *rawConnection) ID() DeviceID {
@ -1040,7 +1040,7 @@ type Statistics struct {
func (c *rawConnection) Statistics() Statistics { func (c *rawConnection) Statistics() Statistics {
return Statistics{ return Statistics{
At: time.Now(), At: time.Now().Truncate(time.Second),
InBytesTotal: c.cr.Tot(), InBytesTotal: c.cr.Tot(),
OutBytesTotal: c.cw.Tot(), OutBytesTotal: c.cw.Tot(),
StartedAt: c.startTime, StartedAt: c.startTime,

View File

@ -62,7 +62,7 @@ func (s *DeviceStatisticsReference) GetLastConnectionDuration() (time.Duration,
func (s *DeviceStatisticsReference) WasSeen() error { func (s *DeviceStatisticsReference) WasSeen() error {
l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device) l.Debugln("stats.DeviceStatisticsReference.WasSeen:", s.device)
return s.ns.PutTime(lastSeenKey, time.Now()) return s.ns.PutTime(lastSeenKey, time.Now().Truncate(time.Second))
} }
func (s *DeviceStatisticsReference) LastConnectionDuration(d time.Duration) error { func (s *DeviceStatisticsReference) LastConnectionDuration(d time.Duration) error {

View File

@ -61,7 +61,7 @@ func (s *FolderStatisticsReference) GetLastFile() (LastFile, error) {
func (s *FolderStatisticsReference) ReceivedFile(file string, deleted bool) error { func (s *FolderStatisticsReference) ReceivedFile(file string, deleted bool) error {
l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file) l.Debugln("stats.FolderStatisticsReference.ReceivedFile:", s.folder, file)
if err := s.ns.PutTime("lastFileAt", time.Now()); err != nil { if err := s.ns.PutTime("lastFileAt", time.Now().Truncate(time.Second)); err != nil {
return err return err
} }
if err := s.ns.PutString("lastFileName", file); err != nil { if err := s.ns.PutString("lastFileName", file); err != nil {
@ -74,7 +74,7 @@ func (s *FolderStatisticsReference) ReceivedFile(file string, deleted bool) erro
} }
func (s *FolderStatisticsReference) ScanCompleted() error { func (s *FolderStatisticsReference) ScanCompleted() error {
return s.ns.PutTime("lastScan", time.Now()) return s.ns.PutTime("lastScan", time.Now().Truncate(time.Second))
} }
func (s *FolderStatisticsReference) GetLastScanTime() (time.Time, error) { func (s *FolderStatisticsReference) GetLastScanTime() (time.Time, error) {

View File

@ -36,7 +36,7 @@ import (
// are prompted for acceptance of the new report. // are prompted for acceptance of the new report.
const Version = 3 const Version = 3
var StartTime = time.Now() var StartTime = time.Now().Truncate(time.Second)
type Service struct { type Service struct {
cfg config.Wrapper cfg config.Wrapper