peersync

package
v1.2.12 Latest Latest
Warning

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

Go to latest
Published: Dec 21, 2023 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

nolint

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPeersNotSynced returned if system clock is out of sync with peers clock for configured period of time.
	ErrPeersNotSynced = errors.New("timesync: peers are not time synced")
	// ErrTimesyncFailed returned if we weren't able to collect enough clock samples from peers.
	ErrTimesyncFailed = errors.New("timesync: failed request")
)

Functions

This section is empty.

Types

type Config

type Config struct {
	Disable            bool          `mapstructure:"disable"`
	RoundRetryInterval time.Duration `mapstructure:"round-retry-interval"`
	RoundInterval      time.Duration `mapstructure:"round-interval"`
	RoundTimeout       time.Duration `mapstructure:"round-timeout"`
	MaxClockOffset     time.Duration `mapstructure:"max-clock-offset"`
	MaxOffsetErrors    int           `mapstructure:"max-offset-errors"`
	RequiredResponses  int           `mapstructure:"required-responses"`
}

Config for Sync.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig for Sync.

type Option

type Option func(*Sync)

Option to modify Sync behavior.

func WithConfig

func WithConfig(config Config) Option

WithConfig modifies config used in Sync.

func WithContext

func WithContext(ctx context.Context) Option

WithContext modifies parent context that is used for all operations in Sync.

func WithLog

func WithLog(lg log.Log) Option

WithLog modifies Log used in Sync.

func WithTime

func WithTime(t Time) Option

WithTime modifies source of time used in Sync.

type Request

type Request struct {
	ID uint64
}

Request is a sync request.

func (*Request) DecodeScale

func (t *Request) DecodeScale(dec *scale.Decoder) (total int, err error)

func (*Request) EncodeScale

func (t *Request) EncodeScale(enc *scale.Encoder) (total int, err error)

type Response

type Response struct {
	ID        uint64
	Timestamp uint64
}

Response is a sync response.

func (*Response) DecodeScale

func (t *Response) DecodeScale(dec *scale.Decoder) (total int, err error)

func (*Response) EncodeScale

func (t *Response) EncodeScale(enc *scale.Encoder) (total int, err error)

type Sync

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

Sync manages background worker that compares peers time with system time.

func New

func New(h host.Host, peers getPeers, opts ...Option) *Sync

New creates Sync instance and returns pointer.

func (*Sync) GetOffset

func (s *Sync) GetOffset(ctx context.Context, id uint64, prs []p2p.Peer) (time.Duration, error)

GetOffset computes offset from received response. The method is stateless and safe to use concurrently.

func (*Sync) Start

func (s *Sync) Start()

Start background workers.

func (*Sync) Stop

func (s *Sync) Stop()

Stop background workers.

func (*Sync) Wait

func (s *Sync) Wait() error

Wait will return first error that is returned by background workers.

type Time

type Time interface {
	Now() time.Time
}

Time provides interface for current time.

Directories

Path Synopsis
Code generated by MockGen.
Code generated by MockGen.

Jump to

Keyboard shortcuts

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