Class: AuthService

Inherits:
Closeable show all
Defined in:
lib/ff/ruby/server/sdk/api/auth_service.rb

Instance Method Summary collapse

Constructor Details

#initialize(connector, callback, logger, retry_delay_ms = 6000) ⇒ AuthService

Returns a new instance of AuthService.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ff/ruby/server/sdk/api/auth_service.rb', line 5

def initialize(connector, callback, logger, retry_delay_ms = 6000)

  unless connector.kind_of?(Connector)
    raise "The 'connector' parameter must be of '" + Connector.to_s + "' data type"
  end

  unless callback.kind_of?(ClientCallback)
    raise "The 'callback' parameter must be of '" + ClientCallback.to_s + "' data type"
  end

  @logger = logger
  @callback = callback
  @connector = connector
  @retry_delay_ms = retry_delay_ms
  @authenticated = false
end

Instance Method Details

#closeObject



52
53
54
# File 'lib/ff/ruby/server/sdk/api/auth_service.rb', line 52

def close
  stop_async
end

#start_asyncObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/ff/ruby/server/sdk/api/auth_service.rb', line 22

def start_async
  @logger.debug "Async starting: " + self.to_s

  @thread = Thread.new :report_on_exception => true do
    attempt = 1
    until @authenticated do
      http_code = @connector.authenticate

      if http_code == 200
        @authenticated = true
        @callback.on_auth_success
        stop_async
      elsif should_retry_http_code http_code
        delay_ms = @retry_delay_ms * [10, attempt].min
        @logger.warn "Got HTTP code #{http_code} while authenticating on attempt #{attempt}, will retry in #{delay_ms} ms"
        sleep(delay_ms/1000)
        attempt += 1
        SdkCodes::warn_auth_retying @logger, attempt
      else
        @logger.warn "Auth Service got HTTP code #{http_code} while authenticating, will not attempt to reconnect"
        @callback.on_auth_failed
        stop_async
        next
      end
    end
  end

  @thread.run
end