Skip to content

Commit

Permalink
feat: run pgproxy as a part of the runner (#3456)
Browse files Browse the repository at this point in the history
The proxy does nothing yet
  • Loading branch information
jvmakine authored Nov 22, 2024
1 parent c5c602e commit 1be2e1f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions cmd/ftl-proxy-pg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var cli struct {
ObservabilityConfig observability.Config `embed:"" prefix:"o11y-"`
LogConfig log.Config `embed:"" prefix:"log-"`

Listen string `name:"listen" short:"l" help:"Address to listen on." env:"FTL_PROXY_PG_LISTEN" default:"127.0.0.1:5678"`
pgproxy.Config
}

func main() {
Expand All @@ -38,7 +38,7 @@ func main() {
err = observability.Init(ctx, false, "", "ftl-provisioner", ftl.Version, cli.ObservabilityConfig)
kctx.FatalIfErrorf(err, "failed to initialize observability")

proxy := pgproxy.New(cli.Listen, func(ctx context.Context, params map[string]string) (string, error) {
proxy := pgproxy.New(cli.Config, func(ctx context.Context, params map[string]string) (string, error) {
return "postgres://localhost:5432/postgres?user=" + params["user"], nil
})
if err := proxy.Start(ctx); err != nil {
Expand Down
24 changes: 22 additions & 2 deletions cmd/ftl-runner/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@ package main

import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/alecthomas/kong"
"golang.org/x/sync/errgroup"

"github.com/TBD54566975/ftl"
"github.com/TBD54566975/ftl/backend/runner"
_ "github.com/TBD54566975/ftl/internal/automaxprocs" // Set GOMAXPROCS to match Linux container CPU quota.
"github.com/TBD54566975/ftl/internal/log"
"github.com/TBD54566975/ftl/internal/pgproxy"
)

var cli struct {
Version kong.VersionFlag `help:"Show version."`
LogConfig log.Config `prefix:"log-" embed:""`
RunnerConfig runner.Config `embed:""`
ProxyConfig pgproxy.Config `embed:"" prefix:"pgproxy-"`
}

func main() {
Expand All @@ -42,6 +46,22 @@ and route to user code.
})
logger := log.Configure(os.Stderr, cli.LogConfig)
ctx := log.ContextWithLogger(context.Background(), logger)
err = runner.Start(ctx, cli.RunnerConfig)
kctx.FatalIfErrorf(err)

g, ctx := errgroup.WithContext(ctx)
g.Go(func() error {
return runPGProxy(ctx, cli.ProxyConfig)
})
g.Go(func() error {
return runner.Start(ctx, cli.RunnerConfig)
})
kctx.FatalIfErrorf(g.Wait())
}

func runPGProxy(ctx context.Context, config pgproxy.Config) error {
if err := pgproxy.New(config, func(ctx context.Context, params map[string]string) (string, error) {
return "postgres://127.0.0.1:5432/postgres?user=" + params["user"], nil
}).Start(ctx); err != nil {
return fmt.Errorf("failed to start pgproxy: %w", err)
}
return nil
}
8 changes: 6 additions & 2 deletions internal/pgproxy/pgproxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
"github.com/TBD54566975/ftl/internal/log"
)

type Config struct {
Listen string `name:"listen" short:"l" help:"Address to listen on." env:"FTL_PROXY_PG_LISTEN" default:"127.0.0.1:5678"`
}

// PgProxy is a configurable proxy for PostgreSQL connections
type PgProxy struct {
listenAddress string
Expand All @@ -26,9 +30,9 @@ type DSNConstructor func(ctx context.Context, params map[string]string) (string,
//
// address is the address to listen on for incoming connections.
// connectionFn is a function that constructs a new connection string from parameters of the incoming connection.
func New(address string, connectionFn DSNConstructor) *PgProxy {
func New(config Config, connectionFn DSNConstructor) *PgProxy {
return &PgProxy{
listenAddress: address,
listenAddress: config.Listen,
connectionStringFn: connectionFn,
}
}
Expand Down

0 comments on commit 1be2e1f

Please sign in to comment.