-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
Use monotonic time in lease #6888
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
// Copyright (C) 2016 Arista Networks, Inc. | ||
// Use of this source code is governed by the Apache License 2.0 | ||
// that can be found in the COPYING file. | ||
|
||
// This file is intentionally empty. | ||
// It's a workaround for https://github.com/golang/go/issues/15006 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Copyright 2016 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package monotime | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Time represents a point in monotonic time | ||
type Time uint64 | ||
|
||
func (t Time) Add(d time.Duration) Time { | ||
return Time(uint64(t) + uint64(d.Nanoseconds())) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright (C) 2016 Arista Networks, Inc. | ||
// Use of this source code is governed by the Apache License 2.0 | ||
// that can be found in the COPYING file. | ||
|
||
// Package monotime provides a fast monotonic clock source. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be // Package monotime provides a fast monotonic clock source.
package monotime Otherwise godoc won't catch it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
package monotime | ||
|
||
import ( | ||
_ "unsafe" // required to use //go:linkname | ||
) | ||
|
||
//go:noescape | ||
//go:linkname nanotime runtime.nanotime | ||
func nanotime() int64 | ||
|
||
// Now returns the current time in nanoseconds from a monotonic clock. | ||
// The time returned is based on some arbitrary platform-specific point in the | ||
// past. The time returned is guaranteed to increase monotonically at a | ||
// constant rate, unlike time.Now() from the Go standard library, which may | ||
// slow down, speed up, jump forward or backward, due to NTP activity or leap | ||
// seconds. | ||
func Now() Time { | ||
return Time(nanotime()) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (C) 2016 Arista Networks, Inc. | ||
// Use of this source code is governed by the Apache License 2.0 | ||
|
||
// Package monotime provides a fast monotonic clock source. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this comment
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And this description is duplicate? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will remove There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
package monotime | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestNow(t *testing.T) { | ||
for i := 0; i < 100; i++ { | ||
t1 := Now() | ||
t2 := Now() | ||
// I honestly thought that we needed >= here, but in some environments | ||
// two consecutive calls can return the same value! | ||
if t1 > t2 { | ||
t.Fatalf("t1=%d should have been less than or equal to t2=%d", t1, t2) | ||
} | ||
} | ||
} |
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.
add blank line above? Otherwise it will break godoc?
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.
fixed
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 still see
?
Can you double-check?
Thanks.
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.
It should be
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.
It's fixed now