@@ -77,7 +77,7 @@ func New[PT Item[T], T any](
7777) * Pool [PT , T ] {
7878 p := & Pool [PT , T ]{
7979 config : Config [PT , T ]{
80- trace : defaultTrace ,
80+ trace : & Trace {} ,
8181 limit : DefaultLimit ,
8282 createItem : defaultCreateItem [T , PT ],
8383 },
@@ -90,16 +90,16 @@ func New[PT Item[T], T any](
9090 }
9191 }
9292
93- onDone := p .config .trace .OnNew ( & NewStartInfo {
94- Context : & ctx ,
95- Call : stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.New" ),
96- } )
97-
98- defer func () {
99- onDone (& NewDoneInfo {
100- Limit : p . config . limit ,
101- })
102- }()
93+ if onNew := p .config .trace .OnNew ; onNew != nil {
94+ onDone := onNew ( & ctx ,
95+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.New" ),
96+ )
97+ if onDone != nil {
98+ defer func () {
99+ onDone (p . config . limit )
100+ }()
101+ }
102+ }
103103
104104 p .createItem = makeCreateItemFunc (p .config , p .done , func (item PT ) error {
105105 return xsync .WithLock (& p .mu , func () error {
@@ -195,14 +195,21 @@ func makeCreateItemFunc[PT Item[T], T any]( //nolint:funlen
195195 }
196196}
197197
198+ func (p * Pool [PT , T ]) stats () Stats {
199+ return Stats {
200+ Limit : p .config .limit ,
201+ Idle : len (p .idle ),
202+ Wait : 0 ,
203+ CreateInProgress : 0 ,
204+ }
205+ }
206+
198207func (p * Pool [PT , T ]) onChangeStats () {
199- p . mu . RLock ()
200- info := ChangeInfo {
201- Limit : p .config . limit ,
202- Idle : len ( p . idle ),
208+ if onChange := p . config . trace . OnChange ; onChange != nil {
209+ onChange ( xsync . WithRLock ( & p . mu , func () Stats {
210+ return p .stats ()
211+ }))
203212 }
204- p .mu .RUnlock ()
205- p .config .trace .OnChange (info )
206213}
207214
208215func (p * Pool [PT , T ]) Stats () Stats {
@@ -229,18 +236,19 @@ func (p *Pool[PT, T]) getItemFromIdle() (item PT) {
229236 return item
230237}
231238
232- func (p * Pool [PT , T ]) getItem (ctx context.Context ) (_ PT , finalErr error ) {
233- onDone := p .config .trace .OnGet (& GetStartInfo {
234- Context : & ctx ,
235- Call : stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).getItem" ),
236- })
237- defer func () {
238- onDone (& GetDoneInfo {
239- Error : finalErr ,
240- })
241- }()
239+ func (p * Pool [PT , T ]) getItem (ctx context.Context ) (item PT , finalErr error ) {
240+ if onGet := p .config .trace .OnGet ; onGet != nil {
241+ onDone := onGet (& ctx ,
242+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).getItem" ),
243+ )
244+ if onDone != nil {
245+ defer func () {
246+ onDone (item , 0 , finalErr )
247+ }()
248+ }
249+ }
242250
243- item : = p .getItemFromIdle ()
251+ item = p .getItemFromIdle ()
244252
245253 if item != nil {
246254 if item .IsAlive () {
@@ -267,16 +275,17 @@ func (p *Pool[PT, T]) appendItemToIdle(item PT) {
267275}
268276
269277func (p * Pool [PT , T ]) putItem (ctx context.Context , item PT ) (finalErr error ) {
270- onDone := p .config .trace .OnPut (& PutStartInfo {
271- Context : & ctx ,
272- Call : stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).putItem" ),
273- })
274- defer func () {
275- onDone (& PutDoneInfo {
276- Error : finalErr ,
277- })
278- }()
279-
278+ if onPut := p .config .trace .OnPut ; onPut != nil {
279+ onDone := onPut (& ctx ,
280+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).putItem" ),
281+ item ,
282+ )
283+ if onDone != nil {
284+ defer func () {
285+ onDone (finalErr )
286+ }()
287+ }
288+ }
280289 if ! item .IsAlive () {
281290 p .closeItem (ctx , item )
282291
@@ -324,15 +333,16 @@ func makeAsyncCloseItemFunc[PT Item[T], T any](
324333}
325334
326335func (p * Pool [PT , T ]) try (ctx context.Context , f func (ctx context.Context , item PT ) error ) (finalErr error ) {
327- onDone := p .config .trace .OnTry (& TryStartInfo {
328- Context : & ctx ,
329- Call : stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).try" ),
330- })
331- defer func () {
332- onDone (& TryDoneInfo {
333- Error : finalErr ,
334- })
335- }()
336+ if onTry := p .config .trace .OnTry ; onTry != nil {
337+ onDone := onTry (& ctx ,
338+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).try" ),
339+ )
340+ if onDone != nil {
341+ defer func () {
342+ onDone (finalErr )
343+ }()
344+ }
345+ }
336346
337347 select {
338348 case <- p .done :
@@ -373,19 +383,18 @@ func (p *Pool[PT, T]) With(
373383 f func (ctx context.Context , item PT ) error ,
374384 opts ... retry.Option ,
375385) (finalErr error ) {
376- var (
377- onDone = p .config .trace .OnWith (& WithStartInfo {
378- Context : & ctx ,
379- Call : stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).With" ),
380- })
381- attempts int
382- )
383- defer func () {
384- onDone (& WithDoneInfo {
385- Error : finalErr ,
386- Attempts : attempts ,
387- })
388- }()
386+ var attempts int
387+
388+ if onWith := p .config .trace .OnWith ; onWith != nil {
389+ onDone := onWith (& ctx ,
390+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).With" ),
391+ )
392+ if onDone != nil {
393+ defer func () {
394+ onDone (attempts , finalErr )
395+ }()
396+ }
397+ }
389398
390399 err := retry .Retry (ctx , func (ctx context.Context ) error {
391400 err := p .try (ctx , f )
@@ -409,15 +418,16 @@ func (p *Pool[PT, T]) With(
409418}
410419
411420func (p * Pool [PT , T ]) Close (ctx context.Context ) (finalErr error ) {
412- onDone := p .config .trace .OnClose (& CloseStartInfo {
413- Context : & ctx ,
414- Call : stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).Close" ),
415- })
416- defer func () {
417- onDone (& CloseDoneInfo {
418- Error : finalErr ,
419- })
420- }()
421+ if onClose := p .config .trace .OnClose ; onClose != nil {
422+ onDone := onClose (& ctx ,
423+ stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/pool.(*Pool).Close" ),
424+ )
425+ if onDone != nil {
426+ defer func () {
427+ onDone (finalErr )
428+ }()
429+ }
430+ }
421431
422432 close (p .done )
423433
0 commit comments