diff --git a/src/NATS.Client/JetStream/JetStream.cs b/src/NATS.Client/JetStream/JetStream.cs index 5ee15ce32..0761d6f56 100644 --- a/src/NATS.Client/JetStream/JetStream.cs +++ b/src/NATS.Client/JetStream/JetStream.cs @@ -86,9 +86,7 @@ private PublishAck PublishSyncInternal(string subject, byte[] data, MsgHeader hd return null; } - Duration timeout = options == null ? JetStreamOptions.RequestTimeout : options.StreamTimeout; - - return ProcessPublishResponse(Conn.Request(msg, timeout.Millis), options); + return ProcessPublishResponse(Conn.Request(msg, Timeout), options); } private async Task PublishAsyncInternal(string subject, byte[] data, MsgHeader hdr, PublishOptions options) @@ -102,9 +100,7 @@ private async Task PublishAsyncInternal(string subject, byte[] data, return null; } - Duration timeout = options == null ? JetStreamOptions.RequestTimeout : options.StreamTimeout; - - var result = await Conn.RequestAsync(msg, timeout.Millis).ConfigureAwait(false); + var result = await Conn.RequestAsync(msg, Timeout).ConfigureAwait(false); return ProcessPublishResponse(result, options); } diff --git a/src/NATS.Client/JetStream/JetStreamBase.cs b/src/NATS.Client/JetStream/JetStreamBase.cs index d41ae2262..43b94dd66 100644 --- a/src/NATS.Client/JetStream/JetStreamBase.cs +++ b/src/NATS.Client/JetStream/JetStreamBase.cs @@ -44,7 +44,7 @@ protected JetStreamBase(IConnection connection, JetStreamOptions options) Conn = connection; JetStreamOptions = options ?? JetStreamOptions.DefaultJsOptions; Prefix = JetStreamOptions.Prefix; - Timeout = JetStreamOptions.RequestTimeout.Millis; + Timeout = JetStreamOptions.RequestTimeout?.Millis ?? Conn.Opts.Timeout; } internal static ServerInfo ServerInfoOrException(IConnection conn) diff --git a/src/NATS.Client/JetStream/JetStreamOptions.cs b/src/NATS.Client/JetStream/JetStreamOptions.cs index 1dc834d93..7d8c5ec5b 100644 --- a/src/NATS.Client/JetStream/JetStreamOptions.cs +++ b/src/NATS.Client/JetStream/JetStreamOptions.cs @@ -11,6 +11,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +using System; using NATS.Client.Internals; using static NATS.Client.Internals.JetStreamConstants; using static NATS.Client.Internals.NatsConstants; @@ -20,7 +21,9 @@ namespace NATS.Client.JetStream { public sealed class JetStreamOptions { + [Obsolete("This property is obsolete. The connection options request timeout is used as the default", false)] public static readonly Duration DefaultTimeout = Duration.OfMillis(Defaults.Timeout); + public static readonly JetStreamOptions DefaultJsOptions = Builder().Build(); private JetStreamOptions(JetStreamOptionsBuilder b) @@ -94,7 +97,7 @@ public static JetStreamOptionsBuilder Builder(JetStreamOptions jso) public sealed class JetStreamOptionsBuilder { internal string _jsPrefix; - internal Duration _requestTimeout = DefaultTimeout; + internal Duration _requestTimeout; internal bool _publishNoAck; internal bool _optOut290ConsumerCreate; @@ -160,7 +163,7 @@ public JetStreamOptionsBuilder WithDomain(string domain) /// The JetStreamOptionsBuilder public JetStreamOptionsBuilder WithRequestTimeout(Duration requestTimeout) { - _requestTimeout = EnsureNotNullAndNotLessThanMin(requestTimeout, Duration.Zero, DefaultTimeout); + _requestTimeout = requestTimeout; return this; } @@ -169,9 +172,9 @@ public JetStreamOptionsBuilder WithRequestTimeout(Duration requestTimeout) /// /// The request timeout in millis. /// The JetStreamOptionsBuilder - public JetStreamOptionsBuilder WithRequestTimeout(long requestTimeoutMillis) + public JetStreamOptionsBuilder WithRequestTimeout(long requestTimeoutMillis) { - _requestTimeout = EnsureDurationNotLessThanMin(requestTimeoutMillis, Duration.Zero, DefaultTimeout); + _requestTimeout = requestTimeoutMillis < 0 ? null : Duration.OfMillis(requestTimeoutMillis); return this; } @@ -201,7 +204,6 @@ public JetStreamOptionsBuilder WithOptOut290ConsumerCreate(bool optOut) { /// The JetStreamOptions object. public JetStreamOptions Build() { - _requestTimeout = _requestTimeout ?? DefaultTimeout; return new JetStreamOptions(this); } } diff --git a/src/Tests/IntegrationTests/TestObjectStore.cs b/src/Tests/IntegrationTests/TestObjectStore.cs index 26efb7aee..26aab94d1 100644 --- a/src/Tests/IntegrationTests/TestObjectStore.cs +++ b/src/Tests/IntegrationTests/TestObjectStore.cs @@ -319,7 +319,7 @@ public void TestObjectStoreOptionsBuilderCoverage() { private void AssertOso(ObjectStoreOptions oso) { JetStreamOptions jso = oso.JSOptions; - Assert.Equal(DefaultJsOptions.RequestTimeout, jso.RequestTimeout); + Assert.Null(jso.RequestTimeout); Assert.Equal(DefaultJsOptions.Prefix, jso.Prefix); Assert.Equal(DefaultJsOptions.IsDefaultPrefix, jso.IsDefaultPrefix); Assert.Equal(DefaultJsOptions.IsPublishNoAck, jso.IsPublishNoAck); diff --git a/src/Tests/UnitTests/JetStream/TestJetStreamOptions.cs b/src/Tests/UnitTests/JetStream/TestJetStreamOptions.cs index 3f015e6fb..a080f3357 100644 --- a/src/Tests/UnitTests/JetStream/TestJetStreamOptions.cs +++ b/src/Tests/UnitTests/JetStream/TestJetStreamOptions.cs @@ -12,7 +12,6 @@ // limitations under the License. using System; -using NATS.Client; using NATS.Client.Internals; using NATS.Client.JetStream; using Xunit; @@ -27,7 +26,7 @@ public void TestBuilder() { // default JetStreamOptions jso = JetStreamOptions.Builder().Build(); - Assert.Equal(Duration.OfMillis(Defaults.Timeout), jso.RequestTimeout); + Assert.Null(jso.RequestTimeout); Assert.Equal(DefaultApiPrefix, jso.Prefix); Assert.True(jso.IsDefaultPrefix); Assert.False(jso.IsPublishNoAck); @@ -35,7 +34,7 @@ public void TestBuilder() // default copy jso = JetStreamOptions.Builder(jso).Build(); - Assert.Equal(Duration.OfMillis(Defaults.Timeout), jso.RequestTimeout); + Assert.Null(jso.RequestTimeout); Assert.Equal(DefaultApiPrefix, jso.Prefix); Assert.True(jso.IsDefaultPrefix); Assert.False(jso.IsPublishNoAck);