Skip to content

Commit d08a28b

Browse files
committed
use threading timer + add semaphore + update http timeouts
1 parent babd33e commit d08a28b

File tree

4 files changed

+41
-26
lines changed

4 files changed

+41
-26
lines changed

src/automatica.drivers/automatica.driver.blockchain.ticker/P3.Driver.Blockchain.Ticker.Driver/Bitcoin/BitcoinNode.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ namespace P3.Driver.Blockchain.Ticker.Driver.Bitcoin
1515
internal class BitcoinNode : CoinNode
1616
{
1717
private readonly List<BitcoinValueNode> _nodes = new List<BitcoinValueNode>();
18-
private readonly HttpClient _client = new HttpClient();
18+
private readonly HttpClient _client;
1919

2020
public BitcoinNode(IDriverContext driverContext) : base(driverContext)
2121
{
22+
_client = new HttpClient();
23+
_client.Timeout = TimeSpan.FromSeconds(5);
2224
}
2325

2426
protected override async Task<bool> Read(IReadContext readContext, CancellationToken token = new CancellationToken())

src/automatica.drivers/automatica.driver.blockchain.ticker/P3.Driver.Blockchain.Ticker.Driver/BlockchainDriver.cs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Threading;
34
using System.Threading.Tasks;
4-
using System.Timers;
55
using Automatica.Core.Driver;
66
using Microsoft.Extensions.Logging;
77
using P3.Driver.Blockchain.Ticker.Driver.Bitcoin;
88
using P3.Driver.Blockchain.Ticker.Driver.Cardano;
99
using P3.Driver.Blockchain.Ticker.Driver.Ethereum;
10-
using Timer = System.Timers.Timer;
10+
using Timer = System.Threading.Timer;
1111

1212
namespace P3.Driver.Blockchain.Ticker.Driver
1313
{
1414
internal class BlockchainDriver : DriverNoneAttributeBase
1515
{
16-
private Timer _timer = new Timer();
16+
private Timer _timer;
1717

18-
private List<CoinNode> _nodes = new List<CoinNode>();
19-
20-
private ILogger _logger;
18+
private readonly List<CoinNode> _nodes = new List<CoinNode>();
19+
private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(1, 1);
20+
private readonly ILogger _logger;
21+
private int _pollTime;
2122

2223
public BlockchainDriver(IDriverContext driverContext) : base(driverContext)
2324
{
@@ -27,31 +28,40 @@ public BlockchainDriver(IDriverContext driverContext) : base(driverContext)
2728
public override Task<bool> Init(CancellationToken token = default)
2829
{
2930
var pollTime = GetPropertyValueInt("poll");
30-
31-
_timer.Elapsed += _timer_Elapsed;
32-
_timer.Interval = pollTime * 1000;
33-
34-
_logger.LogInformation($"Start polling every {pollTime}s");
35-
36-
31+
_pollTime = pollTime;
3732
return base.Init(token);
3833
}
3934

35+
private async void TimeElapsed(object state)
36+
{
37+
try
38+
{
39+
if (await _semaphore.WaitAsync(TimeSpan.FromSeconds(1)))
40+
{
41+
await ReadValues();
42+
}
43+
}
44+
catch (Exception ex)
45+
{
46+
DriverContext.Logger.LogError(ex, "Error read values...");
47+
}
48+
finally
49+
{
50+
_semaphore.Release();
51+
}
52+
}
53+
4054
public override async Task<bool> Start(CancellationToken token = default)
4155
{
42-
_timer.Start();
56+
_timer = new Timer(TimeElapsed, this, _pollTime * 1000, _pollTime * 1000);
4357

58+
_logger.LogInformation($"Start polling every {_pollTime}s");
4459
await ReadValues(token);
4560

4661
return await base.Start(token);
4762
}
4863

49-
private async void _timer_Elapsed(object sender, ElapsedEventArgs e)
50-
{
51-
await ReadValues();
52-
53-
}
54-
64+
5565
protected override async Task<bool> Read(IReadContext readContext, CancellationToken token = new CancellationToken())
5666
{
5767
await ReadValues(token);
@@ -70,8 +80,7 @@ private async Task ReadValues(CancellationToken token = default)
7080

7181
public override Task<bool> Stop(CancellationToken token = default)
7282
{
73-
_timer.Stop();
74-
_timer.Elapsed -= _timer_Elapsed;
83+
_timer.Dispose();
7584
return base.Stop(token);
7685
}
7786

src/automatica.drivers/automatica.driver.blockchain.ticker/P3.Driver.Blockchain.Ticker.Driver/Cardano/CardanoNode.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ namespace P3.Driver.Blockchain.Ticker.Driver.Cardano
1616
internal class CardanoNode : CoinNode
1717
{
1818
private readonly List<CardanoValueNode> _nodes = new();
19-
private readonly HttpClient _client = new ();
19+
private readonly HttpClient _client;
2020

2121
public CardanoNode(IDriverContext driverContext) : base(driverContext)
2222
{
23+
_client = new HttpClient();
24+
_client.Timeout = TimeSpan.FromSeconds(5);
2325
}
2426

2527
protected override async Task<bool> Read(IReadContext readContext, CancellationToken token = new CancellationToken())

src/automatica.drivers/automatica.driver.blockchain.ticker/P3.Driver.Blockchain.Ticker.Driver/Ethereum/EthereumNode.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,12 @@ internal class TickerPriceValue
2424
internal class EthereumNode : CoinNode
2525
{
2626
private readonly List<EthereumValueNode> _nodes = new();
27-
private readonly HttpClient _client = new ();
27+
private readonly HttpClient _client;
2828

2929
public EthereumNode(IDriverContext driverContext) : base(driverContext)
3030
{
31+
_client = new HttpClient();
32+
_client.Timeout = TimeSpan.FromSeconds(5);
3133
}
3234

3335
protected override async Task<bool> Read(IReadContext readContext, CancellationToken token = new CancellationToken())

0 commit comments

Comments
 (0)