Skip to content

Commit

Permalink
prevent memory leak
Browse files Browse the repository at this point in the history
See #21
  • Loading branch information
TechnoBerry committed Dec 3, 2020
1 parent 0ec845b commit f3bde9e
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 28 deletions.
5 changes: 0 additions & 5 deletions src/Camunda.Worker/Client/ExternalTaskClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ public ExternalTaskClient(HttpClient httpClient)
_httpClient = Guard.NotNull(httpClient, nameof(httpClient));
}

public void Dispose()
{
_httpClient?.Dispose();
}

public async Task<List<ExternalTask>> FetchAndLockAsync(
FetchAndLockRequest request,
CancellationToken cancellationToken = default
Expand Down
3 changes: 1 addition & 2 deletions src/Camunda.Worker/Client/IExternalTaskClient.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace Camunda.Worker.Client
{
public interface IExternalTaskClient : IDisposable
public interface IExternalTaskClient
{
Task<List<ExternalTask>> FetchAndLockAsync(
FetchAndLockRequest request,
Expand Down
8 changes: 4 additions & 4 deletions src/Camunda.Worker/Execution/ExternalTaskContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public async Task ExtendLockAsync(int newDuration)
ThrowIfDisposed();
ThrowIfCompleted();

using var client = ServiceProvider.GetRequiredService<IExternalTaskClient>();
var client = ServiceProvider.GetRequiredService<IExternalTaskClient>();
var request = new ExtendLockRequest(Task.WorkerId, newDuration);
await client.ExtendLockAsync(Task.Id, request);
}
Expand All @@ -39,7 +39,7 @@ public async Task CompleteAsync(IDictionary<string, Variable>? variables = null,
ThrowIfDisposed();
ThrowIfCompleted();

using var client = ServiceProvider.GetRequiredService<IExternalTaskClient>();
var client = ServiceProvider.GetRequiredService<IExternalTaskClient>();
var request = new CompleteRequest(Task.WorkerId)
{
Variables = variables,
Expand All @@ -57,7 +57,7 @@ public async Task ReportFailureAsync(string? errorMessage, string? errorDetails,
ThrowIfDisposed();
ThrowIfCompleted();

using var client = ServiceProvider.GetRequiredService<IExternalTaskClient>();
var client = ServiceProvider.GetRequiredService<IExternalTaskClient>();
var request = new ReportFailureRequest(Task.WorkerId)
{
ErrorMessage = errorMessage,
Expand All @@ -76,7 +76,7 @@ public async Task ReportBpmnErrorAsync(string errorCode, string errorMessage,
ThrowIfDisposed();
ThrowIfCompleted();

using var client = ServiceProvider.GetRequiredService<IExternalTaskClient>();
var client = ServiceProvider.GetRequiredService<IExternalTaskClient>();
var request = new BpmnErrorRequest(Task.WorkerId, errorCode, errorMessage)
{
Variables = variables
Expand Down
18 changes: 5 additions & 13 deletions src/Camunda.Worker/Execution/ExternalTaskSelector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Threading;
using System.Threading.Tasks;
using Camunda.Worker.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
Expand All @@ -13,17 +12,17 @@ namespace Camunda.Worker.Execution
{
public sealed class ExternalTaskSelector : IExternalTaskSelector
{
private readonly IServiceProvider _provider;
private readonly IExternalTaskClient _client;
private readonly CamundaWorkerOptions _options;
private readonly ILogger<ExternalTaskSelector> _logger;

public ExternalTaskSelector(
IServiceProvider provider,
IExternalTaskClient client,
IOptions<CamundaWorkerOptions> options,
ILogger<ExternalTaskSelector>? logger = null
)
{
_provider = Guard.NotNull(provider, nameof(provider));
_client = Guard.NotNull(client, nameof(client));
_options = Guard.NotNull(options, nameof(options)).Value;
_logger = logger ?? NullLogger<ExternalTaskSelector>.Instance;
}
Expand All @@ -33,13 +32,11 @@ public async Task<IEnumerable<ExternalTask>> SelectAsync(
CancellationToken cancellationToken = default
)
{
var client = _provider.GetRequiredService<IExternalTaskClient>();

try
{
_logger.LogDebug("Waiting for external task");
var fetchAndLockRequest = MakeRequestBody(topics);
var externalTasks = await PerformSelection(client, fetchAndLockRequest, cancellationToken);
var externalTasks = await PerformSelection(fetchAndLockRequest, cancellationToken);
_logger.LogDebug("Locked {Count} external tasks", externalTasks.Count);
return externalTasks;
}
Expand All @@ -49,10 +46,6 @@ public async Task<IEnumerable<ExternalTask>> SelectAsync(
await DelayOnFailure(cancellationToken);
return Enumerable.Empty<ExternalTask>();
}
finally
{
client?.Dispose();
}
}

private FetchAndLockRequest MakeRequestBody(IEnumerable<FetchAndLockRequest.Topic> topics)
Expand All @@ -68,12 +61,11 @@ private FetchAndLockRequest MakeRequestBody(IEnumerable<FetchAndLockRequest.Topi
}

private async Task<List<ExternalTask>> PerformSelection(
IExternalTaskClient client,
FetchAndLockRequest request,
CancellationToken cancellationToken
)
{
var externalTasks = await client.FetchAndLockAsync(request, cancellationToken);
var externalTasks = await _client.FetchAndLockAsync(request, cancellationToken);
return externalTasks;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public ExternalTaskCamundaTest()
public void Dispose()
{
_handlerMock?.Dispose();
_client?.Dispose();
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading;
using System.Threading.Tasks;
using Camunda.Worker.Client;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using Moq;
using Xunit;
Expand All @@ -25,9 +24,8 @@ public class ExternalTaskSelectorTest

public ExternalTaskSelectorTest()
{
var provider = new ServiceCollection().AddSingleton(_clientMock.Object).BuildServiceProvider();
_selector = new ExternalTaskSelector(
provider,
_clientMock.Object,
_options
);
}
Expand Down

0 comments on commit f3bde9e

Please sign in to comment.