8000 CSOT · mongodb/docs-kotlin@971937e · GitHub
[go: up one dir, main page]

Skip to content

Commit 971937e

Browse files
committed
CSOT
1 parent 3256557 commit 971937e

16 files changed

+511
-15
lines changed

examples/src/test/kotlin/CompoundTest.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ internal class CompoundOperationsTest {
8181
val update = Updates.set(FoodOrder::food.name, "pizza")
8282
val options = FindOneAndUpdateOptions()
8383
.upsert(true)
84-
.maxTime(5, TimeUnit.SECONDS)
8584
/* The result variable contains your document in the
8685
state before your update operation is performed
8786
or null if the document was inserted due to upsert
@@ -183,4 +182,4 @@ internal class CompoundOperationsTest {
183182
assertEquals("joe", roomAfterSafe.first().guest)
184183
assertTrue(roomAfterSafe.first().reserved)
185184
}
186-
}
185+
}

examples/src/test/kotlin/CsotTest.kt

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
2+
import com.mongodb.ClientSessionOptions
3+
import com.mongodb.ConnectionString
4+
import com.mongodb.MongoClientSettings
5+
import com.mongodb.TransactionOptions
6+
import com.mongodb.client.cursor.TimeoutMode
7+
import com.mongodb.client.model.Filters
8+
import com.mongodb.kotlin.client.coroutine.MongoClient
9+
import config.getConfig
10+
import kotlinx.coroutines.runBlocking
11+
import org.bson.Document
12+
import org.junit.jupiter.api.AfterAll
13+
import org.junit.jupiter.api.BeforeAll
14+
import org.junit.jupiter.api.Test
15+
import java.util.concurrent.TimeUnit
16+
import kotlin.test.Ignore
17+
18+
class CsotTest {
19+
20+
companion object {
21+
val config = getConfig()
22+
val CONNECTION_URI_PLACEHOLDER = config.connectionUri
23+
val client = MongoClient.create(CONNECTION_URI_PLACEHOLDER)
24+
val database = client.getDatabase("db")
25+
val collection = database.getCollection<Document>("people")
26+
27+
@BeforeAll
28+
@JvmStatic
29+
fun beforeAll() {
30+
runBlocking {
31+
val people = listOf(
32+
Document("name", "Shelley Price").append("age", 56),
33+
Document("name", "Garrett John").append("age", 39),
34+
Document("name", "Kalima Sheik").append("age", 26)
35+
)
36+
collection.insertMany(people)
37+
}
38+
}
39+
40+
@AfterAll
41+
@JvmStatic
42+
fun afterAll() {
43+
runBlocking {
44+
database.drop()
45+
client.close()
46+
}
47+
}
48+
}
49+
50+
@Ignore
51+
fun connectionStringTest() = runBlocking {
52+
// :snippet-start: connection-string-timeout
53+
val uri = "<connection string>/?timeoutMS=200"
54+
val client = MongoClient.create(uri)
55+
// :snippet-end:
56+
}
57+
58+
// :replace-start: {
59+
// "terms": {
60+
// "uri": "\"<connection string>\""
61+
// }
62+
// }
63+
64+
@Test
65+
fun mongoClientSettingsTest() = runBlocking {
66+
val uri = CONNECTION_URI_PLACEHOLDER
67+
// :snippet-start: mongoclientsettings-timeout
68+
val settings = MongoClientSettings.builder()
69+
.applyConnectionString(ConnectionString(uri))
70+
.timeout(200L, TimeUnit.MILLISECONDS)
71+
.build()
72+
73+
val client = MongoClient.create(settings)
74+
// :snippet-end:
75+
}
76+
77+
@Test
78+
fun operationTimeoutTest() = runBlocking {
79+
val uri = CONNECTION_URI_PLACEHOLDER
80+
// :snippet-start: operation
81+
val settings = MongoClientSettings.builder()
82+
.applyConnectionString(ConnectionString(uri))
83+
.timeout(200L, TimeUnit.MILLISECONDS)
84+
.build()
85+
86+
val client = MongoClient.create(settings)
87+
val collection = client
88+
.getDatabase("db")
89+
.getCollection<Document>("people")
90+
91+
collection.insertOne(Document("name", "Francine Loews"))
92+
// :snippet-end:
93+
}
94+
95+
@Test
96+
fun overrideTimeoutTest() = runBlocking {
97+
val uri = CONNECTION_URI_PLACEHOLDER
98+
// :snippet-start: override
99+
val settings = MongoClientSettings.builder()
100+
.applyConnectionString(ConnectionString(uri))
101+
.timeout(200L, TimeUnit.MILLISECONDS)
102+
.build()
103+
104+
val client = MongoClient.create(settings)
105+
val database = client.getDatabase("db")
106+
val collection = database
107+
.getCollection<Document>("people")
108+
.withTimeout(300L, TimeUnit.MILLISECONDS)
109+
// :snippet-end:
110+
}
111+
112+
@Test
113+
fun transactionTimeoutTest() = runBlocking {
114+
// :snippet-start: session
115+
val opts = ClientSessionOptions.builder()
116+
.defaultTimeout(200L, TimeUnit.MILLISECONDS)
117+
.build()
118+
119+
val session = client.startSession(opts)
120+
// ... perform operations on ClientSession
121+
// :snippet-end:
122+
123+
// :snippet-start: transaction
124+
val transactionOptions = TransactionOptions.builder()
125+
.timeout(200L, TimeUnit.MILLISECONDS)
126+
.build()
127+
// :snippet-end:
128+
}
129+
130+
@Test
131+
fun cursorTimeoutTest() = runBlocking {
132+
val collection = database
133+
.getCollection<Document>("people")
134+
.withTimeout(200L, TimeUnit.MILLISECONDS)
135+
136+
// :snippet-start: cursor-lifetime
137+
val flowWithLifetimeTimeout = collection
138+
.find(Filters.gte("age", 40))
139+
.timeoutMode(TimeoutMode.CURSOR_LIFETIME)
140+
// :snippet-end:
141+
}
142+
}
143+
144+
// :replace-end:

source/examples/generated/CompoundTest.snippet.find-one-update.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ val filter = Filters.eq(FoodOrder::color.name, "green")
33
val update = Updates.set(FoodOrder::food.name, "pizza")
44
val options = FindOneAndUpdateOptions()
55
.upsert(true)
6-
.maxTime(5, TimeUnit.SECONDS)
76
/* The result variable contains your document in the
87
state before your update operation is performed
98
or null if the document was inserted due to upsert
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
val uri = "<connection string>/?timeoutMS=200"
2+
val client = MongoClient.create(uri)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val flowWithLifetimeTimeout = collection
2+
.find(Filters.gte("age", 40))
3+
.timeoutMode(TimeoutMode.CURSOR_LIFETIME)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val settings = MongoClientSettings.builder()
2+
.applyConnectionString(ConnectionString("<connection string>"))
3+
.timeout(200L, TimeUnit.MILLISECONDS)
4+
.build()
5+
6+
val client = MongoClient.create(settings)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
val settings = MongoClientSettings.builder()
2+
.applyConnectionString(ConnectionString("<connection string>"))
3+
.timeout(200L, TimeUnit.MILLISECONDS)
4+
.build()
5+
6+
val client = MongoClient.create(settings)
7+
val collection = client
8+
.getDatabase("db")
9+
.getCollection<Document>("people")
10+
11+
collection.insertOne(Document("name", "Francine Loews"))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
val settings = MongoClientSettings.builder()
2+
.applyConnectionString(ConnectionString("<connection string>"))
3+
.timeout(200L, TimeUnit.MILLISECONDS)
4+
.build()
5+
6+
val client = MongoClient.create(settings)
7+
val database = client.getDatabase("db")
8+
val collection = database
9+
.getCollection<Document>("people")
10+
.withTimeout(300L, TimeUnit.MILLISECONDS)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
val opts = ClientSessionOptions.builder()
2+
.defaultTimeout(200L, TimeUnit.MILLISECONDS)
3+
.build()
4+
5+
val session = client.startSession(opts)
6+
// ... perform operations on ClientSession
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
val transactionOptions = TransactionOptions.builder()
2+
.timeout(200L, TimeUnit.MILLISECONDS)
3+
.build()

source/fundamentals/connection.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Connection Guide
99
Connect to MongoDB </fundamentals/connection/connect>
1010
Connection Options </fundamentals/connection/connection-options>
1111
MongoClient Settings </fundamentals/connection/mongoclientsettings>
12+
Limit Execution Time </fundamentals/connection/csot>
1213
Network Compression </fundamentals/connection/network-compression>
1314
TLS/SSL </fundamentals/connection/tls>
1415
SOCKS5 Proxy Connection </fundamentals/connection/socks5>
@@ -29,6 +30,7 @@ sections:
2930
- :ref:`Connect to MongoDB <connect-to-mongodb>`
3031
- :ref:`View a List of Connection Options <connection-options>`
3132
- :ref:`Specify Connection Behavior with the MongoClient Class <specify-mongoclient-settings>`
33+
- :ref:`Limit Server Execution Time <kotlin-csot>`
3234
- :ref:`Enable Network Compression <network-compression>`
3335
- :ref:`Enable TLS/SSL on a Connection <tls-ssl>`
3436
- :ref:`Connect to MongoDB by Using a SOCKS5 Proxy <kotlin-connect-socks>`

source/fundamentals/connection/connection-options.txt

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,15 @@ parameters of the connection URI to specify the behavior of the client.
3030

3131
| **Default**: ``100``
3232

33-
* - **waitQueueTimeoutMS**
33+
* - **waitQueueTimeoutMS** *(deprecated)*
3434
- integer
35-
- Specifies the maximum amount of time, in milliseconds that a
36-
thread may wait for a connection to become available.
35+
- This option is deprecated. You can configure this timeout by
36+
setting the the :ref:`client-level timeout <kotlin-csot>`
37+
instead.
38+
39+
Maximum wait time in milliseconds that an operation can wait for
40+
a connection to become available. A value of ``0`` means there
41+
is no limit.
3742

3843
| **Default**: ``120000`` (120 seconds)
3944

@@ -111,9 +116,13 @@ parameters of the connection URI to specify the behavior of the client.
111116

112117
| **Default**: ``10000`` (10 seconds)
113118

114-
* - **socketTimeoutMS**
119+
* - **socketTimeoutMS** *(deprecated)*
115120
- integer
116-
- Specifies the maximum amount of time, in milliseconds, the Kotlin
121+
- This option is deprecated. You can configure this timeout by
122+
setting the the :ref:`client-level timeout <kotlin-csot>`
123+
instead.
124+
125+
Maximum amount of time, in milliseconds, the Kotlin
117126
driver will wait to send or receive a request before timing out.
118127
A value of ``0`` instructs the driver to never time out while waiting
119128
to send or receive a request.
@@ -153,9 +162,14 @@ parameters of the connection URI to specify the behavior of the client.
153162

154163
| **Default**: ``1``
155164

156-
* - **wtimeoutMS**
165+
* - **wtimeoutMS** *(deprecated)*
157166
- integer
158-
- Specifies a time limit, in milliseconds, for the write concern. For
167+
168+
- This option is deprecated. You can configure this timeout by
169+
setting the the :ref:`client-level timeout <kotlin-csot>`
170+
instead.
171+
172+
Time limit, in milliseconds, for the write concern. For
159173
more information, see the server documentation for the
160174
:manual:`wtimeoutMS option </reference/connection-string/#write-concern-options>`.
161175
A value of ``0`` instructs the driver to never time out write operations.

0 commit comments

Comments
 (0)
0