FAQ
I wrote a function that calls multiple rpc servers with the same args and
reply and returns the first one that comes back. Is this a bad idea? Is
it possible that the reply will get corrupted since there will be multiple
calls to client.codec.ReadResponseBody(call.Reply)?

Here is the function:

func FirstTimeout(hosts []string, method string, args interface{}, reply
interface{}, timeout time.Duration) error {
ch := make(chan *rpc.Call, len(hosts))
numCalls := 0
var oerr error
for _, host := range hosts {
c, err := clientcache.Client(host)
if err != nil {
oerr = err
continue
}
c.Go(method, args, reply, ch)
numCalls++
}

if numCalls == 0 {
return oerr
}

select {
case r := <-ch:
reply = r.Reply
return r.Error
case <-time.After(timeout):
return fmt.Errorf("timeout calling %s on %v (%s)", method, hosts, timeout)
}

panic("never reached")
}

Thanks!

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMay 7, '13 at 5:26p
activeMay 7, '13 at 5:26p
posts1
users1
websitegolang.org

1 user in discussion

Jerry Worthey: 1 post

People

Translate

site design / logo © 2023 Grokbase