On Wed, Mar 31, 2021 at 9:52 AM 'Matt Wilson' via sequel-talk <
[email protected]> wrote:

> When a new connection is being added to a pool, I need a way to check a
> variable on the server and possibly reject the connection. The idea being
> that the new connection would gracefully be closed and the pool would
> attempt to create new connections until the validation was successful or
> some timeout occurred.
>
> At first glance, it seemed the *ConnectionValidator* extension was right
> for the job. However, if I'm reading the source right, it executes the
> validation statement if the connection has been sitting the pool for
> awhile, not when the connection is first created.
>
> I've tried using the `after_connect`  callback option, however there
> doesn't seem to be away for the callback to signal to the pool that the
> current connection should be closed and retried.
>
> This might be a feature request, but I'm hoping I missed something with
> the current options.
>
> I don't think it makes a difference, but if anyone wants context. I use
> mysql2 for an AWS Aurora connection. The behavior of Aurora during a
> failover is to promote a read-only node to the primary read-write node.
> Then (depending on the type of failover) the previous primary stays around,
> but gets demoted to a read-only node. When the app attempts to reconnect
> the severed connections, depending on how DNS resolves, there is a window
> of time when the app could reconnect to the old primary that is now a
> read-only. The recommendation from AWS is to query "*SHOW GLOBAL
> VARIABLES LIKE 'innodb_read_only';*" and if the value is "ON" attempt a
> reconnect.
>

Sequel doesn't currently have direct support for this.  You could probably
do something like:

DB = Sequel.connect("mysql2://...", test: false)
def DB.connect(*)
  while true
    conn = super
    return conn if # connection is valid
    disconnect_connection(conn)
  end
end

I suppose we could offer a way to :after_connect to return a value
indicating a retry, so you wouldn't need to override connect.

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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sequel-talk/CADGZSScYr%3D_cfULbXeuL29BeKDK1%3DBNgaspeC%3DSpZYAgSa7QTw%40mail.gmail.com.

Reply via email to