entp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2025 License: MIT Imports: 4 Imported by: 0

README

entp (enhanced NTP)

Библиотека и утилита для получения точного времени через NTP (Network Time Protocol).

Основана на beevik/ntp, добавляет удобный клиент с поддержкой списка серверов, таймаутов и контекста.


Установка

Модуль импортируется напрямую:

go get github.com/DenisBochko/entp
В коде:
import "github.com/DenisBochko/entp"

Использование в коде

Простейший пример:
package main

import (
	"context"
	"fmt"
	"log"

	"github.com/DenisBochko/entp"
)

func main() {
	client := entp.NewClient()

	t, err := client.Now(context.Background())
	if err != nil {
		log.Fatalf("failed to get NTP time: %v", err)
	}

	fmt.Println("Exact time:", t)
}
С опциями:
client := entp.NewClient(
    entp.WithTimeout(2 * time.Second),
    entp.WithAddServers("time.cloudflare.com"),
    entp.WithReplaceDefaultServers("xk.pool.ntp.org")
)

Запуск линтера

Проект использует golangci-lint

Установка:
go install github.com/golangci/golangci-lint/cmd/golangci-lint@latest
Запуск:
golangci-lint run ./... --config=./.golangci-lint.yml

Запуск тестов

Обычные тесты:
go test ./...
С выводом покрытия:
go test -cover ./...

P.S. Чтобы тесты правильно работали, необходимо выключить vpn

CLI пример:

go run ./cmd/main.go

Documentation

Overview

Package entp provides NTP-based current time retrieval.

Index

Constants

This section is empty.

Variables

View Source
var ErrAllServersUnavailable = errors.New("all NTP servers are unavailable")

ErrAllServersUnavailable is returned when no NTP server responded successfully.

Functions

This section is empty.

Types

type Client

type Client struct {
	Servers []string
	Timeout time.Duration
}

Client holds NTP configuration.

func NewClient

func NewClient(opts ...Option) *Client

NewClient creates a Client with options.

func (*Client) Now

func (c *Client) Now(ctx context.Context) (time.Time, error)

Now returns the current time using the first responding NTP server. It respects Client.Timeout and an optional ctx deadline (the tighter wins).

type Option

type Option func(*Client)

Option configures Client.

func WithAddServers

func WithAddServers(servers ...string) Option

WithAddServers add "servers" to the end of the list of available default servers.

func WithReplaceDefaultServers

func WithReplaceDefaultServers(servers ...string) Option

WithReplaceDefaultServers replaces the list of default servers with "servers" completely.

func WithTimeout

func WithTimeout(timeout time.Duration) Option

WithTimeout sets the per-request timeout.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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