Sorry, yea, I mistyped...I am doing this on my message consumer:

DB.add_server(subdomain.to_sym => { host: ..., database: ..., user: ..., 
password: ...})

I might just use the arbitrary server extension and just catch exceptions 
if a database doesn't exist on the server. Thanks for the quick response.

On Saturday, March 23, 2019 at 8:32:21 PM UTC-4, Jeremy Evans wrote:
>
> On Saturday, March 23, 2019 at 4:59:07 PM UTC-7, [email protected] 
> <javascript:> wrote:
>>
>> So, I'm using sharding to provide multi-tenancy to my application with 
>> each customer getting their own database and a shared api pool that will 
>> listen to a message queue to add connections to the pool as new customers 
>> signup to prevent restarting the api servers. In my api, I do something 
>> like this:
>>
>> # connection initialization
>> servers = {}
>> # connect to configuration db and read tenant configs
>> TENANT_DB = Sequel.connect(host: ...)
>> TENANT_DB[:tenants_configs].each { |config| servers[config[:subdomain]] = 
>> { host: ... } }
>> TENANT_DB.disconnect
>> DB = Sequel.connect( { host: ... }, servers: servers, logger: logger )
>>
>> # around_action
>> DB.with_server(subdomain.to_sym) do
>>   yield
>> end
>>
>> # message consumer
>> DB.add_server(host: ..., database: ..., user: ..., password: ...)
>>
>
> This doesn't look correct.  The method would be DB.add_servers, and it 
> takes a hash keyed by the server symbols you want to add.  I'm guessing you 
> would want something like this:
>
>   DB.add_servers(subdomain.to_sym => {host: ..., database: ..., user: ..., 
> password: ...})
>  
> You want to call that before calling DB.with_server(subdomain.to_sym), 
> though I'm not sure that is strictly required.
>
> If the server was passed in during initialization, it works as expected, 
>> but using newly added server, the queries inside the with_server block are 
>> running against the default server. I was looking at using the arbitrary 
>> server extension as an alternative to what I'm encountering, but didn't 
>> like the thought of maintaining a global variable for all the "missing" 
>> configurations and not sure of potential performance issues using that 
>> approach. I'm not sure if I overlooked something or just not doing 
>> something incorrectly.
>>
>
> For an arbitrary number of customers, using separate servers for each may 
> eventually run into resource limits.  You may want to use the 
> arbitrary_servers extension with the server_block extension in an 
> around_action (instead of using the add_servers approach), even though that 
> will require connecting and disconnecting from the database for each 
> request.
>
> I'm guessing the reason it fell back to the default server is that is the 
> default way to handle unknown shards. See 
> http://sequel.jeremyevans.net/rdoc/files/doc/sharding_rdoc.html#label-Sharding
>  
> for a way to raise an error for unknown shards.
>
> 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 https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to