Skip to content

Commit b3b3eaa

Browse files
committed
Various improvements.
Rename type to 'Context'; this matches the existing UsePeriodicPasswordProvider. Add CancellationToken; the user might execute commands against the server that might need to be cancelled. Remove sync overload; it could be unnecessary. Move types to their own files. Allow multiple callbacks to be added. Signed-off-by: Bradley Grainger <[email protected]>
1 parent 3a5254b commit b3b3eaa

File tree

6 files changed

+60
-78
lines changed

6 files changed

+60
-78
lines changed

src/MySqlConnector/MySqlConnection.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,10 @@ internal async Task OpenAsync(IOBehavior? ioBehavior, CancellationToken cancella
584584
EnlistTransaction(System.Transactions.Transaction.Current);
585585

586586
if (ConnectionOpenedCallback is { } connectionOpenedCallback)
587-
await connectionOpenedCallback(new(this, m_session.Conditions)).ConfigureAwait(false);
587+
{
588+
cancellationToken.ThrowIfCancellationRequested();
589+
await connectionOpenedCallback(new(this, m_session.Conditions), cancellationToken).ConfigureAwait(false);
590+
}
588591
}
589592
catch (Exception ex) when (activity is { IsAllDataRequested: true })
590593
{

src/MySqlConnector/MySqlConnectionOpenedCallback.cs

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,7 @@ namespace MySqlConnector;
33
/// <summary>
44
/// A callback that is invoked when a new <see cref="MySqlConnection"/> is opened.
55
/// </summary>
6-
/// <param name="data">A <see cref="MySqlConnectionOpenedData"/> giving information about the connection being opened.</param>
6+
/// <param name="context">A <see cref="MySqlConnectionOpenedContext"/> giving information about the connection being opened.</param>
7+
/// <param name="cancellationToken">A <see cref="CancellationToken"/> that can be used to cancel the asynchronous operation.</param>
78
/// <returns>A <see cref="ValueTask"/> representing the result of the possibly-asynchronous operation.</returns>
8-
public delegate ValueTask MySqlConnectionOpenedCallback(MySqlConnectionOpenedData data);
9-
10-
public sealed class MySqlConnectionOpenedData
11-
{
12-
/// <summary>
13-
/// The <see cref="MySqlConnection"/> that was opened.
14-
/// </summary>
15-
public MySqlConnection Connection { get; }
16-
17-
/// <summary>
18-
/// Bitflags giving the conditions under which a connection was opened.
19-
/// </summary>
20-
public MySqlConnectionOpenedConditions Conditions { get; }
21-
22-
internal MySqlConnectionOpenedData(MySqlConnection connection, MySqlConnectionOpenedConditions conditions)
23-
{
24-
Connection = connection;
25-
Conditions = conditions;
26-
}
27-
}
28-
29-
/// <summary>
30-
/// Bitflags giving the conditions under which a connection was opened.
31-
/// </summary>
32-
[Flags]
33-
public enum MySqlConnectionOpenedConditions
34-
{
35-
/// <summary>
36-
/// No specific conditions apply. This value may be used when an existing pooled connection is reused without being reset.
37-
/// </summary>
38-
None = 0,
39-
40-
/// <summary>
41-
/// A new physical connection to a MySQL Server was opened. This value is mutually exclusive with <see cref="Reset"/>.
42-
/// </summary>
43-
New = 1,
44-
45-
/// <summary>
46-
/// An existing pooled connection to a MySQL Server was reset. This value is mutually exclusive with <see cref="New"/>.
47-
/// </summary>
48-
Reset = 2,
49-
}
9+
public delegate ValueTask MySqlConnectionOpenedCallback(MySqlConnectionOpenedContext context, CancellationToken cancellationToken);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace MySqlConnector;
2+
3+
/// <summary>
4+
/// Bitflags giving the conditions under which a connection was opened.
5+
/// </summary>
6+
[Flags]
7+
public enum MySqlConnectionOpenedConditions
8+
{
9+
/// <summary>
10+
/// No specific conditions apply. This value may be used when an existing pooled connection is reused without being reset.
11+
/// </summary>
12+
None = 0,
13+
14+
/// <summary>
15+
/// A new physical connection to a MySQL Server was opened. This value is mutually exclusive with <see cref="Reset"/>.
16+
/// </summary>
17+
New = 1,
18+
19+
/// <summary>
20+
/// An existing pooled connection to a MySQL Server was reset. This value is mutually exclusive with <see cref="New"/>.
21+
/// </summary>
22+
Reset = 2,
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace MySqlConnector;
2+
3+
/// <summary>
4+
/// Contains information passed to <see cref="MySqlConnectionOpenedCallback"/> when a new <see cref="MySqlConnection"/> is opened.
5+
/// </summary>
6+
public sealed class MySqlConnectionOpenedContext
7+
{
8+
/// <summary>
9+
/// The <see cref="MySqlConnection"/> that was opened.
10+
/// </summary>
11+
public MySqlConnection Connection { get; }
12+
13+
/// <summary>
14+
/// Bitflags giving the conditions under which a connection was opened.
15+
/// </summary>
16+
public MySqlConnectionOpenedConditions Conditions { get; }
17+
18+
internal MySqlConnectionOpenedContext(MySqlConnection connection, MySqlConnectionOpenedConditions conditions)
19+
{
20+
Connection = connection;
21+
Conditions = conditions;
22+
}
23+
}

src/MySqlConnector/MySqlDataSourceBuilder.cs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,14 @@ public MySqlDataSourceBuilder UseRemoteCertificateValidationCallback(RemoteCerti
8989
return this;
9090
}
9191

92+
/// <summary>
93+
/// Adds a callback that is invoked when a new <see cref="MySqlConnection"/> is opened.
94+
/// </summary>
95+
/// <param name="callback">The callback to invoke.</param>
96+
/// <returns>This builder, so that method calls can be chained.</returns>
9297
public MySqlDataSourceBuilder UseConnectionOpenedCallback(MySqlConnectionOpenedCallback callback)
9398
{
94-
m_connectionOpenedCallback = callback;
95-
return this;
96-
}
97-
98-
public MySqlDataSourceBuilder UseConnectionOpenedCallback(Action<MySqlConnectionOpenedData> callback)
99-
{
100-
m_connectionOpenedCallback = data =>
101-
{
102-
callback(data);
103-
return default;
104-
};
99+
m_connectionOpenedCallback += callback;
105100
return this;
106101
}
107102

tests/MySqlConnector.Tests/ConnectionOpenedCallbackTests.cs

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -44,28 +44,6 @@ public void CallbackIsInvoked()
4444
}
4545
}
4646

47-
[Fact]
48-
public void SyncCallbackIsInvoked()
49-
{
50-
var dataSource = new MySqlDataSourceBuilder(m_csb.ConnectionString)
51-
.UseConnectionOpenedCallback(data =>
52-
{
53-
m_connectionOpenedCount++;
54-
m_connectionOpenedConditions = data.Conditions;
55-
})
56-
.Build();
57-
using (var connection = dataSource.CreateConnection())
58-
{
59-
Assert.Equal(0, m_connectionOpenedCount);
60-
Assert.Equal(MySqlConnectionOpenedConditions.None, m_connectionOpenedConditions);
61-
62-
connection.Open();
63-
64-
Assert.Equal(1, m_connectionOpenedCount);
65-
Assert.Equal(MySqlConnectionOpenedConditions.New, m_connectionOpenedConditions);
66-
}
67-
}
68-
6947
[Fact]
7048
public void CallbackIsInvokedForPooledConnection()
7149
{
@@ -146,7 +124,7 @@ public void ConditionsForNonResetConnection()
146124
}
147125
}
148126

149-
private ValueTask OnConnectionOpenedAsync(MySqlConnectionOpenedData data)
127+
private ValueTask OnConnectionOpenedAsync(MySqlConnectionOpenedContext data, CancellationToken cancellationToken)
150128
{
151129
m_connectionOpenedCount++;
152130
m_connectionOpenedConditions = data.Conditions;

0 commit comments

Comments
 (0)