Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added reconnection logic when NATS is disconnected #49

Merged
merged 1 commit into from
Jan 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 59 additions & 11 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,65 @@ import (
"encoding/json"
"fmt"
"log"
"sync"
"time"

"github.com/nats-io/go-nats-streaming"
"github.com/openfaas/faas/gateway/queue"
)

// NatsQueue queue for work
type NatsQueue struct {
nc stan.Conn
nc stan.Conn
ncMutex *sync.RWMutex
maxReconnect int
reconnectDelay time.Duration

ClientID string
ClusterID string
NATSURL string
Topic string
}

func (q *NatsQueue) connect() error {
nc, err := stan.Connect(
q.ClusterID,
q.ClientID,
stan.NatsURL(q.NATSURL),
stan.SetConnectionLostHandler(func(conn stan.Conn, err error) {
log.Printf("Disconnected from %s\n", q.NATSURL)

q.reconnect()
}),
)

if err != nil {
return err
}

q.ncMutex.Lock()
q.nc = nc
bartsmykla marked this conversation as resolved.
Show resolved Hide resolved
q.ncMutex.Unlock()
bartsmykla marked this conversation as resolved.
Show resolved Hide resolved

return nil
}

func (q *NatsQueue) reconnect() {
for i := 0; i < q.maxReconnect; i++ {
time.Sleep(time.Second * time.Duration(i) * q.reconnectDelay)

if err := q.connect(); err == nil {
log.Printf("Reconnection (%d/%d) to %s succeeded\n", i+1, q.maxReconnect, q.NATSURL)

return
}

log.Printf("Reconnection (%d/%d) to %s failed\n", i+1, q.maxReconnect, q.NATSURL)
}
bartsmykla marked this conversation as resolved.
Show resolved Hide resolved

log.Printf("Reconnection limit (%d) reached\n", q.maxReconnect)
}

// CreateNatsQueue ready for asynchronous processing
func CreateNatsQueue(address string, port int, clientConfig NatsConfig) (*NatsQueue, error) {
var err error
Expand All @@ -27,30 +72,33 @@ func CreateNatsQueue(address string, port int, clientConfig NatsConfig) (*NatsQu
clientID := clientConfig.GetClientID()
clusterID := "faas-cluster"

nc, err := stan.Connect(clusterID, clientID, stan.NatsURL(natsURL))
queue1 := NatsQueue{
nc: nc,
ClientID: clientID,
ClusterID: clusterID,
NATSURL: natsURL,
Topic: "faas-request",
ClientID: clientID,
ClusterID: clusterID,
NATSURL: natsURL,
Topic: "faas-request",
maxReconnect: clientConfig.GetMaxReconnect(),
reconnectDelay: clientConfig.GetReconnectDelay(),
ncMutex: &sync.RWMutex{},
}

err = queue1.connect()

return &queue1, err
}

// Queue request for processing
func (q *NatsQueue) Queue(req *queue.Request) error {
var err error

fmt.Printf("NatsQueue - submitting request: %s.\n", req.Function)

out, err := json.Marshal(req)
if err != nil {
log.Println(err)
}

err = q.nc.Publish(q.Topic, out)
q.ncMutex.RLock()
nc := q.nc
q.ncMutex.RUnlock()

return err
return nc.Publish(q.Topic, out)
}
17 changes: 17 additions & 0 deletions handler/nats_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ package handler

import (
"os"
"time"

"github.com/openfaas/nats-queue-worker/nats"
)

type NatsConfig interface {
GetClientID() string
GetMaxReconnect() int
GetReconnectDelay() time.Duration
}

type DefaultNatsConfig struct {
maxReconnect int
reconnectDelay time.Duration
}

func NewDefaultNatsConfig(maxReconnect int, reconnectDelay time.Duration) DefaultNatsConfig {
return DefaultNatsConfig{maxReconnect, reconnectDelay}
}

// GetClientID returns the ClientID assigned to this producer/consumer.
Expand All @@ -19,6 +28,14 @@ func (DefaultNatsConfig) GetClientID() string {
return getClientID(val)
}

func (c DefaultNatsConfig) GetMaxReconnect() int {
return c.maxReconnect
}

func (c DefaultNatsConfig) GetReconnectDelay() time.Duration {
return c.reconnectDelay
}

func getClientID(hostname string) string {
return "faas-publisher-" + nats.GetClientID(hostname)
}