任务是通过服务到服务认证机制查询谷歌服务之一,该机制假设每60分钟获得一次新的访问令牌。因此,它需要主动刷新或刷新失败的请求。
最新的技术似乎是一种防御性的技术,不符合长生不老的哲学。此外,无法确定请求是否由于访问令牌过期而失败,或者因为提供的凭据(电子邮件和密钥)通常无效--这将是相同的文本和相同的401代码。
也许有人能就执行策略提出建议?这将是主机应用程序打算使用的库,并且假定在令牌刷新期间(可能是90‘s?)新请求将被创建,一般情况下,它们等待新令牌而不是使用即将过时的令牌会更好。
发布于 2015-09-27 07:53:33
一般来说,在Elixir/Erlang中,大约有4种存储和使用共享数据的方法:
对于您的使用,第一个和第二个解决方案可能几乎是等价的。但我会使用第二个,因为平行化。你可以写这样的东西:
defmodule TokenHolder do
def start_link(user,passwd) do
Agent.start_link(fn ->
tok_time = get_token user, passwd
{user,passwd,tok_time}
end, name: __MODULE__)
end
# refresh the token if older that one hour
@max_age 60*60*1000000
def token do
Agent.get_and_update(__MODULE__, fn state={user,passwd,{token,retrieved}} ->
now = :os.timestamp
if(:timer.now_diff(now, retrieved) < @max_age) do
# return old token and old state
{token,state}
else
# retrieve new token, return it and return changed state
tok_time = {token,_} = get_token user, passwd
{token,{user,passwd,tok_time}}
end
end)
end
defp get_token(user,passwd) do
token = ... # retrieve token somehow...
{token,:os.timestamp}
end
end那你就这么做:
{:ok,_} = TokenHolder.start_link("user","secret")
token = TokenHolder.tokenhttps://stackoverflow.com/questions/32803723
复制相似问题