added a timeout to the login method

module Util::Timeout added, prepends Base
This commit is contained in:
onyx-and-iris 2024-06-28 10:59:34 +01:00
parent aaee848962
commit 57112fd90a
2 changed files with 23 additions and 3 deletions

View File

@ -4,18 +4,21 @@ module Voicemeeter
include Logging
include Worker
include Events::Director
prepend Util::Timeout
prepend Util::Cache
attr_reader :kind, :midi, :event, :delay, :cache
RATELIMIT = 0.033
DELAY = 0.001
LOGIN_TIMEOUT = 2
def initialize(kind, **kwargs)
@kind = kind
@sync = kwargs[:sync] || false
@ratelimit = kwargs[:ratelimit] || RATELIMIT
@delay = kwargs[:delay] || DELAY
@login_timeout = kwargs[:login_timeout] || LOGIN_TIMEOUT
@event =
Events::Tracker.new(
**(kwargs.select { |k, _| %i[pdirty mdirty ldirty midi].include? k })
@ -30,8 +33,6 @@ module Voicemeeter
def login
CBindings.call(:bind_login, ok: [0, 1]) == 1 and run_voicemeeter(kind.name)
clear_dirty
logger.info "Successfully logged into #{self} version #{version}"
end
def logout
@ -72,7 +73,6 @@ module Voicemeeter
logger.debug "Voicemeeter engine running but the GUI appears to be down... launching."
end
CBindings.call(:bind_run_voicemeeter, kinds[kind_id])
sleep(1)
end
def type

View File

@ -18,6 +18,26 @@ module Voicemeeter
end
end
module Timeout
def login
super
err = nil
start = Time.now
begin
sleep 0.1
logger.info "Successfully logged into #{self} version #{version}"
logger.debug "login time: #{(Time.now - start).round(2)}"
err = nil
rescue Errors::VMCAPIError => e
err = e
retry if Time.now < start + @login_timeout
end
raise Errors::VMError.new "Timeout logging into the api" if err
clear_dirty
end
end
module Cache
def get(name, is_string = false)
return cache.delete(name) if cache.key? name