Just to conclude the thread, the pull request was created and merged 
(thanks!), so after it is released, this is how you would apply the 
expiration timeout before *:preconnect* kicks in:

*DB = Sequel.connect(*
*  adapter: 'mysql2', host: '127.0.0.1', port: '49153', database: 'my_db',*
*  user: 'root', password: 'my_pass',*

*  ssl_mode: :disabled,*
  *preconnect_extensions: :connection_expiration,*
*  preconnect: true,*
*  before_preconnect: proc { |db| db.pool.connection_expiration_timeout 
= 100 }*
*)*

Note the new option, *:before_preconnect*, which accepts a proc that will 
be passed the DB instance, after *:preconnect_extensions* are loaded, but 
before the connections are made.

Best,
Marcelo
On Sunday, October 17, 2021 at 5:24:33 PM UTC+2 Jeremy Evans wrote:

> On Sun, Oct 17, 2021 at 4:49 AM Marcelo <marcelovit...@gmail.com> wrote:
>
>> Hello all,
>>
>> I'm adding the :connection_expiration extension to a DB instance, but I 
>> need to change its default expiration time with, for example:
>> DB.pool.connection_expiration_timeout = 100
>>
>> However, this new expiration time is not applied to the initial 
>> connection, but rather the default value of 14400, and I'm having to resort 
>> to manually disconnecting and reconnecting:
>>
>> *DB = Sequel.connect(*
>> *  adapter: 'mysql2', host: '127.0.0.1', port: '49153', 
>> database: 'my_db',*
>> *  user: 'root', password: 'my_pass',*
>>
>> *  ssl_mode: :disabled,*
>> *  extensions: :connection_expiration,*
>>
>> *)*
>>
>> *def expiration_times(db)*
>> *  
>> db.pool.instance_variable_get(:@connection_expiration_timestamps).values.map{|v|
>>  
>> v[1]}.join(', ')*
>> *end*
>>
>> *DB.pool.connection_expiration_timeout = 100*
>> *print "Initial values: "*
>> *puts expiration_times(DB)*
>>
>> *DB.disconnect*
>> *print "After disconnect: "*
>> *puts expiration_times(DB)*
>>
>> *DB[:users].first*
>> *print "After reconnect: "*
>> *puts expiration_times(DB)*
>>
>> *# => Initial values: 14400.0*
>> *# => After disconnect: *
>> *# => After reconnect: 100.0*
>>
>> That's still passable, but if I additionally want to enable :preconnect, 
>> this doesn't work at all, since after DB.disconnect, I'd have to call the 
>> private method `preconnect` directly.
>>
>> *DB = Sequel.connect(*
>> *  adapter: 'mysql2', host: '127.0.0.1', port: '49153', 
>> database: 'my_db',*
>> *  user: 'root', password: 'my_pass',*
>>
>> *  ssl_mode: :disabled,*
>> *  preconnect_extensions: :connection_expiration,*
>> *  preconnect: true*
>> *)*
>>
>>
>> My specific use case is with the connection_expiration extension, but I'm 
>> interested if there's in general a way to configure the extension prior to 
>> a connection being established, and especially for preconnections.
>>
>
> It doesn't look like there is a way to do what you want currently.  We 
> would probably need to add an option like :before_preconnect to do so.  I'm 
> definitely open to that.  If you want to submit a pull request for it, 
> please do so.  Otherwise, I can probably take care of it before the next 
> release.
>
> 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 sequel-talk+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/f6cf5dc9-a8b1-45f9-9bc8-d596e7dec576n%40googlegroups.com.

Reply via email to