-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Serialize duration as string, not as nanosecs, in API responses and DB #1542
feat: Serialize duration as string, not as nanosecs, in API responses and DB #1542
Conversation
b8e7465
to
daccaa0
Compare
// Package duration provides a wrapper for time.Duration type, | ||
// to serialize duration as string instead of int64 nanoseconds. | ||
// | ||
// This cannot be changed in Go 1.X, it would break backward compatibility, see: | ||
// https://github.com/golang/go/issues/10275 | ||
// "This isn't possible to change now, as it would change the encodings produced by programs that exist today." | ||
package duration |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Docs.
"wait": false, | ||
"checkInterval": 1000000, | ||
"checkInterval": "1ms", | ||
"countTrigger": 100, | ||
"bytesTrigger": "100KB", | ||
"intervalTrigger": 100000000 | ||
"intervalTrigger": "100ms" | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change in the database snapshot.
syncCounter++ | ||
clk.Add(cfg.SyncInterval) | ||
clk.Add(cfg.SyncInterval.Duration()) | ||
assert.Eventually(t, func() bool { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated in related code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
if v, ok := fl.Field().Interface().(time.Duration); ok { | ||
value = v | ||
} else if v, ok := fl.Field().Interface().(duration.Duration); ok { | ||
value = v.Duration() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay I think I need a bit more info on type casting here. 🤔 With duration.Duration
being defined as
type Duration time.Duration
I'd expect the first condition to pass even for duration.Duration
so the second branch would never happen.
If that's not the case, what am I missing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Notes from call:
var value any = duration.Duration(123) // 123ns
var value any = int64(123)
--------------
if v, ok := value.(duration.Duration); ok {
// ...
}
--------------
swtich v := value.(type) {
case duration.Duration:
case int64:
default:
panic(....)
}
SyncInterval time.Duration `configKey:"interval" configUsage:"Statistics synchronization interval, from memory to the etcd." validate:"required,minDuration=100ms,maxDuration=5s"` | ||
SyncTimeout time.Duration `configKey:"timeout" configUsage:"Statistics synchronization timeout." validate:"required,minDuration=1s,maxDuration=1m"` | ||
SyncInterval duration.Duration `configKey:"interval" configUsage:"Statistics synchronization interval, from memory to the etcd." validate:"required,minDuration=100ms,maxDuration=5s"` | ||
SyncTimeout duration.Duration `configKey:"timeout" configUsage:"Statistics synchronization timeout." validate:"required,minDuration=1s,maxDuration=1m"` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder, should we disallow time.Duration in linter?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, if the struct is not used in db/api, time.Duration
is ok.
Part of: https://keboola.atlassian.net/browse/PSGO-388
Changes:
time.Duration
is by default serialized asint64
, as number of nanoseconds.time.Duration
values that need to be handled.duration.Duration
type, that serializes as string, for example:100ms
,1h0m
, ...