-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
BaseConfig.java
694 lines (594 loc) · 18.4 KB
/
BaseConfig.java
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
/**
* Copyright (c) 2013-2024 Nikita Koksharov
*
* 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 org.redisson.config;
import org.redisson.api.NameMapper;
import org.redisson.client.DefaultCredentialsResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.TrustManagerFactory;
import java.net.URL;
/**
*
* @author Nikita Koksharov
*
* @param <T> config type
*/
public class BaseConfig<T extends BaseConfig<T>> {
protected static final Logger log = LoggerFactory.getLogger("config");
/**
* If pooled connection not used for a <code>timeout</code> time
* and current connections amount bigger than minimum idle connections pool size,
* then it will closed and removed from pool.
* Value in milliseconds.
*
*/
private int idleConnectionTimeout = 10000;
/**
* Timeout during connecting to any Redis server.
* Value in milliseconds.
*
*/
private int connectTimeout = 10000;
/**
* Redis server response timeout. Starts to countdown when Redis command was succesfully sent.
* Value in milliseconds.
*
*/
private int timeout = 3000;
private int subscriptionTimeout = 7500;
private int retryAttempts = 3;
private int retryInterval = 1500;
/**
* Password for Redis authentication. Should be null if not needed
*/
private String password;
private String username;
private CredentialsResolver credentialsResolver = new DefaultCredentialsResolver();
/**
* Subscriptions per Redis connection limit
*/
private int subscriptionsPerConnection = 5;
/**
* Name of client connection
*/
private String clientName;
private boolean sslEnableEndpointIdentification = true;
private String sslKeystoreType;
private SslProvider sslProvider = SslProvider.JDK;
private URL sslTruststore;
private String sslTruststorePassword;
private URL sslKeystore;
private String sslKeystorePassword;
private String[] sslProtocols;
private String[] sslCiphers;
private TrustManagerFactory sslTrustManagerFactory;
private KeyManagerFactory sslKeyManagerFactory;
private int pingConnectionInterval = 30000;
private boolean keepAlive;
private int tcpKeepAliveCount;
private int tcpKeepAliveIdle;
private int tcpKeepAliveInterval;
private int tcpUserTimeout;
private boolean tcpNoDelay = true;
private NameMapper nameMapper = NameMapper.direct();
private CommandMapper commandMapper = CommandMapper.direct();
BaseConfig() {
}
BaseConfig(T config) {
setPassword(config.getPassword());
setUsername(config.getUsername());
setSubscriptionsPerConnection(config.getSubscriptionsPerConnection());
setRetryAttempts(config.getRetryAttempts());
setRetryInterval(config.getRetryInterval());
setTimeout(config.getTimeout());
setClientName(config.getClientName());
setConnectTimeout(config.getConnectTimeout());
setIdleConnectionTimeout(config.getIdleConnectionTimeout());
setSslEnableEndpointIdentification(config.isSslEnableEndpointIdentification());
setSslProvider(config.getSslProvider());
setSslTruststore(config.getSslTruststore());
setSslTruststorePassword(config.getSslTruststorePassword());
setSslKeystoreType(config.getSslKeystoreType());
setSslKeystore(config.getSslKeystore());
setSslKeystorePassword(config.getSslKeystorePassword());
setSslProtocols(config.getSslProtocols());
setSslCiphers(config.getSslCiphers());
setSslKeyManagerFactory(config.getSslKeyManagerFactory());
setSslTrustManagerFactory(config.getSslTrustManagerFactory());
setPingConnectionInterval(config.getPingConnectionInterval());
setKeepAlive(config.isKeepAlive());
setTcpKeepAliveCount(config.getTcpKeepAliveCount());
setTcpKeepAliveIdle(config.getTcpKeepAliveIdle());
setTcpKeepAliveInterval(config.getTcpKeepAliveInterval());
setTcpUserTimeout(config.getTcpUserTimeout());
setTcpNoDelay(config.isTcpNoDelay());
setNameMapper(config.getNameMapper());
setCredentialsResolver(config.getCredentialsResolver());
setCommandMapper(config.getCommandMapper());
setSubscriptionTimeout(config.getSubscriptionTimeout());
}
/**
* Subscriptions per Redis connection limit
* <p>
* Default is <code>5</code>
*
* @param subscriptionsPerConnection amount
* @return config
*/
public T setSubscriptionsPerConnection(int subscriptionsPerConnection) {
this.subscriptionsPerConnection = subscriptionsPerConnection;
return (T) this;
}
public int getSubscriptionsPerConnection() {
return subscriptionsPerConnection;
}
/**
* Password for Redis authentication. Should be null if not needed.
* <p>
* Default is <code>null</code>
*
* @param password for connection
* @return config
*/
public T setPassword(String password) {
this.password = password;
return (T) this;
}
public String getPassword() {
return password;
}
/**
* Username for Redis authentication. Should be null if not needed
* <p>
* Default is <code>null</code>
* <p>
* Requires Redis 6.0+
*
* @param username for connection
* @return config
*/
public T setUsername(String username) {
this.username = username;
return (T) this;
}
public String getUsername() {
return username;
}
/**
* Error will be thrown if Redis command can't be sent to Redis server after <code>retryAttempts</code>.
* But if it sent successfully then <code>timeout</code> will be started.
* <p>
* Default is <code>3</code> attempts
*
* @see #timeout
* @param retryAttempts retry attempts
* @return config
*/
public T setRetryAttempts(int retryAttempts) {
this.retryAttempts = retryAttempts;
return (T) this;
}
public int getRetryAttempts() {
return retryAttempts;
}
/**
* Defines time interval for another one attempt send Redis command
* if it hasn't been sent already.
* <p>
* Default is <code>1500</code> milliseconds
*
* @param retryInterval - time in milliseconds
* @return config
*/
public T setRetryInterval(int retryInterval) {
this.retryInterval = retryInterval;
return (T) this;
}
public int getRetryInterval() {
return retryInterval;
}
/**
* Redis server response timeout. Starts to countdown when Redis command has been successfully sent.
* <p>
* Default is <code>3000</code> milliseconds
*
* @param timeout in milliseconds
* @return config
*/
public T setTimeout(int timeout) {
this.timeout = timeout;
return (T) this;
}
public int getTimeout() {
return timeout;
}
public int getSubscriptionTimeout() {
return subscriptionTimeout;
}
/**
* Defines subscription timeout applied per channel subscription.
* <p>
* Default is <code>7500</code> milliseconds.
*
* @param subscriptionTimeout timeout in milliseconds
* @return config
*/
public T setSubscriptionTimeout(int subscriptionTimeout) {
this.subscriptionTimeout = subscriptionTimeout;
return (T) this;
}
/**
* Setup connection name during connection init
* via CLIENT SETNAME command
* <p>
* Default is <code>null</code>
*
* @param clientName name of client
* @return config
*/
public T setClientName(String clientName) {
this.clientName = clientName;
return (T) this;
}
public String getClientName() {
return clientName;
}
/**
* Timeout during connecting to any Redis server.
* <p>
* Default is <code>10000</code> milliseconds.
*
* @param connectTimeout timeout in milliseconds
* @return config
*/
public T setConnectTimeout(int connectTimeout) {
this.connectTimeout = connectTimeout;
return (T) this;
}
public int getConnectTimeout() {
return connectTimeout;
}
/**
* If pooled connection not used for a <code>timeout</code> time
* and current connections amount bigger than minimum idle connections pool size,
* then it will closed and removed from pool.
* <p>
* Default is <code>10000</code> milliseconds.
*
* @param idleConnectionTimeout timeout in milliseconds
* @return config
*/
public T setIdleConnectionTimeout(int idleConnectionTimeout) {
this.idleConnectionTimeout = idleConnectionTimeout;
return (T) this;
}
public int getIdleConnectionTimeout() {
return idleConnectionTimeout;
}
public boolean isSslEnableEndpointIdentification() {
return sslEnableEndpointIdentification;
}
/**
* Enables SSL endpoint identification.
* <p>
* Default is <code>true</code>
*
* @param sslEnableEndpointIdentification boolean value
* @return config
*/
public T setSslEnableEndpointIdentification(boolean sslEnableEndpointIdentification) {
this.sslEnableEndpointIdentification = sslEnableEndpointIdentification;
return (T) this;
}
public SslProvider getSslProvider() {
return sslProvider;
}
/**
* Defines SSL provider used to handle SSL connections.
* <p>
* Default is <code>JDK</code>
*
* @param sslProvider ssl provider
* @return config
*/
public T setSslProvider(SslProvider sslProvider) {
this.sslProvider = sslProvider;
return (T) this;
}
public URL getSslTruststore() {
return sslTruststore;
}
/**
* Defines path to SSL truststore
* <p>
* Default is <code>null</code>
*
* @param sslTruststore truststore path
* @return config
*/
public T setSslTruststore(URL sslTruststore) {
this.sslTruststore = sslTruststore;
return (T) this;
}
public String getSslTruststorePassword() {
return sslTruststorePassword;
}
/**
* Defines password for SSL truststore.
* SSL truststore is read on each new connection creation and can be dynamically reloaded.
* <p>
* Default is <code>null</code>
*
* @param sslTruststorePassword - password
* @return config
*/
public T setSslTruststorePassword(String sslTruststorePassword) {
this.sslTruststorePassword = sslTruststorePassword;
return (T) this;
}
public URL getSslKeystore() {
return sslKeystore;
}
/**
* Defines path to SSL keystore.
* SSL keystore is read on each new connection creation and can be dynamically reloaded.
* <p>
* Default is <code>null</code>
*
* @param sslKeystore path to keystore
* @return config
*/
public T setSslKeystore(URL sslKeystore) {
this.sslKeystore = sslKeystore;
return (T) this;
}
public String getSslKeystorePassword() {
return sslKeystorePassword;
}
/**
* Defines password for SSL keystore
* <p>
* Default is <code>null</code>
*
* @param sslKeystorePassword password
* @return config
*/
public T setSslKeystorePassword(String sslKeystorePassword) {
this.sslKeystorePassword = sslKeystorePassword;
return (T) this;
}
public String[] getSslProtocols() {
return sslProtocols;
}
/**
* Defines SSL protocols.
* Example values: TLSv1.3, TLSv1.2, TLSv1.1, TLSv1
* <p>
* Default is <code>null</code>
*
* @param sslProtocols protocols
* @return config
*/
public T setSslProtocols(String[] sslProtocols) {
this.sslProtocols = sslProtocols;
return (T) this;
}
public int getPingConnectionInterval() {
return pingConnectionInterval;
}
/**
* Defines PING command sending interval per connection to Redis.
* <code>0</code> means disable.
* <p>
* Default is <code>30000</code>
*
* @param pingConnectionInterval time in milliseconds
* @return config
*/
public T setPingConnectionInterval(int pingConnectionInterval) {
this.pingConnectionInterval = pingConnectionInterval;
return (T) this;
}
public boolean isKeepAlive() {
return keepAlive;
}
/**
* Enables TCP keepAlive for connection
* <p>
* Default is <code>false</code>
*
* @param keepAlive boolean value
* @return config
*/
public T setKeepAlive(boolean keepAlive) {
this.keepAlive = keepAlive;
return (T) this;
}
public int getTcpKeepAliveCount() {
return tcpKeepAliveCount;
}
/**
* Defines the maximum number of keepalive probes
* TCP should send before dropping the connection.
*
* @param tcpKeepAliveCount maximum number of keepalive probes
* @return config
*/
public T setTcpKeepAliveCount(int tcpKeepAliveCount) {
this.tcpKeepAliveCount = tcpKeepAliveCount;
return (T) this;
}
public int getTcpKeepAliveIdle() {
return tcpKeepAliveIdle;
}
/**
* Defines the time in seconds the connection needs to remain idle
* before TCP starts sending keepalive probes,
*
* @param tcpKeepAliveIdle time in seconds
* @return config
*/
public T setTcpKeepAliveIdle(int tcpKeepAliveIdle) {
this.tcpKeepAliveIdle = tcpKeepAliveIdle;
return (T) this;
}
public int getTcpKeepAliveInterval() {
return tcpKeepAliveInterval;
}
/**
* Defines the time in seconds between individual keepalive probes.
*
* @param tcpKeepAliveInterval time in seconds
* @return config
*/
public T setTcpKeepAliveInterval(int tcpKeepAliveInterval) {
this.tcpKeepAliveInterval = tcpKeepAliveInterval;
return (T) this;
}
public int getTcpUserTimeout() {
return tcpUserTimeout;
}
/**
* Defines the maximum amount of time in milliseconds that transmitted data may
* remain unacknowledged, or buffered data may remain untransmitted
* (due to zero window size) before TCP will forcibly close the connection.
*
* @param tcpUserTimeout time in milliseconds
* @return config
*/
public T setTcpUserTimeout(int tcpUserTimeout) {
this.tcpUserTimeout = tcpUserTimeout;
return (T) this;
}
public boolean isTcpNoDelay() {
return tcpNoDelay;
}
/**
* Enables TCP noDelay for connection
* <p>
* Default is <code>true</code>
*
* @param tcpNoDelay boolean value
* @return config
*/
public T setTcpNoDelay(boolean tcpNoDelay) {
this.tcpNoDelay = tcpNoDelay;
return (T) this;
}
public NameMapper getNameMapper() {
return nameMapper;
}
/**
* Defines Name mapper which maps Redisson object name.
* Applied to all Redisson objects.
*
* @param nameMapper name mapper object
* @return config
*/
public T setNameMapper(NameMapper nameMapper) {
this.nameMapper = nameMapper;
return (T) this;
}
public CredentialsResolver getCredentialsResolver() {
return credentialsResolver;
}
/**
* Defines Credentials resolver which is invoked during connection for Redis server authentication.
* It makes possible to specify dynamically changing Redis credentials.
*
* @param credentialsResolver Credentials resolver object
* @return config
*/
public T setCredentialsResolver(CredentialsResolver credentialsResolver) {
this.credentialsResolver = credentialsResolver;
return (T) this;
}
public String getSslKeystoreType() {
return sslKeystoreType;
}
/**
* Defines SSL keystore type.
* <p>
* Default is <code>null</code>
*
* @param sslKeystoreType keystore type
* @return config
*/
public T setSslKeystoreType(String sslKeystoreType) {
this.sslKeystoreType = sslKeystoreType;
return (T) this;
}
public String[] getSslCiphers() {
return sslCiphers;
}
/**
* Defines SSL ciphers.
* <p>
* Default is <code>null</code>
*
* @param sslCiphers ciphers
* @return config
*/
public BaseConfig<T> setSslCiphers(String[] sslCiphers) {
this.sslCiphers = sslCiphers;
return this;
}
public TrustManagerFactory getSslTrustManagerFactory() {
return sslTrustManagerFactory;
}
/**
* Defines SSL TrustManagerFactory.
* <p>
* Default is <code>null</code>
*
* @param trustManagerFactory trust manager value
* @return config
*/
public BaseConfig<T> setSslTrustManagerFactory(TrustManagerFactory trustManagerFactory) {
this.sslTrustManagerFactory = trustManagerFactory;
return this;
}
public KeyManagerFactory getSslKeyManagerFactory() {
return sslKeyManagerFactory;
}
/**
* Defines SSL KeyManagerFactory.
* <p>
* Default is <code>null</code>
*
* @param keyManagerFactory key manager value
* @return config
*/
public BaseConfig<T> setSslKeyManagerFactory(KeyManagerFactory keyManagerFactory) {
this.sslKeyManagerFactory = keyManagerFactory;
return this;
}
public CommandMapper getCommandMapper() {
return commandMapper;
}
/**
* Defines Command mapper which maps Redis command name.
* Applied to all Redis commands.
*
* @param commandMapper Redis command name mapper object
* @return config
*/
public BaseConfig<T> setCommandMapper(CommandMapper commandMapper) {
this.commandMapper = commandMapper;
return this;
}
}