...

Source file src/go.mongodb.org/mongo-driver/x/mongo/driver/topology/server_options.go

Documentation: go.mongodb.org/mongo-driver/x/mongo/driver/topology

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package topology
     8  
     9  import (
    10  	"time"
    11  
    12  	"go.mongodb.org/mongo-driver/bson"
    13  	"go.mongodb.org/mongo-driver/bson/bsoncodec"
    14  	"go.mongodb.org/mongo-driver/event"
    15  	"go.mongodb.org/mongo-driver/internal/logger"
    16  	"go.mongodb.org/mongo-driver/x/mongo/driver"
    17  	"go.mongodb.org/mongo-driver/x/mongo/driver/connstring"
    18  	"go.mongodb.org/mongo-driver/x/mongo/driver/session"
    19  )
    20  
    21  var defaultRegistry = bson.NewRegistryBuilder().Build()
    22  
    23  type serverConfig struct {
    24  	clock                *session.ClusterClock
    25  	compressionOpts      []string
    26  	connectionOpts       []ConnectionOption
    27  	appname              string
    28  	heartbeatInterval    time.Duration
    29  	heartbeatTimeout     time.Duration
    30  	serverMonitoringMode string
    31  	serverMonitor        *event.ServerMonitor
    32  	registry             *bsoncodec.Registry
    33  	monitoringDisabled   bool
    34  	serverAPI            *driver.ServerAPIOptions
    35  	loadBalanced         bool
    36  
    37  	// Connection pool options.
    38  	maxConns             uint64
    39  	minConns             uint64
    40  	maxConnecting        uint64
    41  	poolMonitor          *event.PoolMonitor
    42  	logger               *logger.Logger
    43  	poolMaxIdleTime      time.Duration
    44  	poolMaintainInterval time.Duration
    45  }
    46  
    47  func newServerConfig(opts ...ServerOption) *serverConfig {
    48  	cfg := &serverConfig{
    49  		heartbeatInterval: 10 * time.Second,
    50  		heartbeatTimeout:  10 * time.Second,
    51  		registry:          defaultRegistry,
    52  	}
    53  
    54  	for _, opt := range opts {
    55  		if opt == nil {
    56  			continue
    57  		}
    58  		opt(cfg)
    59  	}
    60  
    61  	return cfg
    62  }
    63  
    64  // ServerOption configures a server.
    65  type ServerOption func(*serverConfig)
    66  
    67  // ServerAPIFromServerOptions will return the server API options if they have been functionally set on the ServerOption
    68  // slice.
    69  func ServerAPIFromServerOptions(opts []ServerOption) *driver.ServerAPIOptions {
    70  	return newServerConfig(opts...).serverAPI
    71  }
    72  
    73  func withMonitoringDisabled(fn func(bool) bool) ServerOption {
    74  	return func(cfg *serverConfig) {
    75  		cfg.monitoringDisabled = fn(cfg.monitoringDisabled)
    76  	}
    77  }
    78  
    79  // WithConnectionOptions configures the server's connections.
    80  func WithConnectionOptions(fn func(...ConnectionOption) []ConnectionOption) ServerOption {
    81  	return func(cfg *serverConfig) {
    82  		cfg.connectionOpts = fn(cfg.connectionOpts...)
    83  	}
    84  }
    85  
    86  // WithCompressionOptions configures the server's compressors.
    87  func WithCompressionOptions(fn func(...string) []string) ServerOption {
    88  	return func(cfg *serverConfig) {
    89  		cfg.compressionOpts = fn(cfg.compressionOpts...)
    90  	}
    91  }
    92  
    93  // WithServerAppName configures the server's application name.
    94  func WithServerAppName(fn func(string) string) ServerOption {
    95  	return func(cfg *serverConfig) {
    96  		cfg.appname = fn(cfg.appname)
    97  	}
    98  }
    99  
   100  // WithHeartbeatInterval configures a server's heartbeat interval.
   101  func WithHeartbeatInterval(fn func(time.Duration) time.Duration) ServerOption {
   102  	return func(cfg *serverConfig) {
   103  		cfg.heartbeatInterval = fn(cfg.heartbeatInterval)
   104  	}
   105  }
   106  
   107  // WithHeartbeatTimeout configures how long to wait for a heartbeat socket to
   108  // connection.
   109  func WithHeartbeatTimeout(fn func(time.Duration) time.Duration) ServerOption {
   110  	return func(cfg *serverConfig) {
   111  		cfg.heartbeatTimeout = fn(cfg.heartbeatTimeout)
   112  	}
   113  }
   114  
   115  // WithMaxConnections configures the maximum number of connections to allow for
   116  // a given server. If max is 0, then maximum connection pool size is not limited.
   117  func WithMaxConnections(fn func(uint64) uint64) ServerOption {
   118  	return func(cfg *serverConfig) {
   119  		cfg.maxConns = fn(cfg.maxConns)
   120  	}
   121  }
   122  
   123  // WithMinConnections configures the minimum number of connections to allow for
   124  // a given server. If min is 0, then there is no lower limit to the number of
   125  // connections.
   126  func WithMinConnections(fn func(uint64) uint64) ServerOption {
   127  	return func(cfg *serverConfig) {
   128  		cfg.minConns = fn(cfg.minConns)
   129  	}
   130  }
   131  
   132  // WithMaxConnecting configures the maximum number of connections a connection
   133  // pool may establish simultaneously. If maxConnecting is 0, the default value
   134  // of 2 is used.
   135  func WithMaxConnecting(fn func(uint64) uint64) ServerOption {
   136  	return func(cfg *serverConfig) {
   137  		cfg.maxConnecting = fn(cfg.maxConnecting)
   138  	}
   139  }
   140  
   141  // WithConnectionPoolMaxIdleTime configures the maximum time that a connection can remain idle in the connection pool
   142  // before being removed. If connectionPoolMaxIdleTime is 0, then no idle time is set and connections will not be removed
   143  // because of their age
   144  func WithConnectionPoolMaxIdleTime(fn func(time.Duration) time.Duration) ServerOption {
   145  	return func(cfg *serverConfig) {
   146  		cfg.poolMaxIdleTime = fn(cfg.poolMaxIdleTime)
   147  	}
   148  }
   149  
   150  // WithConnectionPoolMaintainInterval configures the interval that the background connection pool
   151  // maintenance goroutine runs.
   152  func WithConnectionPoolMaintainInterval(fn func(time.Duration) time.Duration) ServerOption {
   153  	return func(cfg *serverConfig) {
   154  		cfg.poolMaintainInterval = fn(cfg.poolMaintainInterval)
   155  	}
   156  }
   157  
   158  // WithConnectionPoolMonitor configures the monitor for all connection pool actions
   159  func WithConnectionPoolMonitor(fn func(*event.PoolMonitor) *event.PoolMonitor) ServerOption {
   160  	return func(cfg *serverConfig) {
   161  		cfg.poolMonitor = fn(cfg.poolMonitor)
   162  	}
   163  }
   164  
   165  // WithServerMonitor configures the monitor for all SDAM events for a server
   166  func WithServerMonitor(fn func(*event.ServerMonitor) *event.ServerMonitor) ServerOption {
   167  	return func(cfg *serverConfig) {
   168  		cfg.serverMonitor = fn(cfg.serverMonitor)
   169  	}
   170  }
   171  
   172  // WithClock configures the ClusterClock for the server to use.
   173  func WithClock(fn func(clock *session.ClusterClock) *session.ClusterClock) ServerOption {
   174  	return func(cfg *serverConfig) {
   175  		cfg.clock = fn(cfg.clock)
   176  	}
   177  }
   178  
   179  // WithRegistry configures the registry for the server to use when creating
   180  // cursors.
   181  func WithRegistry(fn func(*bsoncodec.Registry) *bsoncodec.Registry) ServerOption {
   182  	return func(cfg *serverConfig) {
   183  		cfg.registry = fn(cfg.registry)
   184  	}
   185  }
   186  
   187  // WithServerAPI configures the server API options for the server to use.
   188  func WithServerAPI(fn func(serverAPI *driver.ServerAPIOptions) *driver.ServerAPIOptions) ServerOption {
   189  	return func(cfg *serverConfig) {
   190  		cfg.serverAPI = fn(cfg.serverAPI)
   191  	}
   192  }
   193  
   194  // WithServerLoadBalanced specifies whether or not the server is behind a load balancer.
   195  func WithServerLoadBalanced(fn func(bool) bool) ServerOption {
   196  	return func(cfg *serverConfig) {
   197  		cfg.loadBalanced = fn(cfg.loadBalanced)
   198  	}
   199  }
   200  
   201  // withLogger configures the logger for the server to use.
   202  func withLogger(fn func() *logger.Logger) ServerOption {
   203  	return func(cfg *serverConfig) {
   204  		cfg.logger = fn()
   205  	}
   206  }
   207  
   208  // withServerMonitoringMode configures the mode (stream, poll, or auto) to use
   209  // for monitoring.
   210  func withServerMonitoringMode(mode *string) ServerOption {
   211  	return func(cfg *serverConfig) {
   212  		if mode != nil {
   213  			cfg.serverMonitoringMode = *mode
   214  
   215  			return
   216  		}
   217  
   218  		cfg.serverMonitoringMode = connstring.ServerMonitoringModeAuto
   219  	}
   220  }
   221  

View as plain text