Don Newton | 379ae25 | 2019-04-01 12:17:06 -0400 | [diff] [blame^] | 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 connection |
| 8 | |
| 9 | import ( |
| 10 | "net" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/mongodb/mongo-go-driver/event" |
| 14 | "github.com/mongodb/mongo-go-driver/x/network/compressor" |
| 15 | ) |
| 16 | |
| 17 | type config struct { |
| 18 | appName string |
| 19 | connectTimeout time.Duration |
| 20 | dialer Dialer |
| 21 | handshaker Handshaker |
| 22 | idleTimeout time.Duration |
| 23 | lifeTimeout time.Duration |
| 24 | cmdMonitor *event.CommandMonitor |
| 25 | readTimeout time.Duration |
| 26 | writeTimeout time.Duration |
| 27 | tlsConfig *TLSConfig |
| 28 | compressors []compressor.Compressor |
| 29 | } |
| 30 | |
| 31 | func newConfig(opts ...Option) (*config, error) { |
| 32 | cfg := &config{ |
| 33 | connectTimeout: 30 * time.Second, |
| 34 | dialer: nil, |
| 35 | idleTimeout: 10 * time.Minute, |
| 36 | lifeTimeout: 30 * time.Minute, |
| 37 | } |
| 38 | |
| 39 | for _, opt := range opts { |
| 40 | err := opt(cfg) |
| 41 | if err != nil { |
| 42 | return nil, err |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | if cfg.dialer == nil { |
| 47 | cfg.dialer = &net.Dialer{ |
| 48 | KeepAlive: tcpKeepalive, |
| 49 | Timeout: cfg.connectTimeout, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | return cfg, nil |
| 54 | } |
| 55 | |
| 56 | // Option is used to configure a connection. |
| 57 | type Option func(*config) error |
| 58 | |
| 59 | // WithAppName sets the application name which gets sent to MongoDB when it |
| 60 | // first connects. |
| 61 | func WithAppName(fn func(string) string) Option { |
| 62 | return func(c *config) error { |
| 63 | c.appName = fn(c.appName) |
| 64 | return nil |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // WithCompressors sets the compressors that can be used for communication. |
| 69 | func WithCompressors(fn func([]compressor.Compressor) []compressor.Compressor) Option { |
| 70 | return func(c *config) error { |
| 71 | c.compressors = fn(c.compressors) |
| 72 | return nil |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // WithConnectTimeout configures the maximum amount of time a dial will wait for a |
| 77 | // connect to complete. The default is 30 seconds. |
| 78 | func WithConnectTimeout(fn func(time.Duration) time.Duration) Option { |
| 79 | return func(c *config) error { |
| 80 | c.connectTimeout = fn(c.connectTimeout) |
| 81 | return nil |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // WithDialer configures the Dialer to use when making a new connection to MongoDB. |
| 86 | func WithDialer(fn func(Dialer) Dialer) Option { |
| 87 | return func(c *config) error { |
| 88 | c.dialer = fn(c.dialer) |
| 89 | return nil |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | // WithHandshaker configures the Handshaker that wll be used to initialize newly |
| 94 | // dialed connections. |
| 95 | func WithHandshaker(fn func(Handshaker) Handshaker) Option { |
| 96 | return func(c *config) error { |
| 97 | c.handshaker = fn(c.handshaker) |
| 98 | return nil |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | // WithIdleTimeout configures the maximum idle time to allow for a connection. |
| 103 | func WithIdleTimeout(fn func(time.Duration) time.Duration) Option { |
| 104 | return func(c *config) error { |
| 105 | c.idleTimeout = fn(c.idleTimeout) |
| 106 | return nil |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | // WithLifeTimeout configures the maximum life of a connection. |
| 111 | func WithLifeTimeout(fn func(time.Duration) time.Duration) Option { |
| 112 | return func(c *config) error { |
| 113 | c.lifeTimeout = fn(c.lifeTimeout) |
| 114 | return nil |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // WithReadTimeout configures the maximum read time for a connection. |
| 119 | func WithReadTimeout(fn func(time.Duration) time.Duration) Option { |
| 120 | return func(c *config) error { |
| 121 | c.readTimeout = fn(c.readTimeout) |
| 122 | return nil |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // WithWriteTimeout configures the maximum write time for a connection. |
| 127 | func WithWriteTimeout(fn func(time.Duration) time.Duration) Option { |
| 128 | return func(c *config) error { |
| 129 | c.writeTimeout = fn(c.writeTimeout) |
| 130 | return nil |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // WithTLSConfig configures the TLS options for a connection. |
| 135 | func WithTLSConfig(fn func(*TLSConfig) *TLSConfig) Option { |
| 136 | return func(c *config) error { |
| 137 | c.tlsConfig = fn(c.tlsConfig) |
| 138 | return nil |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // WithMonitor configures a event for command monitoring. |
| 143 | func WithMonitor(fn func(*event.CommandMonitor) *event.CommandMonitor) Option { |
| 144 | return func(c *config) error { |
| 145 | c.cmdMonitor = fn(c.cmdMonitor) |
| 146 | return nil |
| 147 | } |
| 148 | } |