Skip to content
This repository was archived by the owner on Sep 10, 2021. It is now read-only.

Commit 6db163c

Browse files
committed
Added Node.DetachAsync
- Added ability to detach as a future - Minor internal fixes - Moved to 0.1.9
1 parent 8d35cda commit 6db163c

File tree

8 files changed

+82
-18
lines changed

8 files changed

+82
-18
lines changed

samples/Example.General/Program.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,16 @@ static async Task AsyncMain(string[] args) {
7777
});
7878

7979
// attach
80-
//Service service = await TestNode.AttachAsync("auth:login", RpcBehaviour.Bind<ITest001>(new Test001(Guid.NewGuid())));
81-
82-
//service.Dispose();
80+
Service service = null;
81+
82+
try {
83+
service = await TestNode.AttachAsync("auth:login", ServiceType.Balanced, RpcBehaviour.Bind<ITest001>(new Test001(Guid.NewGuid())));
84+
} catch(Exception) {
85+
Console.WriteLine();
86+
}
87+
88+
// detaches the service
89+
await TestNode.DetachAsync(service).ConfigureAwait(false);
8390

8491
await Task.Delay(50000);
8592
}

src/Holon/Holon.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard1.6</TargetFramework>
5-
<Version>0.1.8</Version>
5+
<Version>0.1.9</Version>
66
<Authors>Alan Doherty</Authors>
77
<Company>Alan Doherty</Company>
88
<Description>A minimal service and event bus with additional support for RPC</Description>
99
<Copyright>BattleCrate Ltd 2018</Copyright>
1010
<PackageProjectUrl>https://github.com/alandoherty/holon-net</PackageProjectUrl>
1111
<RepositoryUrl>https://github.com/alandoherty/holon-net</RepositoryUrl>
1212
<RepositoryType>git</RepositoryType>
13-
<AssemblyVersion>0.1.8.0</AssemblyVersion>
13+
<AssemblyVersion>0.1.9.0</AssemblyVersion>
1414
<PackageLicenseUrl>https://github.com/alandoherty/holon-net/blob/master/LICENSE</PackageLicenseUrl>
1515
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
1616
<PackageIconUrl>https://s3-eu-west-1.amazonaws.com/assets.alandoherty.co.uk/github/holon-net-nuget.png</PackageIconUrl>
17-
<FileVersion>0.1.8.0</FileVersion>
17+
<FileVersion>0.1.9.0</FileVersion>
1818
</PropertyGroup>
1919

2020
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">

src/Holon/Node.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,24 @@ public void Detach(Service service) {
565565
service.Dispose();
566566
}
567567

568+
/// <summary>
569+
/// Detaches a service from the node, waiting for complete cancellation.
570+
/// </summary>
571+
/// <param name="service">The service.</param>
572+
/// <returns></returns>
573+
public async Task DetachAsync(Service service) {
574+
// remove service
575+
lock (_services) {
576+
if (!_services.Contains(service))
577+
return;
578+
579+
// remove from list
580+
_services.Remove(service);
581+
}
582+
583+
await service.ShutdownAsync().ConfigureAwait(false);
584+
}
585+
568586
/// <summary>
569587
/// Disposes the node and underlying services.
570588
/// </summary>

src/Holon/Protocol/Broker.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public async Task<BrokerQueue> CreateQueueAsync(string name = "", bool durable =
174174
}).ConfigureAwait(false);
175175

176176
// create consumer
177-
ObservableConsumer consumer = new ObservableConsumer(_channel);
177+
ObservableConsumer consumer = new ObservableConsumer(this);
178178

179179
// consume queue
180180
string consumerTag = null;
@@ -234,11 +234,11 @@ internal Broker(BrokerContext ctx, IModel channel, string appId) {
234234
OnReturned(new BrokerReturnedEventArgs(e.BasicProperties, e.Body, e.ReplyText));
235235
};
236236
_channel.ModelShutdown += delegate (object s, ShutdownEventArgs e) {
237-
// dispose
238-
Dispose();
239-
240237
// call event
241238
OnShutdown(new BrokerShutdownEventArgs(e.ReplyText));
239+
240+
// dispose
241+
Dispose();
242242
};
243243
}
244244
#endregion

src/Holon/Protocol/BrokerContext.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,14 @@ internal sealed class BrokerContext : IDisposable
2525
#endregion
2626

2727
#region Properties
28-
28+
/// <summary>
29+
/// Gets the connection.
30+
/// </summary>
31+
public IConnection Connection {
32+
get {
33+
return _connection;
34+
}
35+
}
2936
#endregion
3037

3138
#region Methods

src/Holon/Protocol/BrokerQueue.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,18 @@ public void Unbind(string exchange, string routingKey) {
110110
});
111111
}
112112

113+
/// <summary>
114+
/// Cancels the broker queue.
115+
/// </summary>
116+
/// <returns></returns>
117+
public Task CancelAsync() {
118+
_consumer = null;
119+
return _broker.Context.AskWork(() => {
120+
_broker.Channel.BasicCancel(_consumerTag);
121+
return null;
122+
});
123+
}
124+
113125
/// <summary>
114126
/// Disposes
115127
/// </summary>

src/Holon/Protocol/ObservableConsumer.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,26 @@ namespace Holon.Protocol
1515
internal class ObservableConsumer : IBasicConsumer, IObservable<InboundMessage>
1616
{
1717
#region Fields
18-
private IModel _channel;
18+
private Broker _broker;
1919
private int _cancelled;
2020
private List<SubscribedObserver> _subscriptions = new List<SubscribedObserver>(1);
2121
#endregion
2222

23+
/// <summary>
24+
/// Gets the underlying broker.
25+
/// </summary>
26+
public Broker Broker {
27+
get {
28+
return _broker;
29+
}
30+
}
31+
2332
/// <summary>
2433
/// Gets the underlying channel.
2534
/// </summary>
2635
public IModel Model {
2736
get {
28-
return _channel;
37+
return _broker.Channel;
2938
}
3039
}
3140

@@ -87,7 +96,7 @@ public void HandleBasicConsumeOk(string consumerTag) {
8796
public void HandleBasicDeliver(string consumerTag, ulong deliveryTag, bool redelivered, string exchange, string routingKey, IBasicProperties properties, byte[] body) {
8897
lock(_subscriptions) {
8998
foreach (SubscribedObserver observer in _subscriptions)
90-
observer.Observer.OnNext(new InboundMessage(_channel, deliveryTag, redelivered, exchange, routingKey, properties, body));
99+
observer.Observer.OnNext(new InboundMessage(Model, deliveryTag, redelivered, exchange, routingKey, properties, body));
91100
}
92101
}
93102

@@ -170,9 +179,9 @@ struct SubscribedObserver
170179
/// <summary>
171180
/// Creates a new observable consumer.
172181
/// </summary>
173-
/// <param name="channel">The channel.</param>
174-
public ObservableConsumer(IModel channel) {
175-
_channel = channel;
182+
/// <param name="broker">The broker.</param>
183+
public ObservableConsumer(Broker broker) {
184+
_broker = broker;
176185
}
177186
#endregion
178187
}

src/Holon/Services/Service.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,24 @@ public void Dispose() {
155155
return;
156156

157157
// dispose of queue
158-
_queue.Unbind(_addr.Namespace, _addr.RoutingKey);
159158
_queue.Dispose();
160159

161160
// detach from node
162161
_namespace.Node.Detach(this);
163162
}
164163

164+
/// <summary>
165+
/// Shutdown the service.
166+
/// </summary>
167+
/// <returns></returns>
168+
internal async Task ShutdownAsync() {
169+
// cancel
170+
await _queue.CancelAsync().ConfigureAwait(false);
171+
172+
// dispose
173+
Dispose();
174+
}
175+
165176
/// <summary>
166177
/// Creates the queue and internal consumer for this service.
167178
/// </summary>

0 commit comments

Comments
 (0)