1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 module hunt.pool.impl.AbandonedConfig;
19 
20 import hunt.pool.TrackedUse;
21 import hunt.pool.UsageTracking;
22 import hunt.util.StringBuilder;
23 
24 // import java.io.PrintWriter;
25 
26 /**
27  * Configuration settings for abandoned object removal.
28  *
29  */
30 class AbandonedConfig {
31 
32     /**
33      * Whether or not borrowObject performs abandoned object removal.
34      */
35     private bool removeAbandonedOnBorrow = false;
36 
37     /**
38      * <p>Flag to remove abandoned objects if they exceed the
39      * removeAbandonedTimeout when borrowObject is invoked.</p>
40      *
41      * <p>The default value is false.</p>
42      *
43      * <p>If set to true, abandoned objects are removed by borrowObject if
44      * there are fewer than 2 idle objects available in the pool and
45      * <code>getNumActive() &gt; getMaxTotal() - 3</code></p>
46      *
47      * @return true if abandoned objects are to be removed by borrowObject
48      */
49     bool getRemoveAbandonedOnBorrow() {
50         return this.removeAbandonedOnBorrow;
51     }
52 
53     /**
54      * <p>Flag to remove abandoned objects if they exceed the
55      * removeAbandonedTimeout when borrowObject is invoked.</p>
56      *
57      * @param removeAbandonedOnBorrow true means abandoned objects will be
58      *   removed by borrowObject
59      * @see #getRemoveAbandonedOnBorrow()
60      */
61     void setRemoveAbandonedOnBorrow(bool removeAbandonedOnBorrow) {
62         this.removeAbandonedOnBorrow = removeAbandonedOnBorrow;
63     }
64 
65     /**
66      * Whether or not pool maintenance (evictor) performs abandoned object
67      * removal.
68      */
69     private bool removeAbandonedOnMaintenance = false;
70 
71     /**
72      * <p>Flag to remove abandoned objects if they exceed the
73      * removeAbandonedTimeout when pool maintenance (the "evictor")
74      * runs.</p>
75      *
76      * <p>The default value is false.</p>
77      *
78      * <p>If set to true, abandoned objects are removed by the pool
79      * maintenance thread when it runs.  This setting has no effect
80      * unless maintenance is enabled by setting
81      *{@link GenericObjectPool#getTimeBetweenEvictionRunsMillis() timeBetweenEvictionRunsMillis}
82      * to a positive number.</p>
83      *
84      * @return true if abandoned objects are to be removed by the evictor
85      */
86     bool getRemoveAbandonedOnMaintenance() {
87         return this.removeAbandonedOnMaintenance;
88     }
89 
90     /**
91      * <p>Flag to remove abandoned objects if they exceed the
92      * removeAbandonedTimeout when pool maintenance runs.</p>
93      *
94      * @param removeAbandonedOnMaintenance true means abandoned objects will be
95      *   removed by pool maintenance
96      * @see #getRemoveAbandonedOnMaintenance
97      */
98     void setRemoveAbandonedOnMaintenance(bool removeAbandonedOnMaintenance) {
99         this.removeAbandonedOnMaintenance = removeAbandonedOnMaintenance;
100     }
101 
102     /**
103      * Timeout in seconds before an abandoned object can be removed.
104      */
105     private int removeAbandonedTimeout = 300;
106 
107     /**
108      * <p>Timeout in seconds before an abandoned object can be removed.</p>
109      *
110      * <p>The time of most recent use of an object is the maximum (latest) of
111      * {@link TrackedUse#getLastUsed()} (if this class of the object implements
112      * TrackedUse) and the time when the object was borrowed from the pool.</p>
113      *
114      * <p>The default value is 300 seconds.</p>
115      *
116      * @return the abandoned object timeout in seconds
117      */
118     int getRemoveAbandonedTimeout() {
119         return this.removeAbandonedTimeout;
120     }
121 
122     /**
123      * <p>Sets the timeout in seconds before an abandoned object can be
124      * removed</p>
125      *
126      * <p>Setting this property has no effect if
127      * {@link #getRemoveAbandonedOnBorrow() removeAbandonedOnBorrow} and
128      * {@link #getRemoveAbandonedOnMaintenance() removeAbandonedOnMaintenance}
129      * are both false.</p>
130      *
131      * @param removeAbandonedTimeout new abandoned timeout in seconds
132      * @see #getRemoveAbandonedTimeout()
133      */
134     void setRemoveAbandonedTimeout(int removeAbandonedTimeout) {
135         this.removeAbandonedTimeout = removeAbandonedTimeout;
136     }
137 
138     /**
139      * Determines whether or not to log stack traces for application code
140      * which abandoned an object.
141      */
142     private bool logAbandoned = false;
143 
144     /**
145      * Flag to log stack traces for application code which abandoned
146      * an object.
147      *
148      * Defaults to false.
149      * Logging of abandoned objects adds overhead for every object created
150      * because a stack trace has to be generated.
151      *
152      * @return bool true if stack trace logging is turned on for abandoned
153      * objects
154      *
155      */
156     bool getLogAbandoned() {
157         return this.logAbandoned;
158     }
159 
160     /**
161      * Sets the flag to log stack traces for application code which abandoned
162      * an object.
163      *
164      * @param logAbandoned true turns on abandoned stack trace logging
165      * @see #getLogAbandoned()
166      *
167      */
168     void setLogAbandoned(bool logAbandoned) {
169         this.logAbandoned = logAbandoned;
170     }
171 
172     /**
173      * Determines whether or not to log full stack traces when logAbandoned is true.
174      * If disabled, then a faster method for logging stack traces with only class data
175      * may be used if possible.
176      *
177      */
178     private bool requireFullStackTrace = true;
179 
180     /**
181      * Indicates if full stack traces are required when {@link #getLogAbandoned() logAbandoned}
182      * is true. Defaults to true. Logging of abandoned objects requiring a full stack trace will
183      * generate an entire stack trace to generate for every object created. If this is disabled,
184      * a faster but less informative stack walking mechanism may be used if available.
185      *
186      * @return true if full stack traces are required for logging abandoned connections, or false
187      * if abbreviated stack traces are acceptable
188      * @see CallStack
189      */
190     bool getRequireFullStackTrace() {
191         return requireFullStackTrace;
192     }
193 
194     /**
195      * Sets the flag to require full stack traces for logging abandoned connections when enabled.
196      *
197      * @param requireFullStackTrace indicates whether or not full stack traces are required in
198      *                              abandoned connection logs
199      * @see CallStack
200      * @see #getRequireFullStackTrace()
201      */
202     void setRequireFullStackTrace(bool requireFullStackTrace) {
203         this.requireFullStackTrace = requireFullStackTrace;
204     }
205 
206     /**
207      * PrintWriter to use to log information on abandoned objects.
208      * Use of default system encoding is deliberate.
209      */
210     // private PrintWriter logWriter = new PrintWriter(System.out);
211 
212     /**
213      * Returns the log writer being used by this configuration to log
214      * information on abandoned objects. If not set, a PrintWriter based on
215      * System.out with the system default encoding is used.
216      *
217      * @return log writer in use
218      */
219     // PrintWriter getLogWriter() {
220     //     return logWriter;
221     // }
222 
223     /**
224      * Sets the log writer to be used by this configuration to log
225      * information on abandoned objects.
226      *
227      * @param logWriter The new log writer
228      */
229     // void setLogWriter(PrintWriter logWriter) {
230     //     this.logWriter = logWriter;
231     // }
232 
233     /**
234      * If the pool implements {@link UsageTracking}, should the pool record a
235      * stack trace every time a method is called on a pooled object and retain
236      * the most recent stack trace to aid debugging of abandoned objects?
237      */
238     private bool useUsageTracking = false;
239 
240     /**
241      * If the pool implements {@link UsageTracking}, should the pool record a
242      * stack trace every time a method is called on a pooled object and retain
243      * the most recent stack trace to aid debugging of abandoned objects?
244      *
245      * @return <code>true</code> if usage tracking is enabled
246      */
247     bool getUseUsageTracking() {
248         return useUsageTracking;
249     }
250 
251     /**
252      * If the pool implements {@link UsageTracking}, configure whether the pool
253      * should record a stack trace every time a method is called on a pooled
254      * object and retain the most recent stack trace to aid debugging of
255      * abandoned objects.
256      *
257      * @param   useUsageTracking    A value of <code>true</code> will enable
258      *                              the recording of a stack trace on every use
259      *                              of a pooled object
260      */
261     void setUseUsageTracking(bool useUsageTracking) {
262         this.useUsageTracking = useUsageTracking;
263     }
264 
265     /**
266      */
267     override
268     string toString() {
269         StringBuilder builder = new StringBuilder();
270         builder.append("AbandonedConfig [removeAbandonedOnBorrow=");
271         builder.append(removeAbandonedOnBorrow);
272         builder.append(", removeAbandonedOnMaintenance=");
273         builder.append(removeAbandonedOnMaintenance);
274         builder.append(", removeAbandonedTimeout=");
275         builder.append(removeAbandonedTimeout);
276         builder.append(", logAbandoned=");
277         builder.append(logAbandoned);
278         // builder.append(", logWriter=");
279         // builder.append(logWriter);
280         builder.append(", useUsageTracking=");
281         builder.append(useUsageTracking);
282         builder.append("]");
283         return builder.toString();
284     }
285 }