/
pubsub.go
131 lines (112 loc) · 2.98 KB
/
pubsub.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
// Copyright (c) 2021 Terminus, Inc.
//
// This program is free software: you can use, redistribute, and/or modify
// it under the terms of the GNU Affero General Public License, version 3
// or later ("AGPL"), as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package websocket
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"sync"
"time"
"github.com/coreos/etcd/clientv3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/erda-project/erda/pkg/crypto/uuid"
"github.com/erda-project/erda/pkg/jsonstore"
"github.com/erda-project/erda/pkg/jsonstore/etcd"
"github.com/erda-project/erda/pkg/jsonstore/storetypes"
)
const (
eDir = "/websocket/events"
ttl = 60
)
type Subscriber struct {
js jsonstore.JSONStoreWithWatch
eventC chan *Event
stopC chan struct{}
runningWg sync.WaitGroup
}
func NewSubscriber(ec chan *Event) (*Subscriber, error) {
js, err := jsonstore.New()
if err != nil {
return nil, errors.Wrap(err, "failed to create json storage instance")
}
js_ := js.(jsonstore.JSONStoreWithWatch)
return &Subscriber{
js: js_,
eventC: ec,
stopC: make(chan struct{}),
}, nil
}
func (s *Subscriber) retry(f func() error) {
err := f()
if err != nil {
s.retry(f)
}
}
func (s *Subscriber) Start() {
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-s.stopC
cancel()
}()
f := func() error {
err := s.js.Watch(ctx, eDir, true, true, false, Event{},
func(key string, value interface{}, _ storetypes.ChangeType) error {
event := value.(*Event)
go func() {
logrus.Debugf("watch event %v", event)
s.eventC <- event
}()
return nil
})
return err
}
s.retry(f)
}
func (s *Subscriber) Stop() {
s.stopC <- struct{}{}
s.runningWg.Wait()
logrus.Warn("stop subscriber")
}
type Publisher struct {
es *etcd.Store
}
func NewPublisher() (*Publisher, error) {
es, err := etcd.New()
if err != nil {
return nil, errors.Wrap(err, "failed to create etcd store instance")
}
return &Publisher{es: es}, nil
}
func (p *Publisher) EmitEvent(ctx context.Context, e Event) error {
path := generatePath()
lease := clientv3.NewLease(p.es.GetClient())
r, err := lease.Grant(ctx, ttl)
if err != nil {
return errors.Wrap(err, "failed to grant etcd lease")
}
b, err := json.Marshal(e)
if err != nil {
return errors.Wrapf(err, "failed to marshal event %v", e)
}
if _, err := p.es.PutWithOption(ctx, path, string(b), []interface{}{clientv3.WithLease(r.ID)}); err != nil {
return err
}
logrus.Debugf("emit ws event %+v", e)
return nil
}
func generatePath() string {
uuid := fmt.Sprintf("%d-%s", time.Now().UnixNano(), uuid.UUID())
return filepath.Join(eDir, uuid)
}