ntp_time_provider

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2026 License: MIT Imports: 6 Imported by: 2 modules, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

NTP Time Provider

一、这是什么

基于NTP的TimeProvider实现,Storage的具体实现可以引入这个库来实现GetTime方法,让分布式系统中的各个角色使用统一的NTP时间源。

二、安装依赖

go get -u github.com/storage-lock/go-ntp-time-provider

三、API示例

package main

import (
	"context"
	"fmt"
	"github.com/storage-lock/go-events"
	ntp_time_provider "github.com/storage-lock/go-ntp-time-provider"
)

func main() {
	
	event := events.NewEvent("test")
	timeProvider := ntp_time_provider.NewNTPTimeProvider(event)
	time, err := timeProvider.GetTime(context.Background())
	if err != nil {
		panic(err)
	}
	fmt.Println(time)
	// Output:
	// 2023-08-07 01:36:18.29416265 +0800 CST m=-1.335806449

}

Documentation

Index

Constants

View Source
const (
	ActionNtpSuccess = "ntp-success"
	ActionNtpError   = "ntp-error"
	ActionNtpZero    = "ntp-zero"
)

time provider

Variables

View Source
var DefaultNtpServers = []string{
	"time.windows.com",
	"time.nist.gov",
	"ntp.ntsc.ac.cn",
	"ntp.aliyun.com",
	"time1.cloud.tencent.com",
	"time2.cloud.tencent.com",
	"time3.cloud.tencent.com",
	"time4.cloud.tencent.com",
	"time5.cloud.tencent.com",
}

DefaultNtpServers 默认的NTP服务器,使用者可以在系统初始化的覆盖掉这个变量来设置默认的NTP服务器

View Source
var (
	// ErrTimeProviderUnavailable TimeProvider无法提供时间
	ErrTimeProviderUnavailable = errors.New("time provider unavailable")
)

Functions

func GetDefaultNtpServers

func GetDefaultNtpServers() []string

func SetDefaultNtpServers

func SetDefaultNtpServers(defaultNtpServers []string)

SetDefaultNtpServers 或者调用这个方法来设置默认的NTP服务器

Types

type NTPTimeProvider

type NTPTimeProvider struct {
	// contains filtered or unexported fields
}

NTPTimeProvider 基于NTP的时间源实现

func NewNTPTimeProvider

func NewNTPTimeProvider(e *events.Event, ntpServers ...string) *NTPTimeProvider

NewNTPTimeProvider 如果是在云环境内网的话,手动指定一个内网的ntp服务器速度会更快,云服务商一般都会提供内网的ntp服务器

func (*NTPTimeProvider) GetTime

func (x *NTPTimeProvider) GetTime(ctx context.Context) (time.Time, error)

GetTime 从NTP获取时间,当不方便从Storage获取时间的时候可以使用NTP作为时间源

漏洞修复:原实现完全忽略传入的 ctx,依次查询 DefaultNtpServers(默认 9 个), 每个失败都要等 ntp 库自身的 5s 超时才切换下一个,最坏情况一次 GetTime 阻塞 近 45s,调用方即使传入带超时/取消的 ctx 也完全无法提前中断。现在每个 server 的查询放到独立 goroutine 执行,用 select 监听 ctx.Done(),一旦 ctx 被取消/超时 立即返回,不再傻等剩余服务器逐个超时。

func (*NTPTimeProvider) SetEvent

func (x *NTPTimeProvider) SetEvent(e *events.Event) *NTPTimeProvider

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址