forked from neo4j/neo4j-go-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
130 lines (119 loc) · 3.98 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2002-2019 "Neo4j,"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package neo4j
import (
"math"
"time"
)
// A Config contains options that can be used to customize certain
// aspects of the driver
type Config struct {
// Whether to turn on/off TLS encryption.
//
// default: true
Encrypted bool
// Sets how the driver establishes trust with the Neo4j instance
// it is connected to.
//
// default: TrustAny(false)
TrustStrategy TrustStrategy
// Logging target the driver will send its log outputs
//
// default: No Op Logger
Log Logging
// Resolver that would be used to resolve initial router address. This may
// be useful if you want to provide more than one URL for initial router.
// If not specified, the provided bolt+routing URL is used as the initial
// router.
//
// default: nil
AddressResolver ServerAddressResolver
// Maximum amount of time a retriable operation would continue retrying. It
// cannot be specified as a negative value.
//
// default: 30 * time.Second
MaxTransactionRetryTime time.Duration
// Maximum number of connections per URL to allow on this driver. It
// cannot be specified as 0 and negative values are interpreted as
// math.MaxInt32.
//
// default: 100
MaxConnectionPoolSize int
// Maximum connection life time on pooled connections. Values less than
// or equal to 0 disables the lifetime check.
//
// default: 1 * time.Hour
MaxConnectionLifetime time.Duration
// Maximum amount of time to either acquire an idle connection from the pool
// or create a new connection (when the pool is not full). Negative values
// result in an infinite wait time where 0 value results in no timeout which
// results in immediate failure when there are no available connections.
//
// default: 1 * time.Minute
ConnectionAcquisitionTimeout time.Duration
// Connect timeout that will be set on underlying sockets. Values less than
// or equal to 0 results in no timeout being applied.
//
// default: 5 * time.Second
SocketConnectTimeout time.Duration
// Whether to enable TCP keep alive on underlying sockets.
//
// default: true
SocketKeepalive bool
}
func defaultConfig() *Config {
return &Config{
Encrypted: true,
TrustStrategy: TrustAny(false),
Log: NoOpLogger(),
AddressResolver: nil,
MaxTransactionRetryTime: 30 * time.Second,
MaxConnectionPoolSize: 100,
MaxConnectionLifetime: 1 * time.Hour,
ConnectionAcquisitionTimeout: 1 * time.Minute,
SocketConnectTimeout: 5 * time.Second,
SocketKeepalive: true,
}
}
func validateAndNormaliseConfig(config *Config) error {
// Max Transaction Retry Time
if config.MaxTransactionRetryTime < 0 {
return newDriverError("maximum transaction retry time cannot be smaller than 0, but was %v", config.MaxTransactionRetryTime)
}
// Max Connection Pool Size
if config.MaxConnectionPoolSize == 0 {
return newDriverError("maximum connection pool size cannot be 0")
}
if config.MaxConnectionPoolSize < 0 {
config.MaxConnectionPoolSize = math.MaxInt32
}
// Max Connection Lifetime
if config.MaxConnectionLifetime < 0 {
config.MaxConnectionLifetime = 0
}
// Connection Acquisition Timeout
if config.ConnectionAcquisitionTimeout < 0 {
config.ConnectionAcquisitionTimeout = -1
}
// Socket Connect Timeout
if config.SocketConnectTimeout < 0 {
config.SocketConnectTimeout = 0
}
return nil
}