From 98ee78b0a7835f74e256a00dc9f3c7121f5a5f2c Mon Sep 17 00:00:00 2001 From: a <a@tuxpa.in> Date: Fri, 4 Aug 2023 04:16:27 -0500 Subject: [PATCH] what an awful shitty implementation you wrote --- contrib/client/reconnecting.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/contrib/client/reconnecting.go b/contrib/client/reconnecting.go index ebafc1c..89f6efe 100644 --- a/contrib/client/reconnecting.go +++ b/contrib/client/reconnecting.go @@ -11,6 +11,7 @@ import ( type Reconnecting struct { dialer func(ctx context.Context) (jrpc.Conn, error) base codec.Conn + alive bool middleware []codec.Middleware mu sync.Mutex @@ -24,7 +25,31 @@ func NewReconnecting(dialer func(ctx context.Context) (jrpc.Conn, error)) *Recon } func (r *Reconnecting) getClient(ctx context.Context) (jrpc.Conn, error) { - + reconnect := func() error { + conn, err := r.dialer(ctx) + if err != nil { + return err + } + r.base = conn + r.alive = true + return nil + } + r.mu.Lock() + defer r.mu.Unlock() + if r.base == nil { + err := reconnect() + if err != nil { + return nil, err + } + } + select { + case <-r.base.Closed(): + err := reconnect() + if err != nil { + return nil, err + } + default: + } return r.base, nil } -- GitLab