Skip to content

Commit 8b95df1

Browse files
committed
DOCSP-35922: csot - gridfs
(cherry picked from commit abd9c10)
1 parent 535cf18 commit 8b95df1

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

source/connect-to-mongo/csot.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,36 @@ inherited timeout:
284284
:dedent:
285285
:emphasize-lines: 3
286286

287+
.. _java-rs-csot-gridfs:
288+
289+
GridFS
290+
------
291+
292+
You can set a timeout option for :ref:`GridFS <java-rs-store-large-docs>`
293+
operations when instantiating a ``GridFSBucket`` by using the
294+
``withTimeout()`` method. This timeout applies to all operations
295+
performed on the bucket, such as uploading and downloading data. If you
296+
do not set a timeout, the ``GridFSBucket`` instance inherits the timeout
297+
setting from the ``MongoDatabase`` it is created with.
298+
299+
The following code demonstrates how to set a timeout when instantiating
300+
a ``GridFSBucket``:
301+
302+
.. literalinclude:: /includes/connect/CsotExample.java
303+
:language: java
304+
:start-after: start-gridfsbucket-timeout
305+
:end-before: end-gridfsbucket-timeout
306+
:dedent:
307+
:emphasize-lines: 3
308+
309+
.. important:: InputStream Timeout Support
310+
311+
When you call the ``uploadFromStream()`` method on a ``GridFSBucket``
312+
that has an operation timeout, timeout breaches might occur because
313+
the ``InputStream`` class lacks inherent read timeout support. This might
314+
extend the operation beyond the specified timeout limit, causing a
315+
timeout exception.
316+
287317
API Documentation
288318
-----------------
289319

@@ -298,3 +328,4 @@ API documentation:
298328
- `ClientEncryptionSettings.Builder.timeout() <{+core-api+}/ClientEncryptionSettings.Builder.html#timeout(long,java.util.concurrent.TimeUnit)>`__
299329
- `FindIterable.timeoutMode() <{+driver-api+}/FindPublisher.html#timeoutMode(com.mongodb.client.cursor.TimeoutMode)>`__
300330
- `TimeoutMode <{+core-api+}/client/cursor/TimeoutMode.html>`__
331+
- `GridFSBucket.withTimeout() <{+driver-api+}/gridfs/GridFSBucket.html#withTimeout(long,java.util.concurrent.TimeUnit)>`__

source/includes/connect/CsotExample.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,22 @@ private void cursorTimeout(){
118118
}
119119

120120
}
121+
122+
private void gridFSTimeout(){
123+
MongoClientSettings settings = MongoClientSettings.builder()
124+
.applyConnectionString(new ConnectionString("<connection string>"))
125+
.build();
126+
127+
try (MongoClient mongoClient = MongoClients.create(settings)) {
128+
MongoDatabase database = mongoClient.getDatabase("db");
129+
130+
// start-gridfsbucket-timeout
131+
GridFSBucket gridFSBucket = GridFSBuckets
132+
.create(database)
133+
.withTimeout(200L, MILLISECONDS);
134+
// end-gridfsbucket-timeout
135+
}
136+
137+
}
138+
121139
}

source/write/store-large-docs.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ The code examples in this guide come from the `GridFSTour.java
2929
<{+driver-source-gh+}/blob/master/driver-reactive-streams/src/examples/reactivestreams/gridfs/GridFSTour.java>`__
3030
file in the driver source code GitHub repository.
3131

32+
.. tip:: Timeout Setting
33+
34+
You can use the client-side operation timeout (CSOT) setting to limit
35+
the amount of time in which the server can finish GridFS operations.
36+
To learn more about using this setting with GridFS, see the
37+
:ref:`java-rs-csot-gridfs` section of the Limit Server Execution Time
38+
guide.
39+
3240
Prerequisites
3341
-------------
3442

0 commit comments

Comments
 (0)