basho/riak-go-client

Help needed with configs [JIRA: CLIENTS-1088]

es-lab opened this issue · 2 comments

Hi,

I'm trying to setup an util for accessing riak.
I've created this func that inits the client and stops it after every query. Is this correct? Or should I init the client one time when the server loads and use that instance for every query? In the examples I can see that it executes one time and the app stops, but how should it work when you have a server that listens for requests?

func Exec(q query) error {
	
	o := &riak.NewClientOptions{
		RemoteAddresses:[]string{
			backend.RiakN1Addr,
			backend.RiakN2Addr,
			backend.RiakN3Addr,
			backend.RiakN4Addr,
			backend.RiakN5Addr,
		},
	}
	
	var client *riak.Client
	client, err := riak.NewClient(o)
	if err != nil {
		return err
	}
	
	defer func() error {
		return client.Stop();
	}()
	
	return q(client)
}

type query func(client *riak.Client) (dbErr error)

using it like this:

err = db.Exec(func(rClient *riak.Client) (dbErr error) {
	//access with rClient
})

The client should only be created once at the start of your application. The riak.Client object is thread-safe and is intended to be long-lived.

Please re-open this issue if you have further questions.

Fixed, or closed via GitHub issues.

[posted via JIRA by Alexander Moore]