anchor.diff:  %% @end
   %%--------------------------------------------------------------------
   -spec handle(Request) -> Result when
  -    Request :: healthcheck | start_provider_connection | refresh_subscription |
  +    Request :: healthcheck | ensure_connection_running | refresh_subscription |
       {process_updates, Updates} | {'EXIT', pid(), ExitReason :: term()},
       Updates :: [#sub_update{}],
       Result :: nagios_handler:healthcheck_response() | ok | {ok, Response} |
  
positive.code: 
  %%--------------------------------------------------------------------
  %% @doc @private
  %% Attempts to start the connection. The procedure is wrapped in a global lock
  %% and reattempted in increasing intervals to prevent the provider from
  %% flooding OneZone with connection requests.
  %% @end
  %%--------------------------------------------------------------------
  -spec start_provider_connection() -> ok.
  start_provider_connection() ->
      critical_section:run(subscriptions_wss, fun() ->
          Result = try
              case subscription_wss:start_link() of
                  {ok, Pid} ->
                      ?info("Subscriptions connection started ~p", [Pid]),
                      ok;
                  {error, R1} ->
                      {error, R1}
              end
          catch
              E:R2 ->
                  {E, R2}
          end,
          case Result of
              ok ->
                  reset_reconnect_interval();
              {Type, Reason} ->
                  Interval = increase_reconnect_interval(),
                  ?error("Subscriptions connection failed to start - ~p:~p. "
                  "Next retry not sooner than ~p seconds.", [
                      Type, Reason, round(Interval / 1000)
                  ]),
                  % Sleep, blocking the lock for some time - this ensures that
                  % next attempt will occur after intended interval.
                  timer:sleep(Interval)
          end
      end).
  
  
  %%--------------------------------------------------------------------
  %% @doc @private
  %% Increases the reconnect interval according to RECONNECT_INTERVAL_INCREASE_RATE,
  %% saves the new value to worker host state and returns the old interval.
  %% @end
  %%--------------------------------------------------------------------
  -spec increase_reconnect_interval() -> integer().
  increase_reconnect_interval() ->
      Interval = case worker_host:state_get(?MODULE, reconnect_interval) of
          undefined -> ?INITIAL_RECONNECT_INTERVAL;
          Value -> Value
      end,
      worker_host:state_put(?MODULE, reconnect_interval, min(
          Interval * ?RECONNECT_INTERVAL_INCREASE_RATE,
          ?MAX_RECONNECT_INTERVAL
      )),
      Interval.
  
  
  %%--------------------------------------------------------------------
  %% @doc @private
  %% Resets the reconnect interval to its initial value.
  %% @end
  %%--------------------------------------------------------------------
  -spec reset_reconnect_interval() -> ok.
  reset_reconnect_interval() ->
      worker_host:state_put(?MODULE, reconnect_interval, ?INITIAL_RECONNECT_INTERVAL).
  
negative.code: -include_lib("ctool/include/oz/oz_runner.hrl").
  
  % Definitions of reconnect intervals for subscriptions websocket client.
  -define(INITIAL_RECONNECT_INTERVAL_SEC, 2).
  -define(RECONNECT_INTERVAL_INCREASE_RATE, 2).
  -define(MAX_RECONNECT_INTERVAL, timer:minutes(15)).
  
  