-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Altered to have a struct owning the connection to NATS
- Loading branch information
Showing
3 changed files
with
46 additions
and
63 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,82 +1,55 @@ | ||
package event | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"log" | ||
"sync" | ||
|
||
"github.com/nats-io/nats.go" | ||
"github.com/hashicorp/go-hclog" | ||
"sync" | ||
) | ||
|
||
const NATS_RECONNECT_BUF_SIZE = 5*1024*1024 | ||
|
||
type chanHolder struct { | ||
Ch interface{} | ||
} | ||
type NatsBus struct { | ||
logger hclog.Logger | ||
|
||
var ( | ||
conn *nats.Conn | ||
subCh []chanHolder | ||
mu sync.Mutex | ||
) | ||
} | ||
|
||
func Connect(server string) error { | ||
mu.Lock() | ||
defer mu.Unlock() | ||
func NewNatsBus(logger hclog.Logger) *NatsBus { | ||
return &NatsBus{ | ||
logger: logger, | ||
} | ||
} | ||
|
||
if conn != nil { | ||
func (nb *NatsBus) Connect(server string) error { | ||
nb.mu.Lock() | ||
defer nb.mu.Unlock() | ||
|
||
if nb.conn != nil { | ||
return errors.New("already connected") | ||
} | ||
|
||
c, err := nats.Connect(server, nats.ReconnectBufSize(NATS_RECONNECT_BUF_SIZE)) | ||
if err != nil { | ||
return err | ||
} | ||
conn = c | ||
subCh = make([]chanHolder, 0) | ||
nb.conn = c | ||
|
||
return nil | ||
} | ||
|
||
func Subscribe[T any](topic string) (chan T, error) { | ||
ch := make(chan T) | ||
_, err := conn.Subscribe(topic, func(m *nats.Msg) { | ||
var msg T | ||
decoder := json.NewDecoder(bytes.NewReader(m.Data)) | ||
decoder.DisallowUnknownFields() | ||
err := decoder.Decode(&msg) | ||
if err != nil { | ||
log.Printf("Error unmarshalling message: %v", err) | ||
return | ||
} | ||
ch <- msg | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
mu.Lock() | ||
subCh = append(subCh, chanHolder{Ch: ch}) | ||
mu.Unlock() | ||
|
||
return ch, nil | ||
} | ||
|
||
func Publish[T any](msg T, topic string) error { | ||
// Not a method due to Golang limitations on generics there, so we just pass the bus as a parameter. | ||
func Publish[T any](nb *NatsBus, msg T, topic string) error { | ||
data, err := json.Marshal(msg) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Publishing message to %s: %s", topic, string(data)) | ||
return conn.Publish(topic, data) | ||
nb.logger.Trace("Publishing message", "topic", topic, "data", string(data)) | ||
return nb.conn.Publish(topic, data) | ||
} | ||
|
||
func Close() { | ||
conn.Close() | ||
for _, holder := range subCh { | ||
if ch, ok := holder.Ch.(chan interface{}); ok { | ||
close(ch) | ||
} | ||
} | ||
func (nb *NatsBus) Close() { | ||
nb.conn.Close() | ||
} |
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