in aliyun-net-credentials/Provider/RefreshCachedSupplier.cs [144:205]
private RefreshResult<T> HandleFetchedSuccess(RefreshResult<T> value)
{
Logger.Debug(string.Format("Refresh credentials successfully, retrieved value is {0}, cached value is {1}",
value, this.cachedValue));
Interlocked.Exchange(ref consecutiveRefreshFailures, 0);
var now = DateTime.UtcNow.GetTimeMillis();
// 过期时间大于15分钟,不用管
if (now < value.StaleTime)
{
Logger.Debug(string.Format("Retrieved value stale time is {0}. Using staleTime of {1}",
ParameterHelper.FormatIso8601Date(value.StaleTime),
ParameterHelper.FormatIso8601Date(value.StaleTime)));
return value;
}
// 不足或等于15分钟,但未过期,下次会再次刷新
if (now < value.StaleTime + StaleTime)
{
Logger.Warn(string.Format("Retrieved value stale time is %s in the past ({0}). Using staleTime of {1}",
ParameterHelper.FormatIso8601Date(value.StaleTime),
ParameterHelper.FormatIso8601Date(now)));
return value.ToBuilder().StaleTime(now).Build();
}
Logger.Warn(string.Format(
"Retrieved value expiration time of the credential is in the past ({0}). Trying use the cached value.",
ParameterHelper.FormatIso8601Date(value.StaleTime + StaleTime)));
// 已过期,看缓存,缓存若大于15分钟,返回缓存,若小于15分钟,则根据策略判断是立刻重试还是稍后重试
if (this.cachedValue == null)
{
throw new CredentialException("No cached value was found.");
}
if (now < this.cachedValue.StaleTime)
{
Logger.Warn(string.Format("Cached value staleTime is {0}. Using staleTime of {1}",
ParameterHelper.FormatIso8601Date(this.cachedValue.StaleTime),
ParameterHelper.FormatIso8601Date(this.cachedValue.StaleTime)));
return this.cachedValue;
}
switch (this.staleValueBehavior)
{
case StaleValueBehavior.Strict:
// 立马重试
Logger.Warn(string.Format(
"Cached value expiration is in the past ({0}). Using expiration of {1}",
value.StaleTime, now + 1000));
return this.cachedValue.ToBuilder().StaleTime(now + 1000).Build();
case StaleValueBehavior.Allow:
//一分钟左右重试一次
var waitUntilNextRefresh = 50 * 1000 + jitter.Next(20 * 1000 + 1);
var nextRefreshTime = now + waitUntilNextRefresh;
Logger.Warn(string.Format(
"Cached value expiration has been extended to {0} because the downstream service returned a time in the past: {1}",
nextRefreshTime, value.StaleTime));
return this.cachedValue.ToBuilder().StaleTime(nextRefreshTime).Build();
default:
throw new ArgumentException(string.Format("Unknown stale-value-behavior: {0}",
this.staleValueBehavior));
}
}