MCPcopy Index your code
hub / github.com/GoogleCloudPlatform/cloud-sql-proxy / NewClient

Function NewClient

internal/proxy/proxy.go:556–614  ·  view source on GitHub ↗

NewClient completes the initial setup required to get the proxy to a "steady" state.

(ctx context.Context, d cloudsql.Dialer, l cloudsql.Logger, conf *Config, connRefuseNotify func())

Source from the content-addressed store, hash-verified

554// NewClient completes the initial setup required to get the proxy to a "steady"
555// state.
556func NewClient(ctx context.Context, d cloudsql.Dialer, l cloudsql.Logger, conf *Config, connRefuseNotify func()) (*Client, error) {
557 // Check if the caller has configured a dialer.
558 // Otherwise, initialize a new one.
559 if d == nil {
560 dialerOpts, err := conf.DialerOptions(l)
561 if err != nil {
562 return nil, fmt.Errorf("error initializing dialer: %v", err)
563 }
564 d, err = cloudsqlconn.NewDialer(ctx, dialerOpts...)
565 if err != nil {
566 return nil, fmt.Errorf("error initializing dialer: %v", err)
567 }
568 }
569
570 c := &Client{
571 logger: l,
572 dialer: d,
573 connRefuseNotify: connRefuseNotify,
574 conf: conf,
575 }
576
577 if conf.FUSEDir != "" {
578 return configureFUSE(c, conf)
579 }
580
581 // unless the proxy is in SqlDataEnabled mode, initiate a refresh operation to warm the cache
582 for _, inst := range conf.Instances {
583 // Skip instances with SqlDataEnabled
584 if conf.SQLDataEnabled || inst.SQLDataEnabled != nil && *inst.SQLDataEnabled {
585 continue
586 }
587 go func(name string) { _, _ = d.EngineVersion(ctx, name) }(inst.Name)
588 }
589
590 var mnts []*socketMount
591 pc := newPortConfig(conf.Port)
592 for _, inst := range conf.Instances {
593 m, err := c.newSocketMount(ctx, conf, pc, inst)
594 if err != nil {
595 if conf.SkipFailedInstanceConfig {
596 l.Errorf("[%v] Unable to mount socket: %v (skipped due to skip-failed-instance-config flag)", inst.Name, err)
597 continue
598 }
599
600 for _, m := range mnts {
601 mErr := m.Close()
602 if mErr != nil {
603 l.Errorf("failed to close mount: %v", mErr)
604 }
605 }
606 return nil, fmt.Errorf("[%v] Unable to mount socket: %v", inst.Name, err)
607 }
608
609 l.Infof("[%s] Listening on %s", inst.Name, m.Addr())
610 mnts = append(mnts, m)
611 }
612 c.mnts = mnts
613 return c, nil

Callers 15

newProxyWithParamsFunction · 0.92
TestFuseClosesGracefullyFunction · 0.92
newTestClientFunction · 0.92
TestFUSEWithBadDirFunction · 0.92
TestClientInitializationFunction · 0.92
TestClientClosesCleanlyFunction · 0.92
TestClosesWithErrorFunction · 0.92
TestClientConnCountFunction · 0.92

Calls 9

newSocketMountMethod · 0.95
newPortConfigFunction · 0.85
DialerOptionsMethod · 0.80
AddrMethod · 0.80
configureFUSEFunction · 0.70
ErrorfMethod · 0.65
EngineVersionMethod · 0.65
InfofMethod · 0.65
CloseMethod · 0.45

Tested by 15

newProxyWithParamsFunction · 0.74
TestFuseClosesGracefullyFunction · 0.74
newTestClientFunction · 0.74
TestFUSEWithBadDirFunction · 0.74
TestClientInitializationFunction · 0.74
TestClientClosesCleanlyFunction · 0.74
TestClosesWithErrorFunction · 0.74
TestClientConnCountFunction · 0.74