@@ -168,9 +168,9 @@ defmodule NebulexRedisAdapter do
168168 ]
169169 ]
170170
171- By default, the adapter uses `NebulexRedisAdapter.ClientCluster.Keyslot` for the
172- keyslot. Besides, if `:jchash` is defined as dependency, the adapter will use
173- consistent-hashing automatically. However, you can also provide your own
171+ By default, the adapter uses `NebulexRedisAdapter.ClientCluster.Keyslot` for
172+ the keyslot. Besides, if `:jchash` is defined as dependency, the adapter will
173+ use consistent-hashing automatically. However, you can also provide your own
174174 implementation by implementing the `Nebulex.Adapter.Keyslot` and set it into
175175 the `:keyslot` option. For example:
176176
@@ -194,12 +194,12 @@ defmodule NebulexRedisAdapter do
194194
195195 ### Client-side cluster options
196196
197- In addition to shared options, `:client_side_cluster` mode supports the following
198- options:
197+ In addition to shared options, `:client_side_cluster` mode supports the
198+ following options:
199199
200200 * `:nodes` - The list of nodes the adapter will setup the cluster with;
201- a pool of connections is established per node. The `:client_side_cluster` mode
202- enables resilience to be able to survive in case any node(s) gets
201+ a pool of connections is established per node. The `:client_side_cluster`
202+ mode enables resilience to be able to survive in case any node(s) gets
203203 unreachable. For each element of the list, we set the configuration
204204 for each node, such as `:conn_opts`, `:pool_size`, etc.
205205
@@ -333,13 +333,19 @@ defmodule NebulexRedisAdapter do
333333
334334 @ impl true
335335 def init ( opts ) do
336- # required cache name
336+ # Required cache name
337337 name = opts [ :name ] || Keyword . fetch! ( opts , :cache )
338338
339- # adapter mode
339+ # Init stats
340+ stats_counter = Stats . init ( opts )
341+
342+ # Adapter mode
340343 mode = Keyword . get ( opts , :mode , :standalone )
341344
342- # pool size
345+ # Local registry
346+ registry = normalize_module_name ( [ name , Registry ] )
347+
348+ # Resolve the pool size
343349 pool_size =
344350 get_option (
345351 opts ,
@@ -349,60 +355,62 @@ defmodule NebulexRedisAdapter do
349355 System . schedulers_online ( )
350356 )
351357
352- # init the specs according to the adapter mode
353- { children , default_keyslot } = do_init ( mode , name , pool_size , opts )
354-
355- # keyslot module for selecting nodes
358+ # Keyslot module for selecting nodes
356359 keyslot =
357- opts
358- |> Keyword . get ( : keyslot, default_keyslot )
359- |> assert_behaviour ( Nebulex.Adapter.Keyslot , "keyslot" )
360+ if keyslot = Keyword . get ( opts , :keyslot ) do
361+ assert_behaviour ( keyslot , Nebulex.Adapter.Keyslot , "keyslot" )
362+ end
360363
361- # cluster nodes
364+ # Cluster nodes
362365 nodes =
363366 for { node_name , node_opts } <- Keyword . get ( opts , :nodes , [ ] ) do
364- { node_name , Keyword . get ( node_opts , :pool_size , System . schedulers_online ( ) ) }
367+ { node_name , Keyword . get ( node_opts , :pool_size , pool_size ) }
365368 end
366369
367- # init stats
368- stats_counter = Stats . init ( opts )
369-
370+ # Init adapter metadata
370371 adapter_meta = % {
372+ cache_pid: self ( ) ,
371373 name: name ,
372374 mode: mode ,
373375 keyslot: keyslot ,
374376 nodes: nodes ,
375377 pool_size: pool_size ,
376378 stats_counter: stats_counter ,
379+ registry: registry ,
377380 started_at: DateTime . utc_now ( ) ,
378381 default_dt: Keyword . get ( opts , :default_data_type , :object ) ,
379382 telemetry: Keyword . fetch! ( opts , :telemetry ) ,
380383 telemetry_prefix: Keyword . fetch! ( opts , :telemetry_prefix )
381384 }
382385
386+ # Init the connections child spec according to the adapter mode
387+ { conn_child_spec , adapter_meta } = do_init ( adapter_meta , opts )
388+
389+ # Build the child spec
383390 child_spec =
384391 Nebulex.Adapters.Supervisor . child_spec (
385392 name: normalize_module_name ( [ name , Supervisor ] ) ,
386393 strategy: :one_for_all ,
387- children: [ { NebulexRedisAdapter.BootstrapServer , adapter_meta } | children ]
394+ children: [
395+ { NebulexRedisAdapter.BootstrapServer , adapter_meta } ,
396+ { Registry , name: registry , keys: :unique } ,
397+ conn_child_spec
398+ ]
388399 )
389400
390401 { :ok , child_spec , adapter_meta }
391402 end
392403
393- defp do_init ( :standalone , name , pool_size , opts ) do
394- { :ok , children } = Connection . init ( name , pool_size , opts )
395- { children , ClientCluster.Keyslot }
404+ defp do_init ( % { mode: :standalone } = adapter_meta , opts ) do
405+ Connection . init ( adapter_meta , opts )
396406 end
397407
398- defp do_init ( :client_side_cluster , _name , _pool_size , opts ) do
399- { :ok , children } = ClientCluster . init ( opts )
400- { children , ClientCluster.Keyslot }
408+ defp do_init ( % { mode: :redis_cluster } = adapter_meta , opts ) do
409+ RedisCluster . init ( adapter_meta , opts )
401410 end
402411
403- defp do_init ( :redis_cluster , name , pool_size , opts ) do
404- { :ok , children } = RedisCluster . init ( name , pool_size , opts )
405- { children , RedisCluster.Keyslot }
412+ defp do_init ( % { mode: :client_side_cluster } = adapter_meta , opts ) do
413+ ClientCluster . init ( adapter_meta , opts )
406414 end
407415
408416 ## Nebulex.Adapter.Entry
0 commit comments