fabric-sdk-go: github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazyref Index | Examples | Files

package lazyref

import "github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazyref"

Index

Examples

Package Files

expiration.go lazyref.go options.go

Constants

const (
    // InitOnFirstAccess specifies that the reference should be initialized the first time it is accessed
    InitOnFirstAccess time.Duration = time.Duration(-1)

    // InitImmediately specifies that the reference should be initialized immediately after it is created
    InitImmediately time.Duration = time.Duration(0)
)

func WithAbsoluteExpiration Uses

func WithAbsoluteExpiration(value time.Duration) options.Opt

WithAbsoluteExpiration sets the expiration time for the reference. It will expire after this time period, regardless of whether or not it has been recently accessed.

func WithExpirationProvider Uses

func WithExpirationProvider(expirationProvider ExpirationProvider, expiryType ExpirationType) options.Opt

WithExpirationProvider sets the expiration provider, which determines the expiration time of the reference

func WithFinalizer Uses

func WithFinalizer(finalizer Finalizer) options.Opt

WithFinalizer sets a finalizer function that is called when the reference is closed or if it expires

func WithIdleExpiration Uses

func WithIdleExpiration(value time.Duration) options.Opt

WithIdleExpiration sets the idle-time expiration for the reference. The reference is expired after not being accessed for the given duration.

func WithRefreshInterval Uses

func WithRefreshInterval(initialInit, refreshPeriod time.Duration) options.Opt

WithRefreshInterval specifies that the reference should be proactively refreshed. Argument, initialInit, if greater than or equal to 0, indicates that the reference should be initialized after this duration. If less than 0, the reference will be initialized on first access. Argument, refreshPeriod, is the period at which the reference will be refreshed. Note that the Finalizer will not be invoked each time the value is refreshed.

type ExpirationProvider Uses

type ExpirationProvider func() time.Duration

ExpirationProvider is a function that returns the expiration time of a reference

func NewGraduatingExpirationProvider Uses

func NewGraduatingExpirationProvider(initialExpiry, increments, maxExpiry time.Duration) ExpirationProvider

NewGraduatingExpirationProvider returns an expiration provider that has an initial expiration and then expires in graduated increments with a maximum expiration time.

func NewSimpleExpirationProvider Uses

func NewSimpleExpirationProvider(expiry time.Duration) ExpirationProvider

NewSimpleExpirationProvider returns an expiration provider that sets the given expiration period

type ExpirationType Uses

type ExpirationType uint

ExpirationType indicates how to handle expiration of the reference

const (
    // LastAccessed specifies that the expiration time is calculated
    // from the last access time
    LastAccessed ExpirationType = iota

    // LastInitialized specifies that the expiration time is calculated
    // from the time the reference was initialized
    LastInitialized

    // Refreshing indicates that the reference should be periodically refreshed
    Refreshing
)

type Finalizer Uses

type Finalizer func(value interface{})

Finalizer is a function that is called when the reference is closed

type Initializer Uses

type Initializer func() (interface{}, error)

Initializer is a function that initializes the value

type InitializerWithData Uses

type InitializerWithData func(data interface{}) (interface{}, error)

InitializerWithData is a function that initializes the value using the optional data.

type Reference Uses

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

Reference holds a value that is initialized on first access using the provided Initializer function. The Reference has an optional expiring feature wherin the value is reset after the provided period of time. A subsequent call to Get or MustGet causes the Initializer function to be invoked again. The Reference also has a proactive refresh capability, in which the Initializer function is periodically called out of band (out of band means that the caller of Get or MustGet does not need to wait for the initializer function to complete: the old value will be used until the new value has finished initializing). An optional Finalizer function may be provided to be invoked whenever the Reference is closed (via a call to Close) or if it expires. (Note: The Finalizer function is not called every time the value is refreshed with the periodic refresh feature.)

Code:

ref := New(func() (interface{}, error) {
    return "Value1", nil
})
fmt.Println(ref.MustGet())

Output:

Value1

Code:

sequence := 0
ref := New(
    func() (interface{}, error) {
        sequence++
        return fmt.Sprintf("Data_%d", sequence), nil
    },
    WithIdleExpiration(200*time.Millisecond),
)

for i := 0; i < 5; i++ {
    fmt.Println(ref.MustGet())
    time.Sleep(100 * time.Millisecond)
}

This example demonstrates a refreshing reference. The reference is initialized immediately after creation and every 2 seconds thereafter.

Code:

sequence := 0
ref := New(
    func() (interface{}, error) {
        sequence++
        return fmt.Sprintf("Data_%d", sequence), nil
    },
    WithRefreshInterval(InitImmediately, 200*time.Millisecond),
)

for i := 0; i < 5; i++ {
    fmt.Println(ref.MustGet())
    time.Sleep(300 * time.Millisecond)
}

func New Uses

func New(initializer Initializer, opts ...options.Opt) *Reference

New creates a new reference

func NewWithData Uses

func NewWithData(initializer InitializerWithData, opts ...options.Opt) *Reference

NewWithData creates a new reference where data is passed from the Get function to the initializer. This is useful for refreshing the reference with dynamic data.

func (*Reference) Close Uses

func (r *Reference) Close()

Close ensures that the finalizer (if provided) is called. Close should be called for expiring references and rerences that specify finalizers.

func (*Reference) Get Uses

func (r *Reference) Get(data ...interface{}) (interface{}, error)

Get returns the value, or an error if the initialiser returned an error.

func (*Reference) IsClosed Uses

func (r *Reference) IsClosed() bool

IsClosed returns true if the referenced has been closed

func (*Reference) MustGet Uses

func (r *Reference) MustGet() interface{}

MustGet returns the value. If an error is returned during initialization of the value then this function will panic.

func (*Reference) SetAbsoluteExpiration Uses

func (p *Reference) SetAbsoluteExpiration(expiration time.Duration)

func (*Reference) SetExpirationProvider Uses

func (p *Reference) SetExpirationProvider(expirationProvider ExpirationProvider, expiryType ExpirationType)

func (*Reference) SetFinalizer Uses

func (p *Reference) SetFinalizer(value Finalizer)

func (*Reference) SetIdleExpiration Uses

func (p *Reference) SetIdleExpiration(expiration time.Duration)

func (*Reference) SetRefreshInterval Uses

func (p *Reference) SetRefreshInterval(initialInit, refreshPeriod time.Duration)

Package lazyref imports 8 packages (graph) and is imported by 8 packages. Updated 2019-01-10. Refresh now. Tools for package owners.