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

Reconnect #4

Open
ProTip opened this issue Jun 16, 2014 · 4 comments
Open

Reconnect #4

ProTip opened this issue Jun 16, 2014 · 4 comments

Comments

@ProTip
Copy link

ProTip commented Jun 16, 2014

Hello,

What do you think about handling reconnects? Currently there is nothing exposing whether or not the client is still connected nor is there a "Connect" method to attempt a reconnection.

I'm also curious about having the client handle broken connections itself.. I had to do the same stuff with the graphite client (wrap a bunch of broken connection handling code around it).

@blalor
Copy link
Contributor

blalor commented Jun 9, 2015

It's not particularly elegant, but here's what I use:

package main

import (
    log "github.com/Sirupsen/logrus"

    "time"
    "fmt"

    "github.com/amir/raidman"
)

type ReconnectingRiemannClient struct {
    Proto   string
    Host    string
    Port    int
    Timeout time.Duration

    client  *raidman.Client
}

func (self *ReconnectingRiemannClient) SendMulti(evts []*raidman.Event) (err error) {
    if self.client == nil {
        riemannAddr := fmt.Sprintf("%s:%d", self.Host, self.Port)

        log.Infof("connecting to Riemann at %s via %s", riemannAddr, self.Proto)
        self.client, err = raidman.DialWithTimeout(self.Proto, riemannAddr, self.Timeout)

        if err != nil {
            log.Errorf("error connecting to Riemann: %v", err)
            return
        }
    }

    if self.client != nil {
        err = self.client.SendMulti(evts)

        if err != nil {
            self.client.Close()
            self.client = nil
        }
    }

    return
}

@amir
Copy link
Owner

amir commented Jun 20, 2015

Sorry for the long silence. I've read quite a few go projects to see how they handle reconnects since the issue has been created. I'm yet to see one uniform, idiomatic solution to the problem. @blalor's solution is indeed a good one from a library user point of view and we might be able to avail users of this feature via a configuration parameter, perhaps? What do you think?

@blalor
Copy link
Contributor

blalor commented Jun 26, 2015

I'm wondering if it might be better to keep this as a separate implementation that uses a common interface to describe the methods. This is working well for me, but I'm a little uncomfortable having the library wrap itself; seems like it could make maintenance more complex. But as a separate type, its behavior and relationship are more understandable, in my opinion.

@dhruvbansal
Copy link

+1 for bundling some kind of reconnect functionality into the client. Can't speak to @blalor particular implemenation...IANAGP (IANA Go Programmer...).

Metrics are what we rely on when things break so it's nice when our collection tools are robust against intermittent network errors or (say) a restart of the Riemann server (which is how I discovered this bug & issue...).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants