Add sync => go-sync
This commit is contained in:
18
sync/time/local/local.go
Normal file
18
sync/time/local/local.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Package local provides a local clock
|
||||
package local
|
||||
|
||||
import (
|
||||
gotime "time"
|
||||
|
||||
"github.com/micro/go-micro/sync/time"
|
||||
)
|
||||
|
||||
type Time struct{}
|
||||
|
||||
func (t *Time) Now() (gotime.Time, error) {
|
||||
return gotime.Now(), nil
|
||||
}
|
||||
|
||||
func NewTime(opts ...time.Option) time.Time {
|
||||
return new(Time)
|
||||
}
|
||||
51
sync/time/ntp/ntp.go
Normal file
51
sync/time/ntp/ntp.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Package ntp provides ntp synchronized time
|
||||
package ntp
|
||||
|
||||
import (
|
||||
"context"
|
||||
gotime "time"
|
||||
|
||||
"github.com/beevik/ntp"
|
||||
"github.com/micro/go-micro/sync/time"
|
||||
)
|
||||
|
||||
type ntpTime struct {
|
||||
server string
|
||||
}
|
||||
|
||||
type ntpServerKey struct{}
|
||||
|
||||
func (n *ntpTime) Now() (gotime.Time, error) {
|
||||
return ntp.Time(n.server)
|
||||
}
|
||||
|
||||
// NewTime returns ntp time
|
||||
func NewTime(opts ...time.Option) time.Time {
|
||||
options := time.Options{
|
||||
Context: context.Background(),
|
||||
}
|
||||
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
|
||||
server := "time.google.com"
|
||||
|
||||
if k, ok := options.Context.Value(ntpServerKey{}).(string); ok {
|
||||
server = k
|
||||
}
|
||||
|
||||
return &ntpTime{
|
||||
server: server,
|
||||
}
|
||||
}
|
||||
|
||||
// WithServer sets the ntp server
|
||||
func WithServer(s string) time.Option {
|
||||
return func(o *time.Options) {
|
||||
if o.Context == nil {
|
||||
o.Context = context.Background()
|
||||
}
|
||||
o.Context = context.WithValue(o.Context, ntpServerKey{}, s)
|
||||
}
|
||||
}
|
||||
18
sync/time/time.go
Normal file
18
sync/time/time.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Package time provides clock synchronization
|
||||
package time
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Time returns synchronized time
|
||||
type Time interface {
|
||||
Now() (time.Time, error)
|
||||
}
|
||||
|
||||
type Options struct {
|
||||
Context context.Context
|
||||
}
|
||||
|
||||
type Option func(o *Options)
|
||||
Reference in New Issue
Block a user