generated from kyma-project/template-repository
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rely on the NATS cluster connection status when reporting the Eventin…
…g CR status
- Loading branch information
1 parent
0df1b52
commit ad624bf
Showing
21 changed files
with
597 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package main | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_getNATSConnectionBuilder(t *testing.T) { | ||
assert.NotNil(t, getNATSConnectionBuilder()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package nats | ||
|
||
import ( | ||
natsio "github.com/nats-io/nats.go" | ||
) | ||
|
||
type Builder interface { | ||
Build() NATS | ||
} | ||
|
||
type ConnectionBuilder struct { | ||
url string | ||
opts []natsio.Option | ||
} | ||
|
||
func NewBuilder(url, name string, opts ...natsio.Option) *ConnectionBuilder { | ||
opts = append(opts, natsio.Name(name)) // enforce configuring the connection name | ||
return &ConnectionBuilder{ | ||
url: url, | ||
opts: opts, | ||
} | ||
} | ||
|
||
func (b *ConnectionBuilder) Build() NATS { | ||
return &connection{ | ||
url: b.url, | ||
conn: nil, | ||
opts: b.opts, | ||
reconnectHandlerRegistered: false, | ||
disconnectErrHandlerRegistered: false, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package nats | ||
|
||
import ( | ||
natsio "github.com/nats-io/nats.go" | ||
|
||
natsconnectionerrors "github.com/kyma-project/eventing-manager/internal/connection/nats/errors" | ||
) | ||
|
||
// compile-time check. | ||
var _ NATS = &connection{} | ||
|
||
// connection represents a NATS connection. | ||
type connection struct { | ||
url string | ||
conn *natsio.Conn | ||
opts []natsio.Option | ||
reconnectHandlerRegistered bool | ||
disconnectErrHandlerRegistered bool | ||
} | ||
|
||
func (c *connection) Connect() error { | ||
if c.isConnected() { | ||
return nil | ||
} | ||
|
||
var err error | ||
if c.conn, err = natsio.Connect(c.url, c.opts...); err != nil { | ||
return err | ||
} | ||
|
||
if c.isConnected() { | ||
return nil | ||
} | ||
|
||
return natsconnectionerrors.ErrCannotConnect | ||
} | ||
|
||
func (c *connection) Disconnect() { | ||
if c.conn == nil || c.conn.IsClosed() { | ||
return | ||
} | ||
c.conn.Close() | ||
} | ||
|
||
func (c *connection) isConnected() bool { | ||
if c.conn == nil { | ||
return false | ||
} | ||
return c.conn.IsConnected() | ||
} | ||
|
||
func (c *connection) RegisterReconnectHandlerIfNotRegistered(handler natsio.ConnHandler) { | ||
if c.conn == nil || c.reconnectHandlerRegistered { | ||
return | ||
} | ||
c.conn.SetReconnectHandler(handler) | ||
c.reconnectHandlerRegistered = true | ||
} | ||
|
||
func (c *connection) RegisterDisconnectErrHandlerIfNotRegistered(handler natsio.ConnErrHandler) { | ||
if c.conn == nil || c.disconnectErrHandlerRegistered { | ||
return | ||
} | ||
c.conn.SetDisconnectErrHandler(handler) | ||
c.disconnectErrHandlerRegistered = true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package errors | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
const ( | ||
errCannotConnect = "cannot connect to NATS" | ||
) | ||
|
||
// ErrCannotConnect represents an error when NATS connection failed. | ||
var ErrCannotConnect = errors.New(errCannotConnect) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package nats | ||
|
||
import ( | ||
natsio "github.com/nats-io/nats.go" | ||
) | ||
|
||
//go:generate go run github.com/vektra/mockery/v2 --name=NATS --filename=nats.go | ||
type NATS interface { | ||
// Connect connects to NATS and returns an error if it cannot. | ||
Connect() error | ||
|
||
// Disconnect disconnects the active connection. | ||
Disconnect() | ||
|
||
// RegisterReconnectHandlerIfNotRegistered registers a ReconnectHandler only if not registered. | ||
RegisterReconnectHandlerIfNotRegistered(natsio.ConnHandler) | ||
|
||
// RegisterDisconnectErrHandlerIfNotRegistered registers a DisconnectErrHandler only if not registered. | ||
RegisterDisconnectErrHandlerIfNotRegistered(natsio.ConnErrHandler) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package mocks | ||
|
||
import ( | ||
natsconnection "github.com/kyma-project/eventing-manager/internal/connection/nats" | ||
) | ||
|
||
type Builder struct { | ||
mock *NATS | ||
} | ||
|
||
func NewBuilder(mock *NATS) *Builder { | ||
return &Builder{mock: mock} | ||
} | ||
|
||
func (b *Builder) Build() natsconnection.NATS { | ||
return b.mock | ||
} |
Oops, something went wrong.