Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"core": {
"changeLogMessages": [
"Removed a lock from the cached logger lookup and avoided building debug log arguments during request path canonicalization unless a debug logger is attached."
],
"type": "patch",
"updateMinimum": false
}
}
32 changes: 22 additions & 10 deletions sdk/src/Core/Amazon.Runtime/Internal/Util/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,11 @@ public static Logger GetLogger(Type type)
{
if (type == null) throw new ArgumentNullException("type");

Logger l;
lock (_cachedLoggers)
{
if (!_cachedLoggers.TryGetValue(type, out l))
{
l = new Logger(type);
_cachedLoggers[type] = l;
}
}
return l;
// ConcurrentDictionary already synchronizes; a lock here would serialize every caller on the hot path.
// GetOrAdd may run the factory more than once for the same type if two threads race on first use; the
// extra Logger is discarded and building one has no side effects, so that is cheaper than a Lazy<T>
// indirection on every lookup.
return _cachedLoggers.GetOrAdd(type, static t => new Logger(t));
}

public static void ClearLoggerCache()
Expand Down Expand Up @@ -121,6 +116,23 @@ public void Debug(Exception exception, string messageFormat, params object[] arg
}
}

/// <summary>
/// Returns true if at least one registered adaptor logger is enabled for <paramref name="level"/>.
/// Callers can use this to skip building expensive log arguments.
/// </summary>
internal bool IsEnabled(SdkLogLevel level)
{
foreach (var logger in _loggers)
{
if (logger.IsEnabled(level))
{
return true;
}
}

return false;
}

public void DebugFormat(string messageFormat, params object[] args)
{
foreach (var logger in _loggers)
Expand Down
16 changes: 10 additions & 6 deletions sdk/src/Core/Amazon.Util/AWSSDKUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -521,12 +521,16 @@ public static string CanonicalizeResourcePathV2(Uri endpoint, string resourcePat
}
string canonicalizedResourcePath = JoinResourcePathSegmentsV2(encodedSegments);

// Get the logger each time (it's cached) because we shouldn't store it in a static variable.
Logger.GetLogger(typeof(AWSSDKUtils)).DebugFormat("{0} encoded {1}{2} for canonicalization: {3}",
pathWasPreEncoded ? "Double" : "Single",
resourcePath,
endpoint == null ? "" : " with endpoint " + endpoint.AbsoluteUri,
canonicalizedResourcePath);
var logger = Logger.GetLogger(typeof(AWSSDKUtils));
if (logger.IsEnabled(Amazon.Runtime.Logging.SdkLogLevel.Debug))
{
logger.DebugFormat("{0} encoded {1}{2}{3} for canonicalization: {4}",
pathWasPreEncoded ? "Double" : "Single",
resourcePath,
endpoint == null ? "" : " with endpoint ",
endpoint == null ? "" : endpoint.AbsoluteUri,
canonicalizedResourcePath);
}

return canonicalizedResourcePath;
}
Expand Down
Loading