Sha256: 9976646e76eddd042d69e108274c3f7e4fb17bd23eb34b3e17c9f4a00f667dbb

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

# frozen_string_literal: true

module SplitIoClient
  module Engine
    class PushManager
      def initialize(config, sse_handler, api_key)
        @config = config
        @sse_handler = sse_handler
        @auth_api_client = AuthApiClient.new(@config)
        @api_key = api_key
        @back_off = SplitIoClient::SSE::EventSource::BackOff.new(@config.auth_retry_back_off_base, 1)
      end

      def start_sse
        response = @auth_api_client.authenticate(@api_key)

        @config.logger.debug("Auth service response push_enabled: #{response[:push_enabled]}") if @config.debug_enabled
        if response[:push_enabled]
          @sse_handler.start(response[:token], response[:channels])
          schedule_next_token_refresh(120)
          @back_off.reset
        else
          stop_sse
        end

        schedule_next_token_refresh(@back_off.interval) if response[:retry]
      rescue StandardError => e
        @config.logger.error("start_sse: #{e.inspect}")
      end

      def stop_sse
        @sse_handler.process_disconnect if @sse_handler.sse_client.nil?
        @sse_handler.stop
      end

      private

      def schedule_next_token_refresh(time)
        @config.threads[:schedule_next_token_refresh] = Thread.new do
          begin
            @config.logger.debug("schedule_next_token_refresh refresh in #{time} seconds.") if @config.debug_enabled
            sleep(time)
            @config.logger.debug('schedule_next_token_refresh starting ...') if @config.debug_enabled
            stop_sse
            sleep(1)
            start_sse
          rescue StandardError => e
            @config.logger.debug("schedule_next_token_refresh error: #{e.inspect}") if @config.debug_enabled
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
splitclient-rb-7.1.0.pre.rc11-java lib/splitclient-rb/engine/push_manager.rb
splitclient-rb-7.1.0.pre.rc11 lib/splitclient-rb/engine/push_manager.rb
splitclient-rb-7.1.0.pre.rc10-java lib/splitclient-rb/engine/push_manager.rb
splitclient-rb-7.1.0.pre.rc10 lib/splitclient-rb/engine/push_manager.rb