Skip to content

Commit 230fc61

Browse files
committed
feat(clock): add ntp clock
1 parent 4a27a77 commit 230fc61

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

clock/ntp.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package clock
2+
3+
import (
4+
"fmt"
5+
"time"
6+
7+
"github.com/beevik/ntp"
8+
"github.com/gotd/td/clock"
9+
)
10+
11+
var _ clock.Clock = (*ntpClock)(nil)
12+
13+
const defaultNTP = "pool.ntp.org"
14+
15+
type ntpClock struct {
16+
offset time.Duration
17+
}
18+
19+
func (n *ntpClock) Now() time.Time {
20+
return time.Now().Add(n.offset)
21+
}
22+
23+
func (n *ntpClock) Timer(d time.Duration) clock.Timer {
24+
return clock.System.Timer(d)
25+
}
26+
27+
func (n *ntpClock) Ticker(d time.Duration) clock.Ticker {
28+
return clock.System.Ticker(d)
29+
}
30+
31+
// NewNTP creates new NTP clock.
32+
func NewNTP(ntpHost ...string) (clock.Clock, error) {
33+
var host string
34+
switch len(ntpHost) {
35+
case 0:
36+
host = defaultNTP
37+
case 1:
38+
host = ntpHost[0]
39+
default:
40+
return nil, fmt.Errorf("too many ntp hosts")
41+
}
42+
43+
resp, err := ntp.Query(host)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return &ntpClock{
49+
offset: resp.ClockOffset,
50+
}, nil
51+
}

clock/ntp_example_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package clock_test
2+
3+
import (
4+
"log"
5+
6+
"github.com/gotd/contrib/clock"
7+
"github.com/gotd/td/telegram"
8+
)
9+
10+
func ExampleNewNTP() {
11+
c, err := clock.NewNTP() // or clock.NewNTP("my.ntp.host")
12+
if err != nil {
13+
log.Fatal(err)
14+
}
15+
16+
client, err := telegram.ClientFromEnvironment(telegram.Options{
17+
Clock: c,
18+
})
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
_ = client
24+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module github.com/gotd/contrib
33
go 1.18
44

55
require (
6+
github.com/beevik/ntp v0.3.0
67
github.com/cenkalti/backoff/v4 v4.1.3
78
github.com/cockroachdb/pebble v0.0.0-20220107203702-aa376a819bf6
89
github.com/gen2brain/dlgs v0.0.0-20211108104213-bade24837f0b

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj
5656
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
5757
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
5858
github.com/aymerick/raymond v2.0.3-0.20180322193309-b565731e1464+incompatible/go.mod h1:osfaiScAUVup+UC9Nfq76eWqDhXlp+4UYaA8uhTBO6g=
59+
github.com/beevik/ntp v0.3.0 h1:xzVrPrE4ziasFXgBVBZJDP0Wg/KpMwk2KHJ4Ba8GrDw=
60+
github.com/beevik/ntp v0.3.0/go.mod h1:hIHWr+l3+/clUnF44zdK+CWW7fO8dR5cIylAQ76NRpg=
5961
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
6062
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
6163
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=

0 commit comments

Comments
 (0)