You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Dynamically updated MultiHostDataSource host list #5064
var dataSourceBuilder = new NpgsqlDataSourceBuilder(dbSettings.Value.ConnectionString);
dataSourceBuilder.BuildMultiHost();
It seems that this implementation implies a static connection string, but in our scenario we have our own ConfigurationProvider where we periodically update the list of hosts, for example, the database stopped coping with the load and additional replicas were added to it, it mean that we need to restart service. We need something like UsePeriodicPasswordProvider, ability update connection strings periodically
By design, a data source can only have one connection string; since the connection string can significantly affect connection characteristics and connections are typically pooled, allowing the connection string to be changed would mean that connections with different characteristics are returned randomly... It would effectively make the data source abstraction meaningless.
However, it may make sense to specifically allow hosts to be added/removed on the data source, a bit like how we allow the password to be set on it (not sure a callback like UsePeriodicPasswordProvider makes sense here). I'll keep this open to see what the other team members think.
Until then, it shouldn't be too hard to simply create a new data source when your host list changes, transition to it and dispose the old one.
Until then, it shouldn't be too hard to simply create a new data source when your host list changes, transition to it and dispose the old one.
It's not hard it's just that the code becomes icki as you need to start thinking about thread safety. I assume most of the apps initialise the data source with the hosts in the constructor of a component registered in the DI as a singleton.
It would be great if this could be done under the hood by npgsql 🙇
Activity
roji commentedon May 17, 2023
By design, a data source can only have one connection string; since the connection string can significantly affect connection characteristics and connections are typically pooled, allowing the connection string to be changed would mean that connections with different characteristics are returned randomly... It would effectively make the data source abstraction meaningless.
However, it may make sense to specifically allow hosts to be added/removed on the data source, a bit like how we allow the password to be set on it (not sure a callback like UsePeriodicPasswordProvider makes sense here). I'll keep this open to see what the other team members think.
Until then, it shouldn't be too hard to simply create a new data source when your host list changes, transition to it and dispose the old one.
careless6666 commentedon May 17, 2023
Thanks for the quick response, will try the suggested solution
roji commentedon May 22, 2023
Opening to consider supporting this better in Npgsql, in the backlog.
benlongo commentedon Jun 12, 2023
[-]MultiHostDataSource with dynamic connection string[/-][+]Dynamically updated MultiHostDataSource host list[/+]jernejg commentedon Aug 13, 2024
It's not hard it's just that the code becomes icki as you need to start thinking about thread safety. I assume most of the apps initialise the data source with the hosts in the constructor of a component registered in the DI as a singleton.
It would be great if this could be done under the hood by npgsql 🙇