github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/with.go (about)

     1  package ydb
     2  
     3  import (
     4  	"context"
     5  	"sync/atomic"
     6  
     7  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/stack"
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/trace"
    10  )
    11  
    12  var nextID atomic.Uint64 //nolint:gochecknoglobals
    13  
    14  func (d *Driver) with(ctx context.Context, opts ...Option) (*Driver, uint64, error) {
    15  	id := nextID.Add(1)
    16  
    17  	child, err := newConnectionFromOptions(
    18  		ctx,
    19  		append(
    20  			append(
    21  				d.opts,
    22  				WithBalancer(
    23  					d.config.Balancer(),
    24  				),
    25  				withOnClose(func(child *Driver) {
    26  					d.childrenMtx.Lock()
    27  					defer d.childrenMtx.Unlock()
    28  
    29  					delete(d.children, id)
    30  				}),
    31  				withConnPool(d.pool),
    32  			),
    33  			opts...,
    34  		)...,
    35  	)
    36  	if err != nil {
    37  		return nil, 0, xerrors.WithStackTrace(err)
    38  	}
    39  
    40  	return child, id, nil
    41  }
    42  
    43  // With makes child Driver with the same options and another options
    44  func (d *Driver) With(ctx context.Context, opts ...Option) (*Driver, error) {
    45  	child, id, err := d.with(ctx, opts...)
    46  	if err != nil {
    47  		return nil, xerrors.WithStackTrace(err)
    48  	}
    49  
    50  	onDone := trace.DriverOnWith(
    51  		d.trace(), &ctx,
    52  		stack.FunctionID(""),
    53  		d.config.Endpoint(), d.config.Database(), d.config.Secure(),
    54  	)
    55  	defer func() {
    56  		onDone(err)
    57  	}()
    58  
    59  	if err = child.connect(ctx); err != nil {
    60  		return nil, xerrors.WithStackTrace(err)
    61  	}
    62  
    63  	d.childrenMtx.Lock()
    64  	defer d.childrenMtx.Unlock()
    65  
    66  	d.children[id] = child
    67  
    68  	return child, nil
    69  }