Skip to content

Commit

Permalink
Use a ConcurrentDictionary to remember which calls did work (#1147)
Browse files Browse the repository at this point in the history
  • Loading branch information
fubar-coder authored Jul 11, 2024
1 parent b84a28d commit 9d572ea
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static IFbClient Create(string dllName)
{
result = BuildFbClient(dllName);
cache.Add(dllName, result);
ShutdownHelper.RegisterFbClientShutdown(() => NativeHelpers.CallIfExists(() => result.fb_shutdown(0, 0)));
ShutdownHelper.RegisterFbClientShutdown(() => NativeHelpers.CallIfExists(nameof(IFbClient.fb_shutdown), () => result.fb_shutdown(0, 0)));
return result;
}
finally
Expand Down
24 changes: 14 additions & 10 deletions src/FirebirdSql.Data.FirebirdClient/Client/Native/FesStatement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,11 +371,13 @@ public override void Execute(int timeout, IDescriptorFiller descriptorFiller)
descriptorFiller.Fill(_parameters, 0);

ClearStatusVector();
NativeHelpers.CallIfExists(() =>
{
_database.FbClient.fb_dsql_set_timeout(_statusVector, ref _handle, (uint)timeout);
_database.ProcessStatusVector(_statusVector);
});
NativeHelpers.CallIfExists(
nameof(IFbClient.fb_dsql_set_timeout),
() =>
{
_database.FbClient.fb_dsql_set_timeout(_statusVector, ref _handle, (uint)timeout);
_database.ProcessStatusVector(_statusVector);
});

ClearStatusVector();

Expand Down Expand Up @@ -441,11 +443,13 @@ public override async ValueTask ExecuteAsync(int timeout, IDescriptorFiller desc
await descriptorFiller.FillAsync(_parameters, 0, cancellationToken).ConfigureAwait(false);

ClearStatusVector();
NativeHelpers.CallIfExists(() =>
{
_database.FbClient.fb_dsql_set_timeout(_statusVector, ref _handle, (uint)timeout);
_database.ProcessStatusVector(_statusVector);
});
NativeHelpers.CallIfExists(
nameof(IFbClient.fb_dsql_set_timeout),
() =>
{
_database.FbClient.fb_dsql_set_timeout(_statusVector, ref _handle, (uint)timeout);
_database.ProcessStatusVector(_statusVector);
});

ClearStatusVector();

Expand Down
21 changes: 17 additions & 4 deletions src/FirebirdSql.Data.FirebirdClient/Common/NativeHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,31 @@
//$Authors = Jiri Cincura ([email protected])

using System;
using System.Collections.Concurrent;

namespace FirebirdSql.Data.Common;

internal static class NativeHelpers
{
public static void CallIfExists(Action action)
private static readonly ConcurrentDictionary<string, bool> _cache = new ConcurrentDictionary<string, bool>(StringComparer.Ordinal);

public static void CallIfExists(string actionId, Action action)
{
try
if (!_cache.TryGetValue(actionId, out var executionAllowed))
{
try
{
action();
_cache.TryAdd(actionId, true);
}
catch (EntryPointNotFoundException)
{
_cache.TryAdd(actionId, false);
}
}
else if (executionAllowed)
{
action();
}
catch (EntryPointNotFoundException)
{ }
}
}

0 comments on commit 9d572ea

Please sign in to comment.