generator

package
v1.21.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2024 License: MIT Imports: 6 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrEmptyPool = fmt.Errorf("pool is empty")

ErrEmptyPool is returned by GetNow when the pool is empty.

Functions

This section is empty.

Types

type ParameterPool

type ParameterPool[T any] struct {
	// contains filtered or unexported fields
}

ParameterPool autogenerates parameters based on the provided generation function up to the pool size. Parameters are stored in the cache and persisted using the provided persistence layer to survive client restarts. When a parameter is pulled from the pool, the pool starts generating a new parameter automatically. The pool submits the work to the provided scheduler instance and can be controlled by the scheduler.

func NewParameterPool

func NewParameterPool[T any](
	logger log.StandardLogger,
	scheduler *Scheduler,
	persistence Persistence[T],
	poolSize int,
	generateFn func(context.Context) *T,
	generateDelay time.Duration,
) *ParameterPool[T]

NewParameterPool creates a new instance of ParameterPool. The generateFn may return nil when the context passed to it has been cancelled or timed out during computations.

func (*ParameterPool[T]) GetNow

func (pp *ParameterPool[T]) GetNow() (*T, error)

GetNow returns a new parameter from the pool. Returns ErrEmptyPool when the pool is empty.

func (*ParameterPool[T]) ParametersCount

func (pp *ParameterPool[T]) ParametersCount() int

ParametersCount returns the number of parameters in the pool.

type Persisted

type Persisted[S any] struct {
	Data S
	ID   string
}

Persisted is a wrapper for the data that are stored, it adds an identifier. The identifier can be used in `Delete` function implementation to determine which entry should be removed from the persistent storage.

type Persistence

type Persistence[T any] interface {
	Save(*T) (*Persisted[T], error)
	Delete(*Persisted[T]) error
	ReadAll() ([]*Persisted[T], error)
}

Persistence defines the expected interface for storing and loading generated and not-yet-used parameters on persistent storage. Generating parameters is usually a computationally expensive operation and generated parameters should survive client restarts.

type Protocol

type Protocol interface {
	IsExecuting() bool
}

Protocol defines the interface that allows the Scheduler to determine if the protocol is executing or not. This interface should be implemented by all important protocols of the client, such as distributed key generation or signing.

type ProtocolLatch

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

ProtocolLatch increases the internal counter every time protocol execution starts and decreases the counter every time protocol execution completes. The latch implements Protocol interface and can be registered in the Scheduler.

The protocol code using the latch must guarantee that: 1. `Lock()` is always called before `Unlock()` 2. `Unlock()` is eventually called for every `Lock()`.

Note that the Unlock() function may panic if the conditions are not met.

func NewProtocolLatch

func NewProtocolLatch() *ProtocolLatch

NewProtocolLatch returns a new instance of the latch with 0 counter value.

func (*ProtocolLatch) IsExecuting

func (pl *ProtocolLatch) IsExecuting() bool

IsExecuting returns true if the latch counter is 0. This is happening when the same number of Unlock and Lock happened.

func (*ProtocolLatch) Lock

func (pl *ProtocolLatch) Lock()

Lock increases the counter on the latch by one.

func (*ProtocolLatch) Unlock

func (pl *ProtocolLatch) Unlock()

Unlock decreases the counter on the latch by one. Unlock panics if no Lock was called before.

type Scheduler

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

Scheduler allows managing computationally heavy operations: stopping and resuming them. The client needs to generate parameters for cryptographic algorithms and generating these parameters requires a lot of CPU cycles. The generation process may starve other processes in the client when it comes to access to the CPU. The scheduler allows starting the parameter generation when no other processes such as key generation or signing are executed on the client. This way, the client that would normally be idle, can spend CPU cycles on computationally heavy operations and stop these operations when CPU cycles are needed elsewhere.

func StartScheduler

func StartScheduler() *Scheduler

StartScheduler creates a new instance of a Scheduler that is responsible for managing long-running, computationally-expensive operations. The scheduler stops and resumes operations based on the state of registered protocols. If at least one of the protocols is currently executing, the scheduler stops all computations. Computations are automatically resumed once none of the protocols is executing.

func (*Scheduler) RegisterProtocol

func (s *Scheduler) RegisterProtocol(protocol Protocol)

RegisterProtocol adds the provided protocol to the list that will be inspected periodically by the Scheduler. The Scheduler checks if at least one of the registered protocols is running and if so, it stops the computations. Computations are automatically resumed once none of the protocols is executing. If there are no protocols registered, the scheduler continues to work.

Jump to

Keyboard shortcuts

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