1
1
using System . Buffers ;
2
2
using System . Collections . Concurrent ;
3
3
using System . Runtime . CompilerServices ;
4
- using System . Threading ;
5
4
using Microsoft . Extensions . Logging ;
6
5
using NATS . Client . Core . Commands ;
7
6
@@ -42,7 +41,7 @@ public SubscriptionManager(NatsConnection connection, string inboxPrefix)
42
41
_cleanupInterval = _connection . Opts . SubscriptionCleanUpInterval ;
43
42
_timer = Task . Run ( CleanupAsync ) ;
44
43
InboxSubBuilder = new InboxSubBuilder ( connection . Opts . LoggerFactory . CreateLogger < InboxSubBuilder > ( ) ) ;
45
- _inboxSubSentinel = new InboxSub ( InboxSubBuilder , new NatsSubscriptionProps ( nameof ( _inboxSubSentinel ) ) , default , connection , this ) ;
44
+ _inboxSubSentinel = new InboxSub ( InboxSubBuilder , new NatsSubscriptionProps ( nameof ( _inboxSubSentinel ) , _connection . InboxPrefix ) , default , connection , this ) ;
46
45
_inboxSub = _inboxSubSentinel ;
47
46
}
48
47
@@ -62,7 +61,7 @@ public ValueTask PublishToClientHandlersAsync(NatsProcessProps props, in ReadOnl
62
61
_logger . LogTrace ( NatsLogEvents . Subscription , "Received subscription data for {Subject}/{Sid}" , props . Subject , props . SubscriptionId ) ;
63
62
}
64
63
65
- int ? orphanSid = null ;
64
+ var orphan = false ;
66
65
lock ( _gate )
67
66
{
68
67
if ( _bySid . TryGetValue ( props . SubscriptionId , out var sidMetadata ) )
@@ -75,12 +74,12 @@ public ValueTask PublishToClientHandlersAsync(NatsProcessProps props, in ReadOnl
75
74
_logger . LogTrace ( NatsLogEvents . Subscription , "Found subscription handler for {Subject}/{Sid}" , props . Subject , props . SubscriptionId ) ;
76
75
}
77
76
78
- return sub . ReceiveAsync ( props . Subject , props . ReplyTo , headersBuffer , payloadBuffer ) ;
77
+ return sub . ReceiveAsync ( props , headersBuffer , payloadBuffer ) ;
79
78
}
80
79
else
81
80
{
82
81
_logger . LogWarning ( NatsLogEvents . Subscription , "Subscription GCd but was never disposed {Subject}/{Sid}" , props . Subject , props . SubscriptionId ) ;
83
- orphanSid = props . SubscriptionId ;
82
+ orphan = true ;
84
83
}
85
84
}
86
85
else
@@ -89,11 +88,11 @@ public ValueTask PublishToClientHandlersAsync(NatsProcessProps props, in ReadOnl
89
88
}
90
89
}
91
90
92
- if ( orphanSid != null )
91
+ if ( orphan )
93
92
{
94
93
try
95
94
{
96
- return _connection . UnsubscribeAsync ( new NatsSubscriptionProps ( orphanSid . Value ) ) ;
95
+ return _connection . UnsubscribeAsync ( new NatsSubscriptionProps ( props . SubscriptionId , props . InboxPrefix ) ) ;
97
96
}
98
97
catch ( Exception e )
99
98
{
@@ -148,7 +147,7 @@ public ValueTask RemoveAsync(NatsSubBase sub)
148
147
_logger . LogDebug ( NatsLogEvents . Subscription , "Removing subscription {Subject}/{Sid}" , sub . Subject , subMetadata . Sid ) ;
149
148
}
150
149
151
- return _connection . UnsubscribeAsync ( sub . Props ) ;
150
+ return _connection . UnsubscribeAsync ( sub . SubscriptionProps ( _connection . InboxPrefix ) ) ;
152
151
}
153
152
154
153
/// <summary>
@@ -183,7 +182,7 @@ internal async ValueTask WriteReconnectCommandsAsync(CommandWriter commandWriter
183
182
184
183
foreach ( var ( sub , sid ) in subs )
185
184
{
186
- await sub . WriteReconnectCommandsAsync ( commandWriter , new NatsSubscriptionProps ( sid ) ) . ConfigureAwait ( false ) ;
185
+ await sub . WriteReconnectCommandsAsync ( commandWriter , new NatsSubscriptionProps ( sid , _connection . InboxPrefix ) ) . ConfigureAwait ( false ) ;
187
186
188
187
if ( _debug )
189
188
{
@@ -208,18 +207,14 @@ internal async Task InitializeInboxSubscriptionAsync(CancellationToken cancellat
208
207
{
209
208
if ( Interlocked . CompareExchange ( ref _inboxSub , _inboxSubSentinel , _inboxSubSentinel ) == _inboxSubSentinel )
210
209
{
211
- var inboxSubject = new NatsSubscriptionProps ( $ "{ _inboxPrefix } .*") ;
210
+ var inboxSubject = new NatsSubscriptionProps ( $ "{ _inboxPrefix } .*", _connection . InboxPrefix ) ;
212
211
213
212
// We need to subscribe to the real inbox subject before we can register the internal subject.
214
213
// We use 'default' options here since options provided by the user are for the internal subscription.
215
214
// For example if the user provides a timeout, we don't want to timeout the real inbox subscription
216
215
// since it must live duration of the connection.
217
216
_inboxSub = InboxSubBuilder . Build ( inboxSubject , opts : default , _connection , manager : this ) ;
218
- var props = new NatsSubscriptionProps ( _inboxSub . Subject )
219
- {
220
- InboxPrefix = _connection . InboxPrefix ,
221
- QueueGroup = _inboxSub . QueueGroup ,
222
- } ;
217
+ var props = new NatsSubscriptionProps ( _inboxSub . Subject , _connection . InboxPrefix , _inboxSub . QueueGroup ) ;
223
218
await SubscribeQueueAsync ( props , _inboxSub , cancellationToken ) . ConfigureAwait ( false ) ;
224
219
}
225
220
}
@@ -233,11 +228,7 @@ internal async Task InitializeInboxSubscriptionAsync(CancellationToken cancellat
233
228
private ValueTask SubscribeInternalAsync ( NatsSubBase sub , CancellationToken cancellationToken )
234
229
{
235
230
var start = DateTimeOffset . UtcNow ;
236
- var props = new NatsSubscriptionProps ( sub . Subject )
237
- {
238
- InboxPrefix = _connection . InboxPrefix ,
239
- QueueGroup = sub . QueueGroup ,
240
- } ;
231
+ var props = new NatsSubscriptionProps ( sub . Subject , _connection . InboxPrefix , sub . QueueGroup ) ;
241
232
var tags = Telemetry . GetTags ( _connection . ServerInfo , props ) ;
242
233
using var activity = Telemetry . StartActivity ( start , props , _connection . ServerInfo , Telemetry . Constants . SubscribeActivityName , tags ) ;
243
234
ValueTask task ;
@@ -364,7 +355,7 @@ private async ValueTask UnsubscribeSidsAsync(List<int> sids)
364
355
try
365
356
{
366
357
_logger . LogWarning ( NatsLogEvents . Subscription , "Unsubscribing orphan subscription {Sid}" , sid ) ;
367
- await _connection . UnsubscribeAsync ( new NatsSubscriptionProps ( sid ) ) . ConfigureAwait ( false ) ;
358
+ await _connection . UnsubscribeAsync ( new NatsSubscriptionProps ( sid , _connection . InboxPrefix ) ) . ConfigureAwait ( false ) ;
368
359
}
369
360
catch ( Exception e )
370
361
{
0 commit comments