-
Notifications
You must be signed in to change notification settings - Fork 6
/
subscriber.go
536 lines (485 loc) · 15.4 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
package subscriber
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"sync"
"github.com/google/uuid"
"github.com/redhat-cne/sdk-go/pkg/channel"
"github.com/redhat-cne/sdk-go/pkg/types"
"github.com/pkg/errors"
"github.com/redhat-cne/sdk-go/pkg/pubsub"
"github.com/redhat-cne/sdk-go/pkg/store"
log "github.com/sirupsen/logrus"
SubscriberStore "github.com/redhat-cne/sdk-go/pkg/store/subscriber"
"github.com/redhat-cne/sdk-go/pkg/subscriber"
)
// API ... api methods for publisher subscriber
type API struct {
SubscriberStore *SubscriberStore.Store // each client will have one store
storeFilePath string // subscribers
transportEnabled bool // http is enabled
}
var instance *API
var once sync.Once
var mu sync.Mutex
// NewSubscriber create new subscribers connections
func NewSubscriber(clientID uuid.UUID) subscriber.Subscriber {
return subscriber.Subscriber{
ClientID: clientID,
SubStore: &store.PubSubStore{
RWMutex: sync.RWMutex{},
Store: nil,
},
EndPointURI: nil,
Status: 1,
}
}
// New creates empty publisher or subscriber
func New() subscriber.Subscriber {
return subscriber.Subscriber{}
}
// GetAPIInstance get event instance
func GetAPIInstance(storeFilePath string) *API {
once.Do(func() {
instance = &API{
transportEnabled: true,
SubscriberStore: &SubscriberStore.Store{
RWMutex: sync.RWMutex{},
Store: map[uuid.UUID]*subscriber.Subscriber{},
},
storeFilePath: storeFilePath,
}
hasDir(storeFilePath)
})
instance.ReloadStore()
return instance
}
// ReloadStore reload store if there is any change or refresh is required
func (p *API) ReloadStore() {
// load for file
log.Infof("reloading subscribers from the store %s", p.storeFilePath)
if files, err := loadFileNamesFromDir(p.storeFilePath); err == nil {
for _, f := range files {
// valid subscription filename is <uuid>.json
if uuid.Validate(strings.Split(f, ".")[0]) != nil {
continue
} else if b, err1 := loadFromFile(fmt.Sprintf("%s/%s", p.storeFilePath, f)); err1 == nil {
if len(b) > 0 {
var sub subscriber.Subscriber
var err2 error
if err2 = json.Unmarshal(b, &sub); err2 == nil {
if sub.ClientID != uuid.Nil {
p.SubscriberStore.Set(sub.ClientID, sub)
} else {
log.Errorf("subscriber data from file %s is not valid", f)
}
} else {
log.Errorf("error parsing subscriber %s\n %s", string(b), err2.Error())
}
}
} else {
log.Errorf("error loading file %s\n %s", fmt.Sprintf("%s/%s", p.storeFilePath, f), err1.Error())
}
}
}
log.Infof("%d registered clients reloaded", len(p.SubscriberStore.Store))
for k, v := range p.SubscriberStore.Store {
log.Infof("registered clients %s : %s", k, v.String())
}
}
// HasTransportEnabled ...
func (p *API) HasTransportEnabled() bool {
return p.transportEnabled
}
// DisableTransport ...
func (p *API) DisableTransport() {
p.transportEnabled = false
}
// EnableTransport ...
func (p *API) EnableTransport() {
p.transportEnabled = true
}
// ClientCount .. client cound
func (p *API) ClientCount() int {
return len(p.SubscriberStore.Store)
}
// GetSubFromSubscriptionsStore get data from publisher store
func (p *API) GetSubFromSubscriptionsStore(clientID uuid.UUID, address string) (pubsub.PubSub, error) {
if subscriber, ok := p.HasClient(clientID); ok {
for _, sub := range subscriber.SubStore.Store {
if sub.GetResource() == address {
return pubsub.PubSub{
Version: sub.Version,
ID: sub.ID,
EndPointURI: sub.EndPointURI,
URILocation: sub.URILocation,
Resource: sub.Resource,
}, nil
}
}
}
return pubsub.PubSub{}, fmt.Errorf("publisher not found for address %s", address)
}
// HasSubscription check if the subscriptionOne is already exists in the store/cache
func (p *API) HasSubscription(clientID uuid.UUID, address string) (pubsub.PubSub, bool) {
if sub, err := p.GetSubFromSubscriptionsStore(clientID, address); err == nil {
return sub, true
}
return pubsub.PubSub{}, false
}
// HasClient check if client is already exists in the store/cache
func (p *API) HasClient(clientID uuid.UUID) (*subscriber.Subscriber, bool) {
if subscriber, ok := p.SubscriberStore.Get(clientID); ok {
return &subscriber, true
}
return nil, false
}
// CreateSubscription create a subscriptionOne and store it in a file and cache
func (p *API) CreateSubscription(clientID uuid.UUID, sub subscriber.Subscriber) (subscriptionClient *subscriber.Subscriber, err error) {
var ok bool
if subscriptionClient, ok = p.HasClient(clientID); !ok {
subscriptionClient = subscriber.New(clientID)
}
subscriptionClient.ResetFailCount()
_ = subscriptionClient.SetEndPointURI(sub.GetEndPointURI())
subscriptionClient.SetStatus(subscriber.Active)
subscriptionClient.Action = channel.NEW
pubStore := subscriptionClient.GetSubStore()
var hasResource bool
for key, value := range sub.SubStore.Store {
hasResource = false
for _, s := range pubStore.Store {
if s.Resource == value.Resource {
hasResource = true
continue
}
}
if !hasResource {
if key == "" {
key = uuid.New().String()
}
subscriptionClient.SubStore.Set(key, *value)
}
}
p.SubscriberStore.Set(clientID, *subscriptionClient)
// persist the subscriptionOne -
err = writeToFile(*subscriptionClient, fmt.Sprintf("%s/%s", p.storeFilePath, fmt.Sprintf("%s.json", clientID)))
if err != nil {
log.Errorf("error writing to a store %v\n", err)
return nil, err
}
log.Infof("subscription persisted into a file %s", fmt.Sprintf("%s/%s - content %s", p.storeFilePath, fmt.Sprintf("%s.json", clientID), subscriptionClient.String()))
// store the publisher
return subscriptionClient, nil
}
// GetSubscriptionClient get a clientID by id
func (p *API) GetSubscriptionClient(clientID uuid.UUID) (subscriber.Subscriber, error) {
if subs, ok := p.SubscriberStore.Get(clientID); ok {
return subs, nil
}
return subscriber.Subscriber{}, fmt.Errorf("subscriber data was not found for id %s", clientID)
}
// GetSubscriptionsFromFile get subscriptions data from the file store
func (p *API) GetSubscriptionsFromFile(clientID uuid.UUID) ([]byte, error) {
b, err := loadFromFile(fmt.Sprintf("%s/%s", p.storeFilePath, fmt.Sprintf("%s.json", clientID.String())))
return b, err
}
// GetSubscriptionsFromClientID get all subs from the client
func (p *API) GetSubscriptionsFromClientID(clientID uuid.UUID) (sub map[string]*pubsub.PubSub) {
if subs, ok := p.SubscriberStore.Get(clientID); ok {
sub = subs.SubStore.Store
}
return
}
// GetSubscriptions get all subs
func (p *API) GetSubscriptions() ([]byte, error) {
p.SubscriberStore.RLock()
defer p.SubscriberStore.RUnlock()
var allSubs []pubsub.PubSub
for _, s := range p.SubscriberStore.Store {
for _, sub := range s.SubStore.Store {
allSubs = append(allSubs, *sub)
}
}
return json.MarshalIndent(&allSubs, "", " ")
}
// GetSubscription get sub info from clientID and subID
func (p *API) GetSubscription(clientID uuid.UUID, subID string) (pubsub.PubSub, error) {
if subs, ok := p.SubscriberStore.Get(clientID); ok {
return subs.Get(subID), nil
}
return pubsub.PubSub{}, fmt.Errorf("subscription data was not found for id %s", subID)
}
// GetSubscriberURLByResourceAndClientID get subscription information by client id/resource
func (p *API) GetSubscriberURLByResourceAndClientID(clientID uuid.UUID, resource string) (url *string) {
p.SubscriberStore.RLock()
defer p.SubscriberStore.RUnlock()
for _, subs := range p.SubscriberStore.Store {
if subs.ClientID == clientID {
for _, sub := range subs.SubStore.Store {
if sub.GetResource() == resource {
return func(s string) *string {
return &s
}(subs.GetEndPointURI())
}
}
}
}
return nil
}
// GetSubscriberURLByResource get subscriptionOne information
func (p *API) GetSubscriberURLByResource(resource string) (urls []string) {
p.SubscriberStore.RLock()
defer p.SubscriberStore.RUnlock()
for _, subs := range p.SubscriberStore.Store {
for _, sub := range subs.SubStore.Store {
if sub.GetResource() == resource {
urls = append(urls, subs.GetEndPointURI())
}
}
}
return urls
}
// GetClientIDByResource get subscriptionOne information
func (p *API) GetClientIDByResource(resource string) (clientIDs []uuid.UUID) {
p.SubscriberStore.RLock()
defer p.SubscriberStore.RUnlock()
for _, subs := range p.SubscriberStore.Store {
for _, sub := range subs.SubStore.Store {
if strings.Contains(sub.GetResource(), resource) {
clientIDs = append(clientIDs, subs.ClientID)
}
}
}
return clientIDs
}
// GetClientIDBySubID ...
func (p *API) GetClientIDBySubID(subID string) (clientIDs []uuid.UUID) {
p.SubscriberStore.RLock()
defer p.SubscriberStore.RUnlock()
for _, subs := range p.SubscriberStore.Store {
for _, sub := range subs.SubStore.Store {
if sub.GetID() == subID {
clientIDs = append(clientIDs, subs.ClientID)
}
}
}
return clientIDs
}
// GetClientIDAddressByResource get subscriptionOne information
func (p *API) GetClientIDAddressByResource(resource string) map[uuid.UUID]*types.URI {
clients := map[uuid.UUID]*types.URI{}
p.SubscriberStore.RLock()
defer p.SubscriberStore.RUnlock()
for _, subs := range p.SubscriberStore.Store {
for _, sub := range subs.SubStore.Store {
if sub.GetResource() == resource {
clients[subs.ClientID] = subs.EndPointURI
}
}
}
return clients
}
// DeleteSubscription delete a subscriptionOne by id
func (p *API) DeleteSubscription(clientID uuid.UUID, subscriptionID string) error {
if subStore, ok := p.SubscriberStore.Get(clientID); ok { // client found
if sub, ok2 := subStore.SubStore.Store[subscriptionID]; ok2 {
err := deleteFromFile(*sub, fmt.Sprintf("%s/%s", p.storeFilePath, fmt.Sprintf("%s.json", clientID)))
subStore.SubStore.Delete(subscriptionID)
p.SubscriberStore.Set(clientID, subStore)
return err
}
}
return nil
}
// DeleteAllSubscriptionsForClient delete all subscriptions for the client
func (p *API) DeleteAllSubscriptionsForClient(clientID uuid.UUID) (int, error) {
var err error
var numSubDeleted, numSubToDelete int
if sub, ok := p.SubscriberStore.Get(clientID); ok {
numSubToDelete = len(sub.SubStore.Store)
if err = p.DeleteClient(clientID); err != nil {
return 0, err
}
numSubDeleted += numSubToDelete
}
return numSubDeleted, nil
}
// DeleteAllSubscriptions delete all subscriptions in store
func (p *API) DeleteAllSubscriptions() (int, error) {
var err error
var numSubDeleted, numSubToDelete int
for clientID, subs := range p.SubscriberStore.Store {
numSubToDelete = len(subs.SubStore.Store)
if err = p.DeleteClient(clientID); err != nil {
return numSubDeleted, err
}
numSubDeleted += numSubToDelete
}
return numSubDeleted, nil
}
// DeleteClient delete all subscriptionOne information
func (p *API) DeleteClient(clientID uuid.UUID) error {
if _, ok := p.SubscriberStore.Get(clientID); ok { // client found
log.Infof("delete from file %s",
fmt.Sprintf("%s/%s", p.storeFilePath, fmt.Sprintf("%s.json", clientID)))
if err := deleteAllFromFile(fmt.Sprintf("%s/%s", p.storeFilePath, fmt.Sprintf("%s.json", clientID))); err != nil {
return err
}
p.SubscriberStore.Delete(clientID)
} else {
log.Infof("subscription for client id %s not found", clientID)
}
return nil
}
// UpdateStatus .. update status
func (p *API) UpdateStatus(clientID uuid.UUID, status subscriber.Status) error {
if subStore, ok := p.SubscriberStore.Get(clientID); ok {
subStore.SetStatus(status)
p.SubscriberStore.Set(clientID, subStore)
// do not write to file , if restarts it will consider all client are active
} else {
return errors.New("failed to update subscriber status")
}
return nil
}
// IncFailCountToFail .. update fail count
func (p *API) IncFailCountToFail(clientID uuid.UUID) bool {
if subStore, ok := p.SubscriberStore.Get(clientID); ok {
subStore.IncFailCount()
p.SubscriberStore.Set(clientID, subStore)
if subStore.Action == channel.DELETE {
return true
}
}
return false
}
// ResetFailCount ..reset fail count
func (p *API) ResetFailCount(clientID uuid.UUID) {
if subStore, ok := p.SubscriberStore.Get(clientID); ok {
subStore.ResetFailCount()
p.SubscriberStore.Set(clientID, subStore)
}
}
// FailCountThreshold .. get threshold
func (p *API) FailCountThreshold() int {
return subscriber.SetConnectionToFailAfter
}
func (p *API) GetFailCount(clientID uuid.UUID) int {
if subStore, ok := p.SubscriberStore.Get(clientID); ok {
return subStore.FailedCount()
}
return 0
}
// SubscriberMarkedForDelete ...
func (p *API) SubscriberMarkedForDelete(clientID uuid.UUID) bool {
if subStore, ok := p.SubscriberStore.Get(clientID); ok {
if subStore.Action == channel.DELETE {
return true
}
}
return false
}
// deleteAllFromFile deletes publisher and subscriptionOne information from the file system
func deleteAllFromFile(filePath string) error {
return os.Remove(filePath)
}
// DeleteFromFile is used to delete subscriptionOne from the file system
func deleteFromFile(sub pubsub.PubSub, filePath string) error {
var persistedSubClient subscriber.Subscriber
//open file
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer file.Close()
//read file and unmarshall json file to slice of users
b, err := io.ReadAll(file)
if err != nil {
return err
}
if len(b) > 0 {
err = json.Unmarshal(b, &persistedSubClient)
if err != nil {
return err
}
}
delete(persistedSubClient.SubStore.Store, sub.ID)
newBytes, err := json.MarshalIndent(&persistedSubClient, "", " ")
if err != nil {
log.Errorf("error deleting sub %v", err)
return err
}
return os.WriteFile(filePath, newBytes, 0666)
}
// loadFromFile is used to read subscriptionOne/publisher from the file system
func loadFromFile(filePath string) (b []byte, err error) {
//open file
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return nil, err
}
defer file.Close()
//read file and unmarshall json file to slice of users
b, err = io.ReadAll(file)
if err != nil {
return nil, err
}
return b, nil
}
func loadFileNamesFromDir(filePath string) (subFiles []string, err error) {
files, err := os.ReadDir(filePath)
if err != nil {
return subFiles, err
}
for _, file := range files {
if !file.IsDir() {
subFiles = append(subFiles, file.Name())
}
}
return
}
// writeToFile writes subscriptionOne data to a file
func writeToFile(subscriberClient subscriber.Subscriber, filePath string) error {
//open file
mu.Lock()
defer mu.Unlock()
file, err := os.OpenFile(filePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
defer file.Close()
//read file and unmarshall json file to slice of users
b, err := io.ReadAll(file)
if err != nil {
return err
}
var persistedSubClient subscriber.Subscriber
if len(b) > 0 {
err = json.Unmarshal(b, &persistedSubClient)
if err != nil {
return err
}
} else {
persistedSubClient = *subscriber.New(subscriberClient.ClientID)
} // no file found
_ = persistedSubClient.SetEndPointURI(subscriberClient.GetEndPointURI())
persistedSubClient.SetStatus(subscriber.Active)
for subID, sub := range subscriberClient.SubStore.Store {
persistedSubClient.SubStore.Store[subID] = sub
}
newBytes, err := json.MarshalIndent(&persistedSubClient, "", " ")
if err != nil {
return err
}
log.Infof("persisting following contents %s to a file %s\n", string(newBytes), filePath)
return os.WriteFile(filePath, newBytes, 0666)
}
func hasDir(path string) {
if _, err := os.Stat(path); os.IsNotExist(err) {
_ = os.Mkdir(path, 0700)
}
}