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())
| 554 | // NewClient completes the initial setup required to get the proxy to a "steady" |
| 555 | // state. |
| 556 | func 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 |