noncegen

package
v0.0.0-...-eb215bf Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

This package provides an interface and several implementations for a nonce generator.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HFNonceGenerator

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

A thread-safe nonce generator with no collision risk when used at high frequency. The nonce generator generate nonce from two numbers that are added:

  • base: The UNIX nanosec timestamp of the moment when the generator has been created. This ensures generated nonce will always increase even in case of application restart (without a persistence layer).
  • inc: An atomic counter which increases each time a nonce is generated. This ensures the generator thread-safety.

WARNING: The nonce generator has no risk of collision in case of usage in a monolithic system only. In case several applications need to access Kraken API, use different API keys for each application.

func NewHFNonceGenerator

func NewHFNonceGenerator() *HFNonceGenerator

Factory which returns a new ready-to-use HFNonceGenerator.

func (*HFNonceGenerator) GenerateNonce

func (g *HFNonceGenerator) GenerateNonce() int64

Generate a new nonce.

type MockNonceGenerator

type MockNonceGenerator struct {
	mock.Mock
}

A mock for NonceGenerator interface

func NewMockNonceGenerator

func NewMockNonceGenerator() *MockNonceGenerator

Factory which creates a new MockNonceGenerator without any expectations set.

func (*MockNonceGenerator) GenerateNonce

func (m *MockNonceGenerator) GenerateNonce() int64

Mocked GenerateNonce method

type NonceGenerator

type NonceGenerator interface {
	// Generate a new nonce.
	GenerateNonce() int64
}

Interface which defines a method to get a unique incrementing nonce which will be used to sign a request to Kraken API.

type UnixMillisNonceGenerator

type UnixMillisNonceGenerator struct{}

Nonce generator which returns UNIX millisecond timestamps as nonces.

This nonce generator has a low risk of nonce collision / bad nonce error in case of use at high frequency or in a distributed system.

func NewUnixMillisNonceGenerator

func NewUnixMillisNonceGenerator() *UnixMillisNonceGenerator

Factory which returns a new UnixMillisNonceGenerator.

func (*UnixMillisNonceGenerator) GenerateNonce

func (g *UnixMillisNonceGenerator) GenerateNonce() int64

Generate a new nonce that is a UNIX millisecond timestamp.

Jump to

Keyboard shortcuts

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