-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
132 lines (112 loc) · 4.11 KB
/
Program.cs
File metadata and controls
132 lines (112 loc) · 4.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
using Dapper;
using Dapper.Contrib.Extensions;
using ResilienceDecorators.MySql;
using ResilienceDecorators.MySql.RetryHelpers;
using Serilog;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace ConsoleApp17
{
internal class Program
{
private static string ConnectionString = "Server=127.0.0.1;Port=3306;Database=Aman;Uid=root;Ssl Mode=Required;Pwd=pass123;Pooling=True;MinimumPoolSize=10;ConnectionLifetime=60";
private static async Task Main(string[] args)
{
Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
//DoWithoutResilience();
await DoWithResilience();
}
private static void DoWithoutResilience()
{
for (var i = 0; i < 10_000; i++)
{
try
{
using (var conn = new MySqlConnection(ConnectionString))
{
conn.Open();
conn.Insert(new Record { Id = i + 1, Name = $"Aman {i + 1}" });
Log.Logger.Information("Inserted 1 row successfully!");
}
}
catch (Exception ex)
{
Log.Logger.Error(ex, "Error");
}
Thread.Sleep(100);
}
}
private static async Task DoWithResilience()
{
var facade = new TableFacade(ConnectionString);
for (int x = 0; x < 1_000; x++)
{
var data = await facade.GetIt();
await Task.Delay(500);
}
}
private static void LogRetry(MySqlException mySqlException, TimeSpan time) =>
Log.Logger.Warning("Failed with {message}. Retrying in {@ts}...",
mySqlException.Message,
time);
}
[Table("MyTable")]
public class Record
{
[ExplicitKey]
public int Id { get; set; }
public string Name { get; set; }
}
public class TableFacade : RetryWrapper
{
private readonly string connectionString;
public TableFacade(string connectionString)
{
this.connectionString = connectionString;
Log.Logger = new LoggerConfiguration().WriteTo.Console().CreateLogger();
}
protected override string GetConnectionString() => connectionString;
public async Task DoIt()
{
for (int x = 0; x < 100; x++)
{
await ExecuteWithAsyncRetries(async () =>
{
using (var conn = new MySqlConnection(connectionString))
{
await conn.OpenAsync();
conn.Insert(new Record { Id = x + 1, Name = $"Aman {x + 1}" });
Log.Logger.Information("Inserted 1 row successfully!");
}
},
ResilienceSettings.DefaultFailoverResilienceSettings,
LogRetry);
await Task.Delay(500);
}
}
public async Task<IReadOnlyCollection<Record>> GetIt()
{
return await ExecuteWithAsyncRetries<IReadOnlyCollection<Record>>(async () =>
{
using (var connv = new MySqlConnection(connectionString))
{
await connv.OpenAsync();
var result = await connv.QueryAsync<Record>("SELECT * FROM Aman.MyTable");
Log.Logger.Information("Retrieved!");
return result.ToList();
}
},
ResilienceSettings.DefaultFailoverResilienceSettings,
LogRetry);
}
private static void LogRetry(MySqlException mySqlException, TimeSpan time) =>
Log.Logger.Warning("Failed with {message}. Retrying in {@ts}...",
mySqlException.Message,
time);
}
}