| 169 | } |
| 170 | |
| 171 | func (p *Proxy) Connect() error { |
| 172 | p.mu.Lock() |
| 173 | defer p.mu.Unlock() |
| 174 | |
| 175 | if p.isConnected { |
| 176 | return ErrProxyAlreadyConnected |
| 177 | } |
| 178 | |
| 179 | var err error |
| 180 | p.preparedCache, err = getOrCreateDefaultPreparedCache(p.config.PreparedCache) |
| 181 | if err != nil { |
| 182 | return fmt.Errorf("unable to create prepared cache %w", err) |
| 183 | } |
| 184 | |
| 185 | p.cluster, err = proxycore.ConnectCluster(p.ctx, proxycore.ClusterConfig{ |
| 186 | Version: p.config.Version, |
| 187 | Auth: p.config.Auth, |
| 188 | Resolver: p.config.Resolver, |
| 189 | ReconnectPolicy: p.config.ReconnectPolicy, |
| 190 | HeartBeatInterval: p.config.HeartBeatInterval, |
| 191 | ConnectTimeout: p.config.ConnectTimeout, |
| 192 | IdleTimeout: p.config.IdleTimeout, |
| 193 | Logger: p.logger, |
| 194 | }) |
| 195 | |
| 196 | if err != nil { |
| 197 | return fmt.Errorf("unable to connect to cluster %w", err) |
| 198 | } |
| 199 | |
| 200 | err = p.cluster.Listen(p) |
| 201 | if err != nil { |
| 202 | return fmt.Errorf("unable to register to listen for schema events %w", err) |
| 203 | } |
| 204 | |
| 205 | err = p.buildNodes() |
| 206 | if err != nil { |
| 207 | return fmt.Errorf("unable to build node information: %w", err) |
| 208 | } |
| 209 | |
| 210 | p.buildLocalRow() |
| 211 | |
| 212 | p.lb = proxycore.NewRoundRobinLoadBalancer() |
| 213 | err = p.cluster.Listen(p.lb) |
| 214 | if err != nil { |
| 215 | return err |
| 216 | } |
| 217 | |
| 218 | sess, err := proxycore.ConnectSession(p.ctx, p.cluster, proxycore.SessionConfig{ |
| 219 | ReconnectPolicy: p.config.ReconnectPolicy, |
| 220 | NumConns: p.config.NumConns, |
| 221 | Version: p.cluster.NegotiatedVersion, |
| 222 | Auth: p.config.Auth, |
| 223 | HeartBeatInterval: p.config.HeartBeatInterval, |
| 224 | ConnectTimeout: p.config.ConnectTimeout, |
| 225 | IdleTimeout: p.config.IdleTimeout, |
| 226 | PreparedCache: p.preparedCache, |
| 227 | Logger: p.logger, |
| 228 | }) |