From 37542a5870322e07ff6e4930f1c0212d99dd3971 Mon Sep 17 00:00:00 2001
From: Matt Toohey <mtoohey@block.xyz>
Date: Tue, 7 Jan 2025 14:19:06 +1100
Subject: [PATCH] fix: speed up pubsub provisioning (#3916)

fixes: https://github.com/block/ftl/issues/3901
prerequisite: https://github.com/block/ftl/pull/3917

Wraps `docker compose up` with a lock and a cached success flag.
---
 backend/runner/runner.go |  2 +-
 internal/dev/redpanda.go | 12 ++++++++++++
 2 files changed, 13 insertions(+), 1 deletion(-)

diff --git a/backend/runner/runner.go b/backend/runner/runner.go
index 84017ffccb..efd6f83b76 100644
--- a/backend/runner/runner.go
+++ b/backend/runner/runner.go
@@ -350,7 +350,7 @@ func (s *Service) deploy(ctx context.Context, key model.DeploymentKey, module *s
 	pubSub, err := pubsub.New(module, key, s, s.timelineClient)
 	if err != nil {
 		observability.Deployment.Failure(ctx, optional.Some(key.String()))
-		return fmt.Errorf("failed to create pubsub service: %w", err)
+		return fmt.Errorf("failed to set up pubsub: %w", err)
 	}
 	s.pubSub = pubSub
 
diff --git a/internal/dev/redpanda.go b/internal/dev/redpanda.go
index 93a2c07343..542f3f7759 100644
--- a/internal/dev/redpanda.go
+++ b/internal/dev/redpanda.go
@@ -5,6 +5,7 @@ import (
 	_ "embed"
 	"fmt"
 	"os"
+	"sync"
 
 	"github.com/alecthomas/types/optional"
 
@@ -14,7 +15,17 @@ import (
 //go:embed docker-compose.redpanda.yml
 var redpandaDockerCompose string
 
+// use this lock while checking redPandaRunning status and running `docker compose up` if needed
+var redPandaLock = &sync.Mutex{}
+var redPandaRunning bool
+
 func SetUpRedPanda(ctx context.Context) error {
+	redPandaLock.Lock()
+	defer redPandaLock.Unlock()
+
+	if redPandaRunning {
+		return nil
+	}
 	var profile optional.Option[string]
 	if _, ci := os.LookupEnv("CI"); !ci {
 		// include console except in CI
@@ -24,5 +35,6 @@ func SetUpRedPanda(ctx context.Context) error {
 	if err != nil {
 		return fmt.Errorf("could not start redpanda: %w", err)
 	}
+	redPandaRunning = true
 	return nil
 }