-
Notifications
You must be signed in to change notification settings - Fork 61
/
subscriber.go
234 lines (206 loc) · 4.89 KB
/
subscriber.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
//go:build !plan9
// +build !plan9
package firebirdsql
import (
"encoding/binary"
"fmt"
"net/netip"
"reflect"
"sync"
"sync/atomic"
"syscall"
)
type Subscription struct {
mu sync.RWMutex
revent *remoteEvent
auxHandle int32
callback EventHandler
chEvent chan Event
eventCounts chan Event
closed int32
muClose sync.Mutex
closes []chan error
closer sync.Once
chDoneEvent chan *Subscription
doneSubscription chan struct{}
manager *eventManager
fc *firebirdsqlConn
noNotify int32
}
func newSubscription(dsn *firebirdDsn, events []string, cb EventHandler, chEvent chan Event, chDoneEvent chan *Subscription) (*Subscription, error) {
fc, err := newFirebirdsqlConn(dsn)
if err != nil {
return nil, err
}
newSubscription := &Subscription{
fc: fc,
callback: cb,
chEvent: chEvent,
eventCounts: make(chan Event),
doneSubscription: make(chan struct{}),
chDoneEvent: chDoneEvent,
}
manager, err := newSubscription.getEventManager()
if err != nil {
return nil, err
}
newSubscription.manager = manager
remoteEvent := newRemoteEvent()
if err := remoteEvent.queueEvents(events...); err != nil {
return nil, err
}
newSubscription.revent = remoteEvent
newSubscription.queueEvents(0)
chErrManager := manager.wait(remoteEvent, newSubscription.eventCounts)
go newSubscription.wait(chErrManager)
return newSubscription, nil
}
func (s *Subscription) cancelEvents() error {
if atomic.LoadInt32(&s.closed) == 0 {
return nil
}
id := atomic.LoadInt32(&s.revent.id)
s.mu.Lock()
s.fc.wp.opCancelEvents(id)
_, _, _, err := s.fc.wp.opResponse()
s.mu.Unlock()
if err != nil {
return err
}
s.revent.cancelEvents()
return nil
}
func (s *Subscription) queueEvents(eventID int32) error {
s.mu.Lock()
defer s.mu.Unlock()
id := eventID + 1
epbData := s.revent.buildEpb()
s.fc.wp.opQueEvents(s.auxHandle, epbData, id)
rid, _, _, err := s.fc.wp.opResponse()
if err != nil {
return err
}
atomic.StoreInt32(&s.revent.id, id)
atomic.StoreInt32(&s.revent.rid, rid)
return nil
}
func (s *Subscription) getEventManager() (*eventManager, error) {
auxHandle, addrPort, err := s.connAuxRequest()
if err != nil {
return nil, err
}
newManager, err := newEventManager(addrPort.String(), auxHandle)
if err != nil {
return nil, err
}
s.auxHandle = auxHandle
return newManager, nil
}
func (s *Subscription) wait(chErrManager <-chan error) {
for {
select {
case event := <-s.eventCounts:
s.doEventCounts(event)
s.queueEvents(event.ID)
case <-s.doneSubscription:
return
case err := <-chErrManager:
s.closeWithError(err)
}
}
}
func (s *Subscription) doEventCounts(e Event) {
if s.callback != nil {
go s.callback(e)
return
}
s.chEvent <- e
}
func (s *Subscription) Unsubscribe() error {
if s.IsClose() {
return nil
}
if s.manager != nil {
if err := s.manager.close(); err != nil {
return err
}
s.manager = nil
}
if err := s.cancelEvents(); err != nil {
return err
}
return s.Close()
}
func (s *Subscription) unsubscribeNoNotify() error {
atomic.StoreInt32(&s.noNotify, 1)
return s.Unsubscribe()
}
func (s *Subscription) connAuxRequest() (int32, *netip.AddrPort, error) {
s.mu.Lock()
defer s.mu.Unlock()
s.fc.wp.opConnectRequest()
auxHandle, _, buf, err := s.fc.wp.opResponse()
if err != nil {
return -1, nil, err
}
family := bytes_to_int16(buf[0:2])
port := binary.BigEndian.Uint16(buf[2:4])
var addr netip.Addr
if family == syscall.AF_INET {
addr = netip.AddrFrom4([4]byte(buf[4:8]))
} else if family == syscall.AF_INET6 {
if reflect.DeepEqual(buf[4:20], []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}) {
addr = netip.AddrFrom4([4]byte(buf[20:24]))
} else {
addr = netip.AddrFrom16([16]byte(buf[4:20]))
}
} else {
err = fmt.Errorf("unsupported family protocol: %x", family)
return -1, nil, err
}
addrPort := netip.AddrPortFrom(addr, port)
return auxHandle, &addrPort, nil
}
func (s *Subscription) NotifyClose(receiver chan error) {
s.muClose.Lock()
defer s.muClose.Unlock()
s.closes = append(s.closes, receiver)
}
func (s *Subscription) IsClose() bool {
if s == nil {
return true
}
return atomic.LoadInt32(&s.closed) == 1
}
func (s *Subscription) Close() error {
if s.IsClose() {
return ErrFbEventClosed
}
return s.doClose(nil)
}
func (s *Subscription) closeWithError(err error) error {
if s.IsClose() {
return ErrFbEventClosed
}
return s.doClose(err)
}
func (s *Subscription) doClose(err error) (errResult error) {
atomic.StoreInt32(&s.closed, 1)
s.closer.Do(func() {
close(s.doneSubscription)
s.muClose.Lock()
defer s.muClose.Unlock()
if err != nil {
for _, c := range s.closes {
c <- err
}
}
s.mu.RLock()
errResult = s.fc.Close()
s.mu.RUnlock()
if atomic.LoadInt32(&s.noNotify) == 0 {
s.chDoneEvent <- s
}
})
return
}