On Friday, May 22, 2015 at 7:33:20 AM UTC-7, Joachim Nolten wrote:
>
> Following the example in 
> http://sequel.jeremyevans.net/rdoc/files/doc/sharding_rdoc.html does not 
> seem to work for me.
>
> First:
>
> DB=Sequel.connect('postgres://master_server/database',    :servers=>{:
> read_only=>proc{|db| {:host=>db.get_slave_host}}})
> def DB.get_slave_host
>   @current_host ||= -1
>   "slave_server#{(@current_host+=1)%4}"
> end
>
>
This should only be an issue if the adapter is creating a new connection 
right away, before the method is defined.  I can't replicate that in my 
local setup with the code you posted, but depending on the other extensions 
you are using, it is possible.  You could always just do:

DB=Sequel.connect('postgres://master_server/database',    :servers=>{:
read_only=>proc{|db| db.respond_to?(:get_slave_host) ? {:host=>db.
get_slave_host} : {}}})
 

>
> Results in a 'method not defined' error (this is in Ruby 2.1.3). I have 
> changed my code to define the method inside the proc:
>
> DB=Sequel.connect('postgres://master_server/database',    :servers=>{:
> read_only=>proc{ |db| 
> unless db.respond_to?(:get_slave_host)
>   def db.get_slave_host
>     @current_host ||= -1
>     "slave_server#{(@current_host+=1)%4}"
>   end
> end
> {:host=>db.get_slave_host}}})
>
>
> But the method only appears to be called twice: Once at load time and once 
> for the first query.
> After that, the last return value is used, which does not result in a 
> round robin selection of the DB servers.
>

Are you running multiple queries concurrently?  If not, then the behavior 
is expected.  Sequel will only create new connections (to other slave 
servers) if they are actually needed, i.e. a request for a connection comes 
in, and there aren't any available connections in the pool.  If you are 
using a single-threaded web server with multiple processes and want to use 
multiple slaves, use @current_host ||= rand(4).  That's actually a better 
default in general, I'll update the documentation to reflect it.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to