Dependency Injection automatically reload connection when settings change

I’m trying to use Couchbase with a combination of Hashicorp Vault and have the vault agent automatically update the app settings file when Vault rotates the credentials. However, the ClusterProvider implementation in the DI project does not support reloading when the settings change. Has anyone implemented a ClusterProvider that would handle this?

@jblackburn

I don’t know of one, but I’ve been thinking that it would be a nice feature to have in the official DI library. In theory, it could be done by:

  • Change ClusterProvider to subscribe to IOptionsMonitor<ClusterOptions> rather than the singleton IOptions<ClusterOptions>
  • Swap in the new _cluster when options change (would probably need to mark it _volatile)
  • Dispose of the old cluster, presumably on some kind of delay to give in-flight operations and services which already requested an IBucket or ICluster a chance to complete
  • Perhaps some auto-bootstrap if the previous cluster was bootstrapped?
  • Do something with the caching in BucketProvider

My main concern is with the overhead. Bootstrapping a cluster has a lot of cost and shouldn’t be undertaken lightly. However, IOptionsMonitor will trigger with a new ClusterOptions anytime ANY configuration value is changed, even if the Couchbase-related settings are all identical. I think we’d want some way to compare the actual settings to see if they’re different to avoid an unnecessary ICluster replacement. But some of the settings in ClusterOptions don’t really lend themselves to comparison, like injecting services/loggers/etc. This is where I’ve been stuck so far in my thinking.

@btburnett3

Thank you for the quick response. This is similar to how I’m approaching this, but I’m creating a custom ClusterOptions that only captures the fields that I want to monitor.

We’re looking to automatically rotate the credential after multiple weeks, so I don’t think the overhead is going to be a concern.