Skip to content

Commit 02c39ae

Browse files
committed
introduce ITransactionCallback
1 parent 4bd95f2 commit 02c39ae

File tree

3 files changed

+420
-6
lines changed

3 files changed

+420
-6
lines changed

src/Smdn.Net.MuninNode/Smdn.Net.MuninNode.Protocol/MuninProtocolHandler.cs

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,28 @@ public ValueTask HandleTransactionStartAsync(
147147
}
148148

149149
/// <remarks>
150-
/// In the default implementation, a banner response is sent back to the client.
150+
/// In the default implementation, <see cref="ITransactionCallback.StartTransactionAsync"/> is
151+
/// called for <see cref="IPluginProvider"/> and <see cref="IPlugin"/>s,
152+
/// and then a banner response is sent back to the client.
151153
/// </remarks>
152-
protected virtual ValueTask HandleTransactionStartAsyncCore(
154+
protected virtual async ValueTask HandleTransactionStartAsyncCore(
153155
IMuninNodeClient client,
154156
CancellationToken cancellationToken
155157
)
156-
=> SendResponseAsync(
158+
{
159+
if (profile.PluginProvider is ITransactionCallback providerTransactionCallback)
160+
await providerTransactionCallback.StartTransactionAsync(cancellationToken).ConfigureAwait(false);
161+
162+
foreach (var pluginTransactionCallback in plugins.Values.OfType<ITransactionCallback>()) {
163+
await pluginTransactionCallback.StartTransactionAsync(cancellationToken).ConfigureAwait(false);
164+
}
165+
166+
await SendResponseAsync(
157167
client,
158168
banner,
159169
cancellationToken
160-
);
170+
).ConfigureAwait(false);
171+
}
161172

162173
/// <inheritdoc cref="IMuninProtocolHandler.HandleTransactionEndAsync"/>
163174
public ValueTask HandleTransactionEndAsync(
@@ -178,11 +189,22 @@ public ValueTask HandleTransactionEndAsync(
178189
return HandleTransactionEndAsyncCore(client, cancellationToken);
179190
}
180191

181-
protected virtual ValueTask HandleTransactionEndAsyncCore(
192+
/// <remarks>
193+
/// In the default implementation, <see cref="ITransactionCallback.EndTransactionAsync"/> is
194+
/// called for <see cref="IPluginProvider"/> and <see cref="IPlugin"/>s.
195+
/// </remarks>
196+
protected virtual async ValueTask HandleTransactionEndAsyncCore(
182197
IMuninNodeClient client,
183198
CancellationToken cancellationToken
184199
)
185-
=> default; // do nothing in this class
200+
{
201+
foreach (var pluginTransactionCallback in plugins.Values.OfType<ITransactionCallback>()) {
202+
await pluginTransactionCallback.EndTransactionAsync(cancellationToken).ConfigureAwait(false);
203+
}
204+
205+
if (profile.PluginProvider is ITransactionCallback providerTransactionCallback)
206+
await providerTransactionCallback.EndTransactionAsync(cancellationToken).ConfigureAwait(false);
207+
}
186208

187209
private static bool ExpectCommand(
188210
ReadOnlySequence<byte> commandLine,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// SPDX-FileCopyrightText: 2023 smdn <smdn@smdn.jp>
2+
// SPDX-License-Identifier: MIT
3+
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
using Smdn.Net.MuninNode.Protocol;
8+
9+
namespace Smdn.Net.MuninPlugin;
10+
11+
/// <summary>
12+
/// Defines the callbacks when a request session from the <c>munin-update</c> starts or ends.
13+
/// </summary>
14+
public interface ITransactionCallback {
15+
/// <summary>
16+
/// Implements a callback to be invoked when <c>munin-update</c> starts a transaction.
17+
/// </summary>
18+
/// <remarks>
19+
/// This method is invoked when the <see cref="IMuninProtocolHandler"/> is requested to start
20+
/// processing a transaction.
21+
/// </remarks>
22+
/// <param name="cancellationToken">
23+
/// The <see cref="CancellationToken" /> to monitor for cancellation requests.
24+
/// </param>
25+
/// <seealso cref="IMuninProtocolHandler.HandleTransactionStartAsync"/>
26+
ValueTask StartTransactionAsync(CancellationToken cancellationToken);
27+
28+
/// <summary>
29+
/// Implements a callback to be invoked when <c>munin-update</c> ends a transaction.
30+
/// </summary>
31+
/// <remarks>
32+
/// This method is invoked when the <see cref="IMuninProtocolHandler"/> is requested to end
33+
/// processing a transaction.
34+
/// </remarks>
35+
/// <param name="cancellationToken">
36+
/// The <see cref="CancellationToken" /> to monitor for cancellation requests.
37+
/// </param>
38+
/// <seealso cref="Smdn.Net.MuninNode.Protocol.IMuninProtocolHandler.HandleTransactionEndAsync"/>
39+
ValueTask EndTransactionAsync(CancellationToken cancellationToken);
40+
}

0 commit comments

Comments
 (0)