From b4635386bb7cd558ba6f88a90bda9089270c0fb3 Mon Sep 17 00:00:00 2001 From: jar3b Date: Fri, 21 Feb 2020 15:47:48 +0300 Subject: [PATCH] feat: add FinalizeNats function; chore: add checks for non-nil connections before close --- nacl.go | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/nacl.go b/nacl.go index fd30ff4..6740c10 100644 --- a/nacl.go +++ b/nacl.go @@ -14,9 +14,10 @@ var ( ) type ( - Msg = stan.Msg - NatsMsg = nats.Msg - Subscription = stan.Subscription + Msg = stan.Msg + NatsMsg = nats.Msg + Subscription = stan.Subscription + NatsSubscription = nats.Subscription ) func SetupNats(host string, port int, user string, pass string, closeHandler *grawt.CloseHandler) error { @@ -64,6 +65,9 @@ func SetupStan(clusterName string, clientId string, host string, port int, user } func FinalizeStan(subscriptions *[]Subscription) error { + if StanClient == nil { + return fmt.Errorf("stan client is not initialized") + } for _, subscription := range *subscriptions { _ = subscription.Unsubscribe() } @@ -74,3 +78,16 @@ func FinalizeStan(subscriptions *[]Subscription) error { return nil } + +func FinalizeNats(subscriptions *[]*NatsSubscription) error { + if StanClient == nil { + return fmt.Errorf("stan client is not initialized") + } + for _, subscription := range *subscriptions { + _ = subscription.Unsubscribe() + } + + NatsClient.Close() + + return nil +}