From 7ed68b8cc42221481c5a67ec462f45c0a1d36d06 Mon Sep 17 00:00:00 2001 From: Gensuke Toshiro Date: Wed, 29 Sep 2021 10:27:59 +0900 Subject: [PATCH] Retry initial connection --- client.go | 13 ++++++++++++- rpc.go | 7 ++----- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/client.go b/client.go index 1d036ae..801c859 100644 --- a/client.go +++ b/client.go @@ -3,6 +3,7 @@ package amqp import ( "fmt" "context" + "time" "sync" "github.com/streadway/amqp" @@ -49,7 +50,17 @@ func (f backendFactory) New(remote *config.Backend) proxy.Proxy { } func (f backendFactory) newChannel(path string) (*amqp.Channel, closer, error) { - conn, err := amqp.Dial(path) + var ( + conn *amqp.Connection + err error + ) + for { + conn, err = amqp.Dial(path) + if err == nil { + break + } + time.Sleep(time.Duration(3) * time.Second) + } if err != nil { return nil, nopCloser, err } diff --git a/rpc.go b/rpc.go index 0f3dac1..f057bda 100644 --- a/rpc.go +++ b/rpc.go @@ -107,11 +107,8 @@ func (f backendFactory) initRpcChannel(ctx context.Context, cfg *rpcCfg, dns str if !ok { break } - for { - time.Sleep(time.Duration(3) * time.Second) - if err := f.initRpcChannel(ctx, cfg, dns, rch); err == nil { - break - } + if err := f.initRpcChannel(ctx, cfg, dns, rch); err == nil { + break } case reply, ok := <-replies: if !ok {