Skip to content

Commit

Permalink
Add error handling for strategy for object reinitialisation.
Browse files Browse the repository at this point in the history
Switch to modern code analysis.
  • Loading branch information
Yortw committed Jan 16, 2021
1 parent 7edda44 commit acc4f5e
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 14 deletions.
1 change: 1 addition & 0 deletions src/PoolSharp.Android/PoolSharp.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<GenerateSerializationAssemblies>Off</GenerateSerializationAssemblies>
<AndroidUseLatestPlatformSdk>False</AndroidUseLatestPlatformSdk>
<TargetFrameworkVersion>v9.0</TargetFrameworkVersion>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down
Binary file added src/PoolSharp.Bait/GlobalSuppressions.cs
Binary file not shown.
6 changes: 4 additions & 2 deletions src/PoolSharp.Bait/PoolSharp.Bait.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<TargetFrameworkProfile>Profile328</TargetFrameworkProfile>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -24,7 +25,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>true</RunCodeAnalysis>
<RunCodeAnalysis>false</RunCodeAnalysis>
<CodeAnalysisRuleSet>..\PoolSharpAnalysis.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Debug\PoolSharp.XML</DocumentationFile>
</PropertyGroup>
Expand All @@ -35,12 +36,13 @@
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>true</RunCodeAnalysis>
<RunCodeAnalysis>false</RunCodeAnalysis>
<CodeAnalysisRuleSet>..\PoolSharpAnalysis.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Release\PoolSharp.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Compile Include="ExceptionHelper.cs" />
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Pool.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UnsynchronizedPool.cs" />
Expand Down
Binary file modified src/PoolSharp.Net40/GlobalSuppressions.cs
Binary file not shown.
5 changes: 3 additions & 2 deletions src/PoolSharp.Net40/PoolSharp.Net40.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<AssemblyName>PoolSharp</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -20,7 +21,7 @@
<DefineConstants>TRACE;DEBUG;CODE_ANALYSIS;SUPPORTS_THREADS</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>true</RunCodeAnalysis>
<RunCodeAnalysis>false</RunCodeAnalysis>
<CodeAnalysisRuleSet>..\PoolSharpAnalysis.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Debug\PoolSharp.XML</DocumentationFile>
</PropertyGroup>
Expand All @@ -31,7 +32,7 @@
<DefineConstants>TRACE;CODE_ANALYSIS;SUPPORTS_THREADS</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<RunCodeAnalysis>true</RunCodeAnalysis>
<RunCodeAnalysis>false</RunCodeAnalysis>
<CodeAnalysisRuleSet>..\PoolSharpAnalysis.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Release\PoolSharp.XML</DocumentationFile>
</PropertyGroup>
Expand Down
2 changes: 2 additions & 0 deletions src/PoolSharp.NetStandard20/PoolSharp.NetStandard20.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>PoolSharp</AssemblyName>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<RunAnalyzersDuringBuild>false</RunAnalyzersDuringBuild>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
Expand Down
38 changes: 38 additions & 0 deletions src/PoolSharp.NetStandard20/PoolSharp.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 24 additions & 5 deletions src/PoolSharp.Shared.ConcurrentBagImplementation/Pool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class Pool<T> : PoolBase<T>
private long _PoolInstancesCount;

#if SUPPORTS_THREADS
System.Threading.Thread _ReinitialiseThread;
System.Threading.Thread _ReinitialiseThread;
#endif

#endregion
Expand Down Expand Up @@ -225,10 +225,19 @@ private void BackgroundReinitialise()
SafeDispose(item);
else
{
if (PoolPolicy.ReinitializeObject != null)
PoolPolicy.ReinitializeObject(item);
try
{
if (PoolPolicy.ReinitializeObject != null)
PoolPolicy.ReinitializeObject(item);
}
catch (Exception ex)
{
OnReinitialiseError(new ReinitialiseErrorEventArgs<T>(ex, item));
SafeDispose(item);
item = default;
}

if (ShouldReturnToPool(item))
if (item != null && ShouldReturnToPool(item))
{
_Pool.Add(item);

Expand Down Expand Up @@ -274,7 +283,17 @@ private void ReinitialiseAndReturnToPoolOrDispose(T value)
{
if (ShouldReturnToPool(value))
{
PoolPolicy.ReinitializeObject(value);
try
{
PoolPolicy.ReinitializeObject(value);
}
catch (Exception ex)
{
OnReinitialiseError(new ReinitialiseErrorEventArgs<T>(ex, value));
SafeDispose(value);
return;
}

_Pool.Add(value);
Interlocked.Increment(ref _PoolInstancesCount);
}
Expand Down
23 changes: 22 additions & 1 deletion src/PoolSharp.Shared/PoolBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public abstract class PoolBase<T> : IPool<T>

#endregion

#region Events

/// <summary>
/// Raised if an error is thrown by the <see cref="PoolPolicy{T}.ReinitializeObject"/> callback.
/// </summary>
/// <remarks>
/// <para>This event is raised when an exception is thrown during reinitialisation of an object as this may occur on a background thread where the caller
/// otherwise couldn't catch and handle the exception. If this event is raised, the item was being reinitialised is not added to the pool.</para>
/// </remarks>
public event EventHandler<ReinitialiseErrorEventArgs<T>> ReinitialiseError;

#endregion

#region Constructors

/// <summary>
Expand Down Expand Up @@ -145,6 +158,15 @@ protected bool IsPooledTypeWrapped

#region Public Methods

/// <summary>
/// Raises the <see cref="ReinitialiseError"/> event.
/// </summary>
/// <param name="e">The event arguments to pass to handlers of the event.</param>
protected virtual void OnReinitialiseError(ReinitialiseErrorEventArgs<T> e)
{
ReinitialiseError?.Invoke(this, e);
}

#if NETFX_CORE
/// <summary>
/// Disposes <paramref name="pooledObject"/> if it is not null and supports <see cref="IDisposable"/>, otherwise does nothing. If <paramref name="pooledObject"/> is actually a <see cref="PooledObject{T}"/> instance, then disposes the <see cref="PooledObject{T}.Value"/> property instead.
Expand All @@ -168,7 +190,6 @@ protected void SafeDispose(object pooledObject)
/// Disposes <paramref name="pooledObject"/> if it is not null and supports <see cref="IDisposable"/>, otherwise does nothing. If <paramref name="pooledObject"/> is actually a <see cref="PooledObject{T}"/> instance, then disposes the <see cref="PooledObject{T}.Value"/> property instead.
/// </summary>
/// <param name="pooledObject"></param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1720:IdentifiersShouldNotContainTypeNames", MessageId = "object")]
protected void SafeDispose(object pooledObject)
{
if (IsPooledTypeWrapped)
Expand Down
1 change: 1 addition & 0 deletions src/PoolSharp.Shared/PoolSharp.Shared.projitems
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@
<Compile Include="$(MSBuildThisFileDirectory)PooledObject.cs" />
<Compile Include="$(MSBuildThisFileDirectory)PoolPolicy.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ReflectionUtils.cs" />
<Compile Include="$(MSBuildThisFileDirectory)ReinitialiseErrorEventArgs.cs" />
</ItemGroup>
</Project>
37 changes: 37 additions & 0 deletions src/PoolSharp.Shared/ReinitialiseErrorEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace PoolSharp
{
/// <summary>
/// Event arguments for the <see cref="PoolBase{T}.ReinitialiseError"/> event.
/// </summary>
/// <typeparam name="T">The type of value stored in the pool and being reinitialised.</typeparam>
public class ReinitialiseErrorEventArgs<T> : EventArgs
{
private readonly Exception _Exception;
private readonly T _Item;

/// <summary>
/// Full constructor.
/// </summary>
/// <param name="exception">The exception that was thrown from the reinitialisation callback.</param>
/// <param name="item">The item that was being reinitialised when the exception were thrown.</param>
public ReinitialiseErrorEventArgs(Exception exception, T item)
{
_Exception = exception;
_Item = item;
}

/// <summary>
/// The exception that was thrown from the reinitialisation callback.
/// </summary>
public Exception Exception => _Exception;

/// <summary>
/// The item that was being reinitialised when the exception were thrown.
/// </summary>
public T Item => _Item;
}
}
Binary file added src/PoolSharp.iOS/GlobalSuppressions.cs
Binary file not shown.
7 changes: 4 additions & 3 deletions src/PoolSharp.iOS/PoolSharp.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<RootNamespace>PoolSharp</RootNamespace>
<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
<AssemblyName>PoolSharp</AssemblyName>
<EnableNETAnalyzers>true</EnableNETAnalyzers>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand All @@ -21,7 +22,6 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<RunCodeAnalysis>true</RunCodeAnalysis>
<CodeAnalysisRuleSet>..\PoolSharpAnalysis.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Debug\PoolSharp.XML</DocumentationFile>
</PropertyGroup>
Expand All @@ -32,13 +32,13 @@
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<RunCodeAnalysis>true</RunCodeAnalysis>
<RunCodeAnalysis>false</RunCodeAnalysis>
<CodeAnalysisRuleSet>..\PoolSharpAnalysis.ruleset</CodeAnalysisRuleSet>
<DefineConstants>CODE_ANALYSIS;__UNIFIED__;__MOBILE__;__IOS__;CODE_ANALYSIS;__UNIFIED__;__MOBILE__;__IOS__;SUPPORTS_THREADS;SUPPORTS_AGGRESSIVEINLINING</DefineConstants>
<DocumentationFile>bin\Release\PoolSharp.XML</DocumentationFile>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'AppStore|AnyCPU'">
<RunCodeAnalysis>true</RunCodeAnalysis>
<RunCodeAnalysis>false</RunCodeAnalysis>
<CodeAnalysisRuleSet>..\..\PoolNetAnalysis.ruleset</CodeAnalysisRuleSet>
<DefineConstants>CODE_ANALYSIS;__UNIFIED__;__MOBILE__;__IOS__;SUPPORTS_THREADS</DefineConstants>
</PropertyGroup>
Expand All @@ -52,6 +52,7 @@
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="GlobalSuppressions.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="..\PoolSharp.Shared\PoolSharp.Shared.projitems" Label="Shared" />
Expand Down
2 changes: 1 addition & 1 deletion src/PoolSharp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<projectUrl>https://github.com/Yortw/PoolSharp</projectUrl>
<summary />
<tags>object pool sharp winrt uwp net40 ios android xamarin portable</tags>
<releaseNotes>Removed WinRT support. Added NetStandard 2.0 support.</releaseNotes>
<releaseNotes>Removed WinRT support. Added NetStandard 2.0 support. Improved error handling during item reinitialisation.</releaseNotes>
</metadata>
<files>
<file src="PoolSharp.Android\bin\Release\PoolSharp.dll" target="lib\MonoAndroid\PoolSharp.dll" />
Expand Down

0 comments on commit acc4f5e

Please sign in to comment.