1 /* 2 * Copyright (C) 2019, HuntLabs 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 module hunt.database.base.PoolOptions; 19 20 import hunt.Exceptions; 21 22 import core.time; 23 24 /** 25 * The options for configuring a connection pool. 26 * 27 * @author <a href="mailto:julien@julienviet.com">Julien Viet</a> 28 */ 29 class PoolOptions { 30 31 /** 32 * The default maximum number of connections a client will pool = 4 33 */ 34 enum int DEFAULT_MAX_SIZE = 4; 35 36 /** 37 * Default max wait queue size = -1 (unbounded) 38 */ 39 enum int DEFAULT_MAX_WAIT_QUEUE_SIZE = -1; 40 41 private int maxSize = DEFAULT_MAX_SIZE; 42 private int maxWaitQueueSize = DEFAULT_MAX_WAIT_QUEUE_SIZE; 43 44 private Duration _awaittingTimeout = 10.seconds; 45 private size_t _retry = 5; 46 47 this() { 48 } 49 50 this(PoolOptions other) { 51 maxSize = other.maxSize; 52 maxWaitQueueSize = other.maxWaitQueueSize; 53 awaittingTimeout = other.awaittingTimeout; 54 } 55 56 Duration awaittingTimeout() { 57 return _awaittingTimeout; 58 } 59 60 PoolOptions awaittingTimeout(Duration value) { 61 _awaittingTimeout = value; 62 return this; 63 } 64 65 /** 66 * @return the maximum pool size 67 */ 68 int getMaxSize() { 69 return maxSize; 70 } 71 72 /** 73 * Set the maximum pool size 74 * 75 * @param maxSize the maximum pool size 76 * @return a reference to this, so the API can be used fluently 77 */ 78 PoolOptions setMaxSize(int maxSize) { 79 if (maxSize < 0) { 80 throw new IllegalArgumentException("Max size cannot be negative"); 81 } 82 this.maxSize = maxSize; 83 return this; 84 } 85 86 /** 87 * @return the maximum wait queue size 88 */ 89 int getMaxWaitQueueSize() { 90 return maxWaitQueueSize; 91 } 92 93 /** 94 * Set the maximum connection request allowed in the wait queue, any requests beyond the max size will result in 95 * an failure. If the value is set to a negative number then the queue will be unbounded. 96 * 97 * @param maxWaitQueueSize the maximum number of waiting requests 98 * @return a reference to this, so the API can be used fluently 99 */ 100 PoolOptions setMaxWaitQueueSize(int maxWaitQueueSize) { 101 this.maxWaitQueueSize = maxWaitQueueSize; 102 return this; 103 } 104 105 size_t retry() { 106 return _retry; 107 } 108 109 PoolOptions retry(size_t value) { 110 _retry = value; 111 return this; 112 } 113 114 // JsonObject toJson() { 115 // JsonObject json = new JsonObject(); 116 // PoolOptionsConverter.toJson(this, json); 117 // return json; 118 // } 119 120 override bool opEquals(Object o) { 121 if (this is o) 122 return true; 123 if (!super.opEquals(o)) 124 return false; 125 126 PoolOptions that = cast(PoolOptions) o; 127 if (that is null) 128 return false; 129 130 if (maxSize != that.maxSize) 131 return false; 132 133 return true; 134 } 135 136 override size_t toHash() @trusted nothrow { 137 size_t result = super.toHash(); 138 result = 31 * result + maxSize; 139 return result; 140 } 141 }