lib/flagutil: add Duration.Milliseconds() convenience function after 0c7d46d637

This function is a faster replacement for Duration.Duration().Milliseconds() call
This commit is contained in:
Aliaksandr Valialkin 2023-09-03 10:51:29 +02:00
parent 0c7d46d637
commit 24d61bf193
No known key found for this signature in database
GPG key ID: A72BEC6CD3D0DED1
3 changed files with 9 additions and 4 deletions

View file

@ -75,7 +75,7 @@ func CheckTimeRange(tr storage.TimeRange) error {
if !*denyQueriesOutsideRetention { if !*denyQueriesOutsideRetention {
return nil return nil
} }
minAllowedTimestamp := int64(fasttime.UnixTimestamp()*1000) - retentionPeriod.Duration().Milliseconds() minAllowedTimestamp := int64(fasttime.UnixTimestamp()*1000) - retentionPeriod.Milliseconds()
if tr.MinTimestamp > minAllowedTimestamp { if tr.MinTimestamp > minAllowedTimestamp {
return nil return nil
} }

View file

@ -31,11 +31,16 @@ type Duration struct {
valueString string valueString string
} }
// Duration convert to time.Duration. // Duration returns d as time.Duration
func (d *Duration) Duration() time.Duration { func (d *Duration) Duration() time.Duration {
return time.Millisecond * time.Duration(d.msecs) return time.Millisecond * time.Duration(d.msecs)
} }
// Milliseconds returns d in milliseconds
func (d *Duration) Milliseconds() int64 {
return d.msecs
}
// String implements flag.Value interface // String implements flag.Value interface
func (d *Duration) String() string { func (d *Duration) String() string {
return d.valueString return d.valueString

View file

@ -42,8 +42,8 @@ func TestDurationSetSuccess(t *testing.T) {
if err := d.Set(value); err != nil { if err := d.Set(value); err != nil {
t.Fatalf("unexpected error in d.Set(%q): %s", value, err) t.Fatalf("unexpected error in d.Set(%q): %s", value, err)
} }
if d.msecs != expectedMsecs { if d.Milliseconds() != expectedMsecs {
t.Fatalf("unexpected result; got %d; want %d", d.msecs, expectedMsecs) t.Fatalf("unexpected result; got %d; want %d", d.Milliseconds(), expectedMsecs)
} }
valueString := d.String() valueString := d.String()
valueExpected := strings.ToLower(value) valueExpected := strings.ToLower(value)