/
types.go
112 lines (99 loc) · 3.35 KB
/
types.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
package types
import (
"sync"
"sync/atomic"
"go.uber.org/zap"
)
// LogInterface inherited by internal packages to provide hierarchical logs
type LogInterface struct {
Prod *zap.Logger
Dev *zap.Logger
}
// Default configs
const (
DefaultKeepAlive = 60 // DefaultKeepAlive default keep
DefaultConnectTimeout = 5 // DefaultConnectTimeout connect timeout
DefaultMaxPacketSize = 268435455
DefaultReceiveMax = 65535
DefaultAckTimeout = 20 // DefaultAckTimeout ack timeout
DefaultTimeoutRetries = 3 // DefaultTimeoutRetries retries
MinKeepAlive = 30
DefaultSessionsProvider = "mem" // DefaultSessionsProvider default session provider
DefaultAuthenticator = "mockSuccess" // DefaultAuthenticator default auth provider
DefaultTopicsProvider = "mem" // DefaultTopicsProvider default topics provider
)
// RetainObject general interface of the retain as not only publish message can be retained
type RetainObject interface {
Topic() string
}
// TopicMessenger interface for session or systree used to publish or retain messages
type TopicMessenger interface {
Publish(interface{}) error
Retain(RetainObject) error
}
// OnceWait is an object that will perform exactly one action.
type OnceWait struct {
val uintptr
wait sync.WaitGroup
lock sync.Mutex
}
// Once is an object that will perform exactly one action.
type Once struct {
val uintptr
}
// Do calls the function f if and only if Do is being called for the
// first time for this instance of Once. In other words, given
// var once Once
// if once.Do(f) is called multiple times, only the first call will invoke f,
// even if f has a different value in each invocation. A new instance of
// Once is required for each function to execute.
//
// Do is intended for initialization that must be run exactly once. Since f
// is niladic, it may be necessary to use a function literal to capture the
// arguments to a function to be invoked by Do:
// config.once.Do(func() { config.init(filename) })
//
// Because no call to Do returns until the one call to f returns, if f causes
// Do to be called, it will deadlock.
//
// If f panics, Do considers it to have returned; future calls of Do return
// without calling f.
func (o *OnceWait) Do(f func()) bool {
o.lock.Lock()
res := atomic.CompareAndSwapUintptr(&o.val, 0, 1)
if res {
o.wait.Add(1)
}
o.lock.Unlock()
if res {
f()
o.wait.Done()
} else {
o.wait.Wait()
}
return res
}
// Do calls the function f if and only if Do is being called for the
// first time for this instance of Once. In other words, given
// var once Once
// if once.Do(f) is called multiple times, only the first call will invoke f,
// even if f has a different value in each invocation. A new instance of
// Once is required for each function to execute.
//
// Do is intended for initialization that must be run exactly once. Since f
// is niladic, it may be necessary to use a function literal to capture the
// arguments to a function to be invoked by Do:
// config.once.Do(func() { config.init(filename) })
//
// Because no call to Do returns until the one call to f returns, if f causes
// Do to be called, it will deadlock.
//
// If f panics, Do considers it to have returned; future calls of Do return
// without calling f.
func (o *Once) Do(f func()) bool {
if atomic.CompareAndSwapUintptr(&o.val, 0, 1) {
f()
return true
}
return false
}