@@ -110,6 +110,7 @@ type (
110
110
111
111
reopenMutex sync.Mutex
112
112
refresh * poolRefresh
113
+ verbose sync2.AtomicBool
113
114
}
114
115
)
115
116
@@ -180,6 +181,10 @@ func NewResourcePool(factory Factory, capacity, maxCap int, idleTimeout time.Dur
180
181
return rp
181
182
}
182
183
184
+ func (rp * ResourcePool ) Verbose (v bool ) {
185
+ rp .verbose .Set (v )
186
+ }
187
+
183
188
func (rp * ResourcePool ) Name () string {
184
189
return "ResourcePool"
185
190
}
@@ -263,6 +268,7 @@ func (rp *ResourcePool) Get(ctx context.Context, setting *Setting) (resource Res
263
268
}
264
269
265
270
func (rp * ResourcePool ) get (ctx context.Context ) (resource Resource , err error ) {
271
+ rp .verboseInfo ("AYO getting a resource without settings" )
266
272
rp .getCount .Add (1 )
267
273
// Fetch
268
274
var wrapper resourceWrapper
@@ -274,19 +280,24 @@ func (rp *ResourcePool) get(ctx context.Context) (resource Resource, err error)
274
280
// check normal resources first
275
281
case wrapper , ok = <- rp .resources :
276
282
default :
283
+ rp .verboseInfo ("AYO nothing in rp.resources" )
277
284
select {
278
285
// then checking setting resources
279
286
case wrapper , ok = <- rp .settingResources :
280
287
default :
288
+ rp .verboseInfo ("AYO nothing in rp.settingResources" )
289
+ rp .verboseInfo ("AYO waiting for a resource" )
281
290
// now waiting
282
291
startTime := time .Now ()
283
292
select {
284
293
case wrapper , ok = <- rp .resources :
285
294
case wrapper , ok = <- rp .settingResources :
286
295
case <- ctx .Done ():
296
+ rp .verboseError ("AYO timed out waiting for a resource" )
287
297
return nil , ErrTimeout
288
298
}
289
299
rp .recordWait (startTime )
300
+ rp .verboseInfo ("AYO got a resource after waiting" )
290
301
}
291
302
}
292
303
if ! ok {
@@ -295,6 +306,7 @@ func (rp *ResourcePool) get(ctx context.Context) (resource Resource, err error)
295
306
296
307
// if the resource has setting applied, we will close it and return a new one
297
308
if wrapper .resource != nil && wrapper .resource .IsSettingApplied () {
309
+ rp .verboseInfo ("AYO got a resource with settings applied. Gonna reset it." )
298
310
rp .resetSettingCount .Add (1 )
299
311
err = wrapper .resource .ResetSetting (ctx )
300
312
if err != nil {
@@ -307,20 +319,38 @@ func (rp *ResourcePool) get(ctx context.Context) (resource Resource, err error)
307
319
308
320
// Unwrap
309
321
if wrapper .resource == nil {
322
+ rp .verboseInfo ("AYO gonna create a new resource." )
310
323
wrapper .resource , err = rp .factory (ctx )
311
324
if err != nil {
312
325
rp .resources <- resourceWrapper {}
326
+ rp .verboseError ("AYO error creating resource: %s" , err )
313
327
return nil , err
314
328
}
329
+ rp .verboseInfo ("AYO created a resource" )
315
330
rp .active .Add (1 )
316
331
}
332
+
333
+ rp .verboseInfo ("AYO got a resource now (from pool or just created)" )
334
+
317
335
if rp .available .Add (- 1 ) <= 0 {
318
336
rp .exhausted .Add (1 )
319
337
}
320
338
rp .inUse .Add (1 )
321
339
return wrapper .resource , err
322
340
}
323
341
342
+ func (rp * ResourcePool ) verboseInfo (format string , args ... interface {}) {
343
+ if rp .verbose .Get () {
344
+ log .Infof (format , args ... )
345
+ }
346
+ }
347
+
348
+ func (rp * ResourcePool ) verboseError (format string , args ... interface {}) {
349
+ if rp .verbose .Get () {
350
+ log .Infof (format , args ... )
351
+ }
352
+ }
353
+
324
354
func (rp * ResourcePool ) getWithSettings (ctx context.Context , setting * Setting ) (Resource , error ) {
325
355
rp .getSettingCount .Add (1 )
326
356
var wrapper resourceWrapper
0 commit comments