> This page location: /docs.md > /docs/drivers.md > /docs/languages/go.md > /docs/drivers/go/current/connect/specify-connection-options.md

> For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!--
Tab options on this page. Append to the .md URL to filter:
  ?tabs=<id,...>   select specific tabs (e.g. ?tabs=nodejs,shell)
  ?allTabs=true    include every tab
  (no param)       default: one tab per tabset

Available tabs:
  other tabs: mongoclientsettings, connection-string
-->

# Limit Server Execution Time

## Overview

In this guide, you can learn about the single timeout setting in the Go driver, also known as the **client-side operation timeout (CSOT)**.

When you use the Go driver to perform a server operation, you can also limit the amount of time in which the server can finish the operation. The timeout applies to all steps needed to complete the operation, including server selection, connection checkout, and server-side execution. When the timeout expires, the Go driver raises a timeout exception.

**Note: Experimental Feature**

The CSOT feature is experimental and might change in future driver releases.

## timeoutMS Option

To specify a timeout when connecting to a MongoDB deployment, set the `timeoutMS` connection option to the timeout length in milliseconds. You can set the `timeoutMS` option in the following ways:

- Calling the `SetTimeout()` method when specifying options for your `Client` instance

- Setting the `timeoutMS` parameter in your connection string

The following code examples set a client-level timeout of `200` milliseconds. Select the Client or Connection
String tab to see the corresponding code.

### MongoClientSettings

```go
opts := options.Client().SetTimeout(200 * time.Millisecond)
client, err := mongo.Connect(opts)
```

**Note: Retries Under Timeout Specification**

If you set a timeout on your `Client` or in an operation-level Context and the server returns a retryable error, the driver retries the operation as many times as possible before the timeout expires.

Once the timeout expires, the driver returns a timeout error. See the Server manual for more information about [retryable reads](https://www.mongodb.com/docs/manual/core/retryable-reads.md#std-label-retryable-reads) and [retryable writes.](https://www.mongodb.com/docs/manual/core/retryable-writes/)

### Accepted Timeout Values

The following table describes the timeout behavior corresponding to the accepted values for `timeoutMS`:

| Value | Behavior |
| --- | --- |
| Positive integer | Sets the timeout to use for operation completion. |
| `0` | Specifies that operations never time out. |
| `null` or unset | Defers the timeout behavior to the following settings: [waitQueueTimeoutMS](https://www.mongodb.com/docs/manual/reference/connection-string-options/#mongodb-urioption-urioption.waitQueueTimeoutMS); [socketTimeoutMS](https://www.mongodb.com/docs/manual/reference/connection-string-options/#mongodb-urioption-urioption.socketTimeoutMS); [wTimeoutMS](https://www.mongodb.com/docs/manual/reference/connection-string-options/#mongodb-urioption-urioption.wtimeoutMS); [maxTimeMS](https://www.mongodb.com/docs/manual/reference/method/cursor.maxTimeMS/) These settings are deprecated and are ignored if you set `timeoutMS`. |

If you specify the `timeoutMS` option, the driver automatically applies the specified timeout to each server operation.

### Timeout Inheritance

By default, all operations in your application inherit the `Timeout` option from `Client` if you do not set a different timeout on specific operations in the operation's Context.

If you set a timeout on a Context passed into an operation, the driver uses that value for the operation. If you do not specify a Context timeout, the operation Context derives the timeout from the `Client` instance.

The following table describes how the timeout value is inherited at each level:

| Level | Inheritance Description |
| --- | --- |
| Operation | Takes the highest precedence and overrides the timeout options that you set at any other level. |
| Transaction | Takes precedence over the timeout value that you set at the client level. |
| Client | Applies to all databases, collections, sessions, transactions, and operations within that client that do not otherwise specify a Context timeout. |

For more information on overrides and specific options, see the following [Overrides](https://www.mongodb.com/docs/drivers/go/current/connect/connection-options/csot.md#std-label-golang-csot-overrides) section.

## Overrides

The Go driver supports various levels of configuration to control the behavior and performance of database operations.

You can specify a `timeoutMS` option at a more specific level to override the client-level configuration. The table in the preceding section describes the levels at which you can specify a timeout setting. This allows you to customize timeouts based on the needs of individual operations.

The following example shows how to set an operation-level timeout in a Context, which takes priority over the client-level timeout:

```go
opts = options.Client().SetTimeout(200 * time.Millisecond)
client, err = mongo.Connect(opts)
if err != nil {
	log.Fatal(err)
}

coll := client.Database("db").Collection("people")

ctx, cancel := context.WithTimeout(context.TODO(), 300*time.Millisecond)
defer cancel()

_, err = coll.InsertOne(ctx, bson.D{{"name", "Agnes Georgiou"}})
```

### Transactions

When you perform a transaction by using the [WithTransaction()](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Session.WithTransaction) method, you can apply a timeout to the transaction operations by setting a timeout within the Context.

The following code demonstrates how to set a Context timeout when calling the `WithTransaction()` method to perform a transaction:

```go
txnContext, cancel := context.WithTimeout(context.TODO(), 300*time.Millisecond)
defer cancel()

result, err := session.WithTransaction(txnContext, func(ctx context.Context) (string, error) {
	// Perform transaction operations
})
```

If you do not specify a Context timeout, the driver inherits the timeout value set on the parent `Client`.

You can also pass Context timeouts to the following session methods:

- `AbortTransaction()`

- `CommitTransaction()`

- `EndSession()`

To learn more about transactions, see the [Transactions](https://www.mongodb.com/docs/drivers/go/current/crud/transactions.md#std-label-golang-transactions) guide.

## Cursors

Cursors offer configurable timeout settings when using the CSOT feature. You can adjust cursor timeouts by passing Contexts that have timeout specifications to [Cursor](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo#Cursor) methods.

For operations that create cursors, the timeout setting can either limit the lifetime of the cursor or be applied separately to the original operation and all subsequent calls.

For example, if you pass a Context timeout to the `Cursor.Next()` method, the timeout applies to each action to fetch a result document. If you pass a Context timeout to the `Cursor.All()` method, the timeout applies to the entire lifetime of the cursor.

To learn more about cursors, see the [Access Data From a Cursor](https://www.mongodb.com/docs/drivers/go/current/crud/query/cursor.md#std-label-golang-cursor) guide.

## API Documentation

To learn more about using timeouts with the Go driver, see the following API documentation:

- [ClientOptions](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo/options#ClientOptions)

- [SetTimeout()](https://pkg.go.dev/go.mongodb.org/mongo-driver/v2/mongo/options#ClientOptions.SetTimeout)

- [Context.WithTimeout()](https://pkg.go.dev/context#WithTimeout)
