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 topology |
| 8 | |
| 9 | import ( |
| 10 | "time" |
| 11 | |
| 12 | "github.com/mongodb/mongo-go-driver/bson" |
| 13 | "github.com/mongodb/mongo-go-driver/bson/bsoncodec" |
| 14 | "github.com/mongodb/mongo-go-driver/x/mongo/driver/session" |
| 15 | "github.com/mongodb/mongo-go-driver/x/network/connection" |
| 16 | ) |
| 17 | |
| 18 | var defaultRegistry = bson.NewRegistryBuilder().Build() |
| 19 | |
| 20 | type serverConfig struct { |
| 21 | clock *session.ClusterClock |
| 22 | compressionOpts []string |
| 23 | connectionOpts []connection.Option |
| 24 | appname string |
| 25 | heartbeatInterval time.Duration |
| 26 | heartbeatTimeout time.Duration |
| 27 | maxConns uint16 |
| 28 | maxIdleConns uint16 |
| 29 | registry *bsoncodec.Registry |
| 30 | } |
| 31 | |
| 32 | func newServerConfig(opts ...ServerOption) (*serverConfig, error) { |
| 33 | cfg := &serverConfig{ |
| 34 | heartbeatInterval: 10 * time.Second, |
| 35 | heartbeatTimeout: 10 * time.Second, |
| 36 | maxConns: 100, |
| 37 | maxIdleConns: 100, |
| 38 | registry: defaultRegistry, |
| 39 | } |
| 40 | |
| 41 | for _, opt := range opts { |
| 42 | err := opt(cfg) |
| 43 | if err != nil { |
| 44 | return nil, err |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | return cfg, nil |
| 49 | } |
| 50 | |
| 51 | // ServerOption configures a server. |
| 52 | type ServerOption func(*serverConfig) error |
| 53 | |
| 54 | // WithConnectionOptions configures the server's connections. |
| 55 | func WithConnectionOptions(fn func(...connection.Option) []connection.Option) ServerOption { |
| 56 | return func(cfg *serverConfig) error { |
| 57 | cfg.connectionOpts = fn(cfg.connectionOpts...) |
| 58 | return nil |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // WithCompressionOptions configures the server's compressors. |
| 63 | func WithCompressionOptions(fn func(...string) []string) ServerOption { |
| 64 | return func(cfg *serverConfig) error { |
| 65 | cfg.compressionOpts = fn(cfg.compressionOpts...) |
| 66 | return nil |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | // WithHeartbeatInterval configures a server's heartbeat interval. |
| 71 | func WithHeartbeatInterval(fn func(time.Duration) time.Duration) ServerOption { |
| 72 | return func(cfg *serverConfig) error { |
| 73 | cfg.heartbeatInterval = fn(cfg.heartbeatInterval) |
| 74 | return nil |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // WithHeartbeatTimeout configures how long to wait for a heartbeat socket to |
| 79 | // connection. |
| 80 | func WithHeartbeatTimeout(fn func(time.Duration) time.Duration) ServerOption { |
| 81 | return func(cfg *serverConfig) error { |
| 82 | cfg.heartbeatTimeout = fn(cfg.heartbeatTimeout) |
| 83 | return nil |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | // WithMaxConnections configures the maximum number of connections to allow for |
| 88 | // a given server. If max is 0, then there is no upper limit to the number of |
| 89 | // connections. |
| 90 | func WithMaxConnections(fn func(uint16) uint16) ServerOption { |
| 91 | return func(cfg *serverConfig) error { |
| 92 | cfg.maxConns = fn(cfg.maxConns) |
| 93 | return nil |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | // WithMaxIdleConnections configures the maximum number of idle connections |
| 98 | // allowed for the server. |
| 99 | func WithMaxIdleConnections(fn func(uint16) uint16) ServerOption { |
| 100 | return func(cfg *serverConfig) error { |
| 101 | cfg.maxIdleConns = fn(cfg.maxIdleConns) |
| 102 | return nil |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // WithClock configures the ClusterClock for the server to use. |
| 107 | func WithClock(fn func(clock *session.ClusterClock) *session.ClusterClock) ServerOption { |
| 108 | return func(cfg *serverConfig) error { |
| 109 | cfg.clock = fn(cfg.clock) |
| 110 | return nil |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | // WithRegistry configures the registry for the server to use when creating |
| 115 | // cursors. |
| 116 | func WithRegistry(fn func(*bsoncodec.Registry) *bsoncodec.Registry) ServerOption { |
| 117 | return func(cfg *serverConfig) error { |
| 118 | cfg.registry = fn(cfg.registry) |
| 119 | return nil |
| 120 | } |
| 121 | } |