@@ -140,18 +140,12 @@ recover(internal, start, Data = #statem_data{endpoints = Endpoints, connection_m
140140 rabbit_log :debug (" etcd v3 API client will attempt to connect, endpoints: ~ts " ,
141141 [string :join (Endpoints , " ," )]),
142142 maybe_demonitor (Ref ),
143- {Transport , TransportOpts } = pick_transport (Data ),
144- case Transport of
145- tcp -> rabbit_log :info (" etcd v3 API client is configured to connect over plain TCP, without using TLS" );
146- tls -> rabbit_log :info (" etcd v3 API client is configured to use TLS" )
147- end ,
148- ConnName = ? ETCD_CONN_NAME ,
149- case connect (ConnName , Endpoints , Transport , TransportOpts , Data ) of
143+ case connect (? ETCD_CONN_NAME , Endpoints , Data ) of
150144 {ok , Pid } ->
151145 rabbit_log :debug (" etcd v3 API client connection: ~tp " , [Pid ]),
152146 rabbit_log :debug (" etcd v3 API client: total number of connections to etcd is ~tp " , [length (eetcd_conn_sup :info ())]),
153147 {next_state , connected , Data # statem_data {
154- connection_name = ConnName ,
148+ connection_name = ? ETCD_CONN_NAME ,
155149 connection_pid = Pid ,
156150 connection_monitor = monitor (process , Pid )
157151 }};
@@ -213,8 +207,12 @@ connected({call, From}, {unlock, GeneratedKey}, Data = #statem_data{connection_n
213207connected ({call , From }, register , Data = # statem_data {connection_name = Conn }) ->
214208 Ctx = registration_context (Conn , Data ),
215209 Key = node_key (Data ),
216- eetcd_kv :put (Ctx , Key , registration_value (Data )),
217- rabbit_log :debug (" etcd peer discovery: put key ~tp , done with registration" , [Key ]),
210+ case eetcd_kv :put (Ctx , Key , registration_value (Data )) of
211+ {ok , _ } ->
212+ rabbit_log :debug (" etcd peer discovery: put key ~tp , done with registration" , [Key ]);
213+ {error , Reason } ->
214+ rabbit_log :error (" etcd peer discovery: put key ~tp failed: ~p " , [Key , Reason ])
215+ end ,
218216 gen_statem :reply (From , ok ),
219217 keep_state_and_data ;
220218connected ({call , From }, unregister , Data = # statem_data {connection_name = Conn }) ->
@@ -320,20 +318,21 @@ error_is_already_started({_Endpoint, already_started}) ->
320318error_is_already_started ({_Endpoint , _ }) ->
321319 false .
322320
323- connect (Name , Endpoints , Transport , TransportOpts , Data ) ->
321+ connect (Name , Endpoints , Data ) ->
324322 case eetcd_conn :lookup (Name ) of
325323 {ok , Pid } when is_pid (Pid ) ->
326324 {ok , Pid };
327325 {error , eetcd_conn_unavailable } ->
328- do_connect (Name , Endpoints , Transport , TransportOpts , Data )
326+ do_connect (Name , Endpoints , Data )
329327 end .
330328
331- do_connect (Name , Endpoints , Transport , TransportOpts , Data = # statem_data {username = Username }) ->
329+ do_connect (Name , Endpoints , Data = # statem_data {username = Username }) ->
330+ Opts = connection_options (Data ),
332331 case Username of
333332 undefined -> rabbit_log :info (" etcd peer discovery: will connect to etcd without authentication (no credentials configured)" );
334333 _ -> rabbit_log :info (" etcd peer discovery: will connect to etcd as user '~ts '" , [Username ])
335334 end ,
336- case eetcd :open (Name , Endpoints , connection_options ( Data ), Transport , TransportOpts ) of
335+ case eetcd :open (Name , Endpoints , Opts ) of
337336 {ok , Pid } -> {ok , Pid };
338337 {error , Errors0 } ->
339338 Errors = case is_list (Errors0 ) of
@@ -354,16 +353,6 @@ do_connect(Name, Endpoints, Transport, TransportOpts, Data = #statem_data{userna
354353 end
355354 end .
356355
357- connection_options (# statem_data {username = Username , obfuscated_password = Password }) ->
358- SharedOpts = [{mode , random }],
359- case {Username , Password } of
360- {undefined , _ } -> SharedOpts ;
361- {_ , undefined } -> SharedOpts ;
362- {UVal , PVal } ->
363- [{name , UVal }, {password , to_list (deobfuscate (PVal ))}] ++ SharedOpts
364- end .
365-
366-
367356obfuscate (undefined ) -> undefined ;
368357obfuscate (Password ) ->
369358 credentials_obfuscation :encrypt (to_binary (Password )).
@@ -379,9 +368,9 @@ disconnect(ConnName, #statem_data{connection_monitor = Ref}) ->
379368unregister (Conn , Data = # statem_data {node_key_lease_id = LeaseID , node_lease_keepalive_pid = KAPid }) ->
380369 Ctx = unregistration_context (Conn , Data ),
381370 Key = node_key (Data ),
382- eetcd_kv :delete (Ctx , Key ),
371+ _ = eetcd_kv :delete (Ctx , Key ),
383372 rabbit_log :debug (" etcd peer discovery: deleted key ~ts , done with unregistration" , [Key ]),
384- eetcd_lease :revoke (Ctx , LeaseID ),
373+ _ = eetcd_lease :revoke (Ctx , LeaseID ),
385374 exit (KAPid , normal ),
386375 rabbit_log :debug (" etcd peer discovery: revoked a lease ~tp for node key ~ts " , [LeaseID , Key ]),
387376 ok .
@@ -429,7 +418,24 @@ normalize_settings(Map) when is_map(Map) ->
429418 maps :merge (maps :without ([etcd_prefix , lock_wait_time ], Map ),
430419 #{endpoints => AllEndpoints }).
431420
432- pick_transport (# statem_data {tls_options = []}) ->
433- {tcp , []};
434- pick_transport (# statem_data {tls_options = Opts }) ->
435- {tls , Opts }.
421+ connection_options (# statem_data {tls_options = TlsOpts ,
422+ username = Username ,
423+ obfuscated_password = Password }) ->
424+ Opts0 = case TlsOpts of
425+ [] ->
426+ rabbit_log :info (" etcd v3 API client is configured to use plain TCP (without TLS)" ),
427+ [{transport , tcp }];
428+ _ ->
429+ rabbit_log :info (" etcd v3 API client is configured to use TLS" ),
430+ [{transport , tls },
431+ {tls_opts , TlsOpts }]
432+ end ,
433+ Opts = [{mode , random } | Opts0 ],
434+ case Username =:= undefined orelse
435+ Password =:= undefined of
436+ true ->
437+ Opts ;
438+ false ->
439+ [{name , Username },
440+ {password , to_list (deobfuscate (Password ))}] ++ Opts
441+ end .
0 commit comments