mirror of
https://github.com/openziti/zrok.git
synced 2025-06-19 17:27:54 +02:00
simpler amqp sink approach (#351)
This commit is contained in:
parent
aabf695bec
commit
5a2f6a1f72
@ -31,38 +31,39 @@ func loadAmqpSinkConfig(v interface{}, _ *cf.Options) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type amqpSink struct {
|
type amqpSink struct {
|
||||||
cfg *AmqpSinkConfig
|
cfg *AmqpSinkConfig
|
||||||
conn *amqp.Connection
|
conn *amqp.Connection
|
||||||
ch *amqp.Channel
|
ch *amqp.Channel
|
||||||
queue amqp.Queue
|
queue amqp.Queue
|
||||||
join chan struct{}
|
connected bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func newAmqpSink(cfg *AmqpSinkConfig) (*amqpSink, error) {
|
func newAmqpSink(cfg *AmqpSinkConfig) (*amqpSink, error) {
|
||||||
return &amqpSink{
|
as := &amqpSink{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
join: make(chan struct{}),
|
}
|
||||||
}, nil
|
return as, nil
|
||||||
}
|
|
||||||
|
|
||||||
func (s *amqpSink) Start() (join chan struct{}, err error) {
|
|
||||||
logrus.Info("started")
|
|
||||||
return s.join, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *amqpSink) Handle(event ZitiEventJson) error {
|
func (s *amqpSink) Handle(event ZitiEventJson) error {
|
||||||
|
if !s.connected {
|
||||||
|
if err := s.connect(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
logrus.Infof("connected to '%v'", s.cfg.Url)
|
||||||
|
s.connected = true
|
||||||
|
}
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
logrus.Infof("pushing '%v'", event)
|
logrus.Infof("pushing '%v'", event)
|
||||||
return s.ch.PublishWithContext(ctx, "", s.queue.Name, false, false, amqp.Publishing{
|
err := s.ch.PublishWithContext(ctx, "", s.queue.Name, false, false, amqp.Publishing{
|
||||||
ContentType: "application/json",
|
ContentType: "application/json",
|
||||||
Body: []byte(event),
|
Body: []byte(event),
|
||||||
})
|
})
|
||||||
}
|
if err != nil {
|
||||||
|
s.connected = false
|
||||||
func (s *amqpSink) Stop() {
|
}
|
||||||
close(s.join)
|
return err
|
||||||
logrus.Info("stopped")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *amqpSink) connect() (err error) {
|
func (s *amqpSink) connect() (err error) {
|
||||||
@ -70,18 +71,13 @@ func (s *amqpSink) connect() (err error) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error dialing amqp broker")
|
return errors.Wrap(err, "error dialing amqp broker")
|
||||||
}
|
}
|
||||||
|
|
||||||
s.ch, err = s.conn.Channel()
|
s.ch, err = s.conn.Channel()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error getting amqp channel")
|
return errors.Wrap(err, "error getting amqp channel")
|
||||||
}
|
}
|
||||||
|
|
||||||
s.queue, err = s.ch.QueueDeclare(s.cfg.QueueName, true, false, false, false, nil)
|
s.queue, err = s.ch.QueueDeclare(s.cfg.QueueName, true, false, false, false, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "error declaring queue")
|
return errors.Wrap(err, "error declaring queue")
|
||||||
}
|
}
|
||||||
|
|
||||||
logrus.Infof("connected to amqp broker at '%v'", s.cfg.Url)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,6 @@ type Bridge struct {
|
|||||||
src ZitiEventJsonSource
|
src ZitiEventJsonSource
|
||||||
srcJoin chan struct{}
|
srcJoin chan struct{}
|
||||||
snk ZitiEventJsonSink
|
snk ZitiEventJsonSink
|
||||||
snkJoin chan struct{}
|
|
||||||
events chan ZitiEventMsg
|
events chan ZitiEventMsg
|
||||||
close chan struct{}
|
close chan struct{}
|
||||||
join chan struct{}
|
join chan struct{}
|
||||||
@ -40,9 +39,6 @@ func NewBridge(cfg *BridgeConfig) (*Bridge, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bridge) Start() (join chan struct{}, err error) {
|
func (b *Bridge) Start() (join chan struct{}, err error) {
|
||||||
if b.snkJoin, err = b.snk.Start(); err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if b.srcJoin, err = b.src.Start(b.events); err != nil {
|
if b.srcJoin, err = b.src.Start(b.events); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -76,9 +72,7 @@ func (b *Bridge) Start() (join chan struct{}, err error) {
|
|||||||
|
|
||||||
func (b *Bridge) Stop() {
|
func (b *Bridge) Stop() {
|
||||||
b.src.Stop()
|
b.src.Stop()
|
||||||
b.snk.Stop()
|
|
||||||
close(b.close)
|
close(b.close)
|
||||||
<-b.srcJoin
|
<-b.srcJoin
|
||||||
<-b.snkJoin
|
|
||||||
<-b.join
|
<-b.join
|
||||||
}
|
}
|
||||||
|
@ -79,7 +79,5 @@ type ZitiEventJsonSource interface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type ZitiEventJsonSink interface {
|
type ZitiEventJsonSink interface {
|
||||||
Start() (join chan struct{}, err error)
|
|
||||||
Handle(event ZitiEventJson) error
|
Handle(event ZitiEventJson) error
|
||||||
Stop()
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user