forked from hyperledger/fabric-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
133 lines (111 loc) · 4.15 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package lazyref
import (
"time"
"github.com/hyperledger/fabric-sdk-go/pkg/common/options"
)
// WithIdleExpiration sets the idle-time expiration for the reference.
// The reference is expired after not being accessed for the given duration.
func WithIdleExpiration(value time.Duration) options.Opt {
return func(p options.Params) {
logger.Debug("Checking idleExpirationSetter")
if setter, ok := p.(idleExpirationSetter); ok {
setter.SetIdleExpiration(value)
}
}
}
// 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 WithAbsoluteExpiration(value time.Duration) options.Opt {
return func(p options.Params) {
logger.Debug("Checking absoluteExpirationSetter")
if setter, ok := p.(absoluteExpirationSetter); ok {
setter.SetAbsoluteExpiration(value)
}
}
}
// WithExpirationProvider sets the expiration provider, which determines
// the expiration time of the reference
func WithExpirationProvider(expirationProvider ExpirationProvider, expiryType ExpirationType) options.Opt {
return func(p options.Params) {
logger.Debug("Checking expirationProviderSetter")
if setter, ok := p.(expirationProviderSetter); ok {
setter.SetExpirationProvider(expirationProvider, expiryType)
}
}
}
// WithFinalizer sets a finalizer function that is called when the
// reference is closed or if it expires
func WithFinalizer(finalizer Finalizer) options.Opt {
return func(p options.Params) {
logger.Debug("Checking finalizerSetter")
if setter, ok := p.(finalizerSetter); ok {
setter.SetFinalizer(finalizer)
}
}
}
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)
)
// 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.
func WithRefreshInterval(initialInit, refreshPeriod time.Duration) options.Opt {
return func(p options.Params) {
logger.Debug("Checking refreshIntervalSetter")
if setter, ok := p.(refreshIntervalSetter); ok {
setter.SetRefreshInterval(initialInit, refreshPeriod)
}
}
}
type idleExpirationSetter interface {
SetIdleExpiration(expiration time.Duration)
}
type absoluteExpirationSetter interface {
SetAbsoluteExpiration(expiration time.Duration)
}
type expirationProviderSetter interface {
SetExpirationProvider(expirationProvider ExpirationProvider, expiryType ExpirationType)
}
type finalizerSetter interface {
SetFinalizer(value Finalizer)
}
type refreshIntervalSetter interface {
SetRefreshInterval(initialInit, refreshPeriod time.Duration)
}
type params struct {
initialInit time.Duration
finalizer Finalizer
expirationProvider ExpirationProvider
expiryType ExpirationType
}
func (p *params) SetIdleExpiration(expiration time.Duration) {
p.expirationProvider = NewSimpleExpirationProvider(expiration)
p.expiryType = LastAccessed
}
func (p *params) SetAbsoluteExpiration(expiration time.Duration) {
p.expirationProvider = NewSimpleExpirationProvider(expiration)
p.expiryType = LastInitialized
}
func (p *params) SetExpirationProvider(expirationProvider ExpirationProvider, expiryType ExpirationType) {
p.expirationProvider = expirationProvider
p.expiryType = expiryType
}
func (p *params) SetFinalizer(value Finalizer) {
p.finalizer = value
}
func (p *params) SetRefreshInterval(initialInit, refreshPeriod time.Duration) {
p.expiryType = Refreshing
p.expirationProvider = NewSimpleExpirationProvider(refreshPeriod)
p.initialInit = initialInit
}