/
connect.go
124 lines (99 loc) · 3.27 KB
/
connect.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
/*
* Copyright (c) Simon Pelczer 2021. All rights reserved.
* Licensed under the MIT license. See LICENSE file in the project root for full license information.
*/
package connector
import (
"log"
"github.com/Templum/rabbitmq-connector/pkg/config"
"github.com/Templum/rabbitmq-connector/pkg/rabbitmq"
"github.com/Templum/rabbitmq-connector/pkg/types"
"github.com/streadway/amqp"
)
// RabbitToOpenFaaS defines the basic interactions for the connector
type RabbitToOpenFaaS interface {
Run() error
Shutdown()
}
// New creates a new connector instance using the provided parameters & config to build it up
func New(manager rabbitmq.Manager, factory rabbitmq.Factory, invoker types.Invoker, conf *config.Controller) RabbitToOpenFaaS {
return &Connector{
client: invoker,
factory: factory,
conManager: manager,
conf: conf,
}
}
// Connector includes all relevant information that the connector needs to hold and maintain
type Connector struct {
client types.Invoker
factory rabbitmq.Factory
conManager rabbitmq.Manager
conf *config.Controller
exchanges []rabbitmq.ExchangeOrganizer
}
// Run starts the connector and creates a connection RabbitMQ. Further it implements the defined Topology.
// Also it adds a listener that handles connection failures.
func (c *Connector) Run() error {
log.Println("Started RabbitMQ <=> OpenFaaS Connector")
log.Printf("Will now establish connection to %s", c.conf.RabbitSanitizedURL)
failureChan, conErr := c.conManager.Connect(c.conf.RabbitConnectionURL)
if conErr != nil {
return conErr
}
go c.HandleConnectionError(failureChan)
genErr := c.generateExchangesFrom(c.conf.Topology)
if genErr != nil {
return genErr
}
for _, ex := range c.exchanges {
err := ex.Start()
if err != nil {
return err
}
}
return nil
}
// HandleConnectionError listens for incoming connection errors. If it is recoverable it will attempt a self-heal.
// Otherwise it shutsdown the whole connector
func (c *Connector) HandleConnectionError(ch <-chan *amqp.Error) {
err := <-ch
log.Printf("Rabbit MQ Connection failed with %s Code: %d [Server=%t Recover=%t]", err.Reason, err.Code, err.Server, err.Recover)
if err.Recover {
for _, ex := range c.exchanges {
ex.Stop()
}
// Release old exchange refs to garbage collection
c.exchanges = nil
err := c.Run()
if err != nil {
log.Panicf("Received critical error: %s during restart, shutting down", err)
}
} else {
log.Panicf("Received critical error: %s, shutting down", err)
}
}
// Shutdown is usually called during graceful shutdown. It stops all exchanges and finally closes the connection
// to RabbitMQ
func (c *Connector) Shutdown() {
log.Println("Shutdown RabbitMQ <=> OpenFaaS Connector")
// Loop over Exchanges to close
for _, ex := range c.exchanges {
ex.Stop()
}
// Close Connection
c.conManager.Disconnect()
}
func (c *Connector) generateExchangesFrom(t types.Topology) error {
// Do we want to use a connection per Exchange or continue with channels ?
c.factory.WithChanCreator(c.conManager).WithInvoker(c.client)
for _, topology := range c.conf.Topology {
tmp := types.Exchange(topology)
exchange, buildErr := c.factory.WithExchange(&tmp).Build()
if buildErr != nil {
return buildErr
}
c.exchanges = append(c.exchanges, exchange)
}
return nil
}