You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: proxy.go
    path of file: ./repos/encore/cli/daemon/sqldb
    
    the code of the file until where you have to start completion: package sqldb

import (
	"context"
	"crypto/tls"
	"fmt"
	"io"
	"net"
	"strings"
	"time"

	"github.com/jackc/pgproto3/v2"
	"github.com/rs/zerolog/log"

	"encr.dev/cli/daemon/namespace"
	"encr.dev/pkg/fns"
	"encr.dev/pkg/pgproxy"
)

// ServeProxy serves the database proxy using the given listener.
func (cm *ClusterManager) ServeProxy(ln net.Listener) error {
	var tempDelay time.Duration // how long to sleep on accept failure
	for {
		conn, e := ln.Accept()
		if e != nil {
			if ne, ok := e.(net.Error); ok && ne.Temporary() {
				if tempDelay == 0 {
					tempDelay = 5 * time.Millisecond
				} else {
					tempDelay *= 2
				}
				if max := 1 * time.Second; tempDelay > max {
					tempDelay = max
				}
				log.Error().Err(e).Msgf("dbpro
    
func (cm *ClusterManager) PreauthProxyConn(client net.Conn, id ClusterID) error {
	defer fns.CloseIgnore(client)
	cl, err := pgproxy.SetupClient(client, &pgproxy.ClientConfig{
		TLS: &tls.Config{MinVersion: tls.VersionTLS12},
	})
	if err != nil {
		log.Error().Err(err).Msg("failed to setup client")
		return err
	}

	if cancel, ok := cl.Hello.(*pgproxy.CancelData); ok {
		cm.cancelRequest(client, cancel)
		return nil
	}
	startup := cl.Hello.(*pgproxy.StartupData)

	cluster, ok := cm.Get(id)
	if !ok {
		cm.log.Error().Interface("cluster", id).Msg("dbproxy: could not find cluster")
		_ = cl.Backend.Send(&pgproto3.ErrorResponse{
			Severity: "FATAL",
			Code:     "08006",
			Message:  "database cluster not running",
		})
		return nil
	}

	db, ok := cluster.GetDB(startup.Database)
	if !ok {
		_ = cl.Backend.Send(&pgproto3.ErrorResponse{
			Severity: "FATAL",
			Code:     "08006",
			Message:  "database not found",
		})
		return nil
	}

	// Wait for up to 60s for the cluster to come online.
	select {
	case <-db.Ctx.Done():
		_ = cl.Backend.Send(&pgproto3.ErrorResponse{
			Severity: "FATAL",
			Code:     "08006",
			Message:  "db is shutting down",
		})
		return nil
	case <-time.After(60 * time.Second):
		cm.log.Error().Str("db", startup.Database).Msg("dbproxy: timed out waiting for database to come online")
		_ = cl.Backend.Send(&pgproto3.ErrorResponse{
			Severity: "FATAL",
			Code:     "08006",
			Message:  "timed out waiting for db to complete setup",
		})
		return nil

	case <-cluster.Ready():
		// Continue connecting to backend, below
	}

	info, err := cluster.Info(context.Background())
	if err != nil {
		_ = cl.Backend.Send(&pgproto3.ErrorResponse{
			Severity: "FATAL",
			Code:     "08006",
			Message:  "cluster not running: " + err.Error(),
		})
		return nil
	}

	server, err := net.Dial("tcp", info.Config.Host)
	if err != nil {
		_ = cl.Backend.Send(&pgproto3.ErrorResponse{
			Severity: "FATAL",
			Code:     "08006",
			Message:  "database not running: " + err.Error(),
		})
		return nil
	}
	defer fns.CloseIgnore(server)

	admin, _ := info.Encore.First(RoleAdmin, RoleSuperuser)
	startup.Username = admin.Username
	startup.Password = admin.Password
	startup.Database = db.DriverName
	fe, err := pgproxy.SetupServer(server, &pgproxy.ServerConfig{
		TLS:     nil,
		Startup: startup,
	})
	if err != nil {
		_ = cl.Backend.Send(&pgproto3.ErrorResponse{
			Severity: "FATAL",
			Code:     "08006",
			Message:  "could not connect: " + err.Error(),
		})
		return nil
	}

	if err := pgproxy.AuthenticateClient(cl.Backend); err != nil {
		return err
	}

	keyData, err := pgproxy.FinalizeInitialHandshake(cl.Backend, fe)
	if err != nil {
		_ = cl.Backend.Send(&pgproto3.ErrorResponse{
			Severity: "FATAL",
			Code:     "08006",
			Message:  "could not establish connection: " + err.Error(),
		})
		return nil
	}

	// Store the key data so we know where to route cancellation requests.
	if keyData != nil {
		cm.mu.Lock()
		cm.backendKeyData[keyData.SecretKey] = cluster
		cm.mu.Unlock()
		defer func() {
			cm.mu.Lock()
			delete(cm.backendKeyData, keyData.SecretKey)
			cm.mu.Unlock()
		}()
	}

	log.Trace().Msg("successfully completed handshake, copying data back and forth")
	return pgproxy.CopySteadyState(cl.Backend, fe)
}