/
closeable.go
158 lines (139 loc) · 3.01 KB
/
closeable.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package util
import (
"os"
"os/signal"
"sync"
"sync/atomic"
"syscall"
"time"
"github.com/emirpasic/gods/queues/linkedlistqueue"
)
type CloseableChannel[T any] struct {
ch chan T
close atomic.Bool
mu sync.RWMutex
queue *linkedlistqueue.Queue
}
// Channel returns the channel of the CloseableChannel.
func (c *CloseableChannel[T]) Channel() <-chan T { return c.ch }
func (c *CloseableChannel[T]) Close() {
c.LockWrapFunc(func() {
c.close.Store(true)
close(c.ch)
})
}
// Dequeue removes a value from the front of the queue
func (c *CloseableChannel[T]) Dequeue() (val T, ok bool) {
c.LockWrapFunc(func() {
if c.queue.Empty() {
return
}
var _val interface{}
_val, ok = c.queue.Dequeue()
if !ok {
return
}
val = _val.(T)
})
return
}
// Enqueue adds a value to the end of the queue
func (c *CloseableChannel[T]) Enqueue(value T) {
c.LockWrapFunc(func() { c.queue.Enqueue(value) })
}
func (c *CloseableChannel[T]) IsClosed() bool { return c.close.Load() }
func (c *CloseableChannel[T]) LockWrapFunc(fn func()) {
if c.IsClosed() {
return
}
c.mu.Lock()
if c.IsClosed() {
c.mu.Unlock()
return
}
fn()
c.mu.Unlock()
}
func (c *CloseableChannel[T]) RLockWrapFunc(fn func()) {
if c.IsClosed() {
return
}
c.mu.RLock()
if c.IsClosed() {
c.mu.RUnlock()
return
}
fn()
c.mu.RUnlock()
}
func (c *CloseableChannel[T]) Send(val T) { c.Enqueue(val) }
func (c *CloseableChannel[T]) SendWithDefaultTimeout(val T) { c.SendWithTimeout(val, 0) }
// SendWithTimeout send val to the channel with timeout
func (c *CloseableChannel[T]) SendWithTimeout(val T, timeout time.Duration) {
c.RLockWrapFunc(func() { c.sendWithTimeout(val, timeout) })
}
func (c *CloseableChannel[T]) dequeueSend() (ok bool) {
c.LockWrapFunc(func() {
if c.queue.Empty() {
return
}
var _val interface{}
_val, ok = c.queue.Dequeue()
if !ok {
return
}
c.sendWithTimeout(_val.(T), 0)
})
return
}
// runBackgroundLoop runs a background loop that will send all values
// that are enqueued to the channel
// if the channel is closed, the loop will exit
// if enqueue is empty, the loop will wait for a value to be enqueued
func (c *CloseableChannel[T]) runBackgroundLoop() {
for {
if c.IsClosed() {
return
}
ok := c.dequeueSend()
if !ok {
time.Sleep(time.Millisecond * 100)
}
}
}
func (c *CloseableChannel[T]) sendWithTimeout(val T, timeout time.Duration) {
if timeout < 0 {
c.ch <- val
return
}
if timeout < time.Millisecond*100 {
timeout = time.Millisecond * 100
}
select {
case c.ch <- val:
case <-time.After(timeout):
break
}
}
type CloseableType interface {
IsClosed() bool
Close()
Send(val interface{})
Channel() <-chan interface{}
}
func NewCloseable[T any](ch chan T) *CloseableChannel[T] {
c := &CloseableChannel[T]{ch: ch, queue: linkedlistqueue.New()}
go c.runBackgroundLoop()
return c
}
func WaitSignal() {
notifier := make(chan os.Signal, 1)
signal.Notify(notifier,
os.Interrupt,
syscall.SIGTERM,
syscall.SIGINT,
syscall.SIGQUIT,
syscall.SIGTSTP,
)
<-notifier
}