Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions internal/strategy/git/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,60 +5,13 @@ import (
"io"
"log/slog"
"net/textproto"
"os"
"strings"
"time"

"github.com/alecthomas/errors"

"github.com/block/cachew/internal/cache"
"github.com/block/cachew/internal/logging"
)

func (s *Strategy) cloneBundleLoop(ctx context.Context, c *clone) {
logger := logging.FromContext(ctx)

s.generateAndUploadBundleIfMissing(ctx, c)

ticker := time.NewTicker(s.config.BundleInterval)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
logger.DebugContext(ctx, "Bundle generator shutting down",
slog.String("upstream", c.upstreamURL))
return

case <-ticker.C:
s.generateAndUploadBundle(ctx, c)
}
}
}

func (s *Strategy) generateAndUploadBundleIfMissing(ctx context.Context, c *clone) {
logger := logging.FromContext(ctx)

cacheKey := cache.NewKey(c.upstreamURL + ".bundle")

reader, _, err := s.cache.Open(ctx, cacheKey)
if err == nil {
_ = reader.Close()
logger.DebugContext(ctx, "Bundle already exists in cache, skipping generation",
slog.String("upstream", c.upstreamURL))
return
}

if !errors.Is(err, os.ErrNotExist) {
logger.ErrorContext(ctx, "Failed to check for existing bundle",
slog.String("upstream", c.upstreamURL),
slog.String("error", err.Error()))
return
}

s.generateAndUploadBundle(ctx, c)
}

func (s *Strategy) generateAndUploadBundle(ctx context.Context, c *clone) {
logger := logging.FromContext(ctx)

Expand Down
31 changes: 23 additions & 8 deletions internal/strategy/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,10 @@ type Strategy struct {
httpClient *http.Client
proxy *httputil.ReverseProxy
ctx context.Context
scheduler jobscheduler.Scheduler
}

func New(ctx context.Context, _ jobscheduler.Scheduler, config Config, cache cache.Cache, mux strategy.Mux) (*Strategy, error) {
func New(ctx context.Context, scheduler jobscheduler.Scheduler, config Config, cache cache.Cache, mux strategy.Mux) (*Strategy, error) {
logger := logging.FromContext(ctx)

if config.MirrorRoot == "" {
Expand All @@ -88,6 +89,7 @@ func New(ctx context.Context, _ jobscheduler.Scheduler, config Config, cache cac
clones: make(map[string]*clone),
httpClient: http.DefaultClient,
ctx: ctx,
scheduler: scheduler.WithQueuePrefix("git"),
}

if err := s.discoverExistingClones(ctx); err != nil {
Expand Down Expand Up @@ -170,7 +172,7 @@ func (s *Strategy) handleRequest(w http.ResponseWriter, r *http.Request) {
slog.String("error", err.Error()))
}
}
s.maybeBackgroundFetch(ctx, c)
s.maybeBackgroundFetch(c)
s.serveFromBackend(w, r, c)

case stateCloning:
Expand All @@ -179,7 +181,10 @@ func (s *Strategy) handleRequest(w http.ResponseWriter, r *http.Request) {

case stateEmpty:
logger.DebugContext(ctx, "Starting background clone, forwarding to upstream")
go s.startClone(context.WithoutCancel(ctx), c)
s.scheduler.Submit(c.upstreamURL, "clone", func(ctx context.Context) error {
s.startClone(ctx, c)
return nil
})
s.forwardToUpstream(w, r, host, pathValue)
}
}
Expand Down Expand Up @@ -268,7 +273,7 @@ func (s *Strategy) getOrCreateClone(ctx context.Context, upstreamURL string) *cl
slog.String("path", clonePath))

if s.config.BundleInterval > 0 {
go s.cloneBundleLoop(s.ctx, c)
s.scheduleBundleJobs(c)
}
}

Expand Down Expand Up @@ -349,7 +354,7 @@ func (s *Strategy) discoverExistingClones(ctx context.Context) error {
slog.String("upstream", upstreamURL))

if s.config.BundleInterval > 0 {
go s.cloneBundleLoop(s.ctx, c)
s.scheduleBundleJobs(c)
}

return nil
Expand Down Expand Up @@ -397,11 +402,11 @@ func (s *Strategy) startClone(ctx context.Context, c *clone) {
slog.String("path", c.path))

if s.config.BundleInterval > 0 {
go s.cloneBundleLoop(context.WithoutCancel(ctx), c)
s.scheduleBundleJobs(c)
}
}

func (s *Strategy) maybeBackgroundFetch(ctx context.Context, c *clone) {
func (s *Strategy) maybeBackgroundFetch(c *clone) {
c.mu.RLock()
lastFetch := c.lastFetch
c.mu.RUnlock()
Expand All @@ -410,7 +415,10 @@ func (s *Strategy) maybeBackgroundFetch(ctx context.Context, c *clone) {
return
}

go s.backgroundFetch(context.WithoutCancel(ctx), c)
s.scheduler.Submit(c.upstreamURL, "fetch", func(ctx context.Context) error {
s.backgroundFetch(ctx, c)
return nil
})
}

func (s *Strategy) backgroundFetch(ctx context.Context, c *clone) {
Expand All @@ -434,3 +442,10 @@ func (s *Strategy) backgroundFetch(ctx context.Context, c *clone) {
slog.String("error", err.Error()))
}
}

func (s *Strategy) scheduleBundleJobs(c *clone) {
s.scheduler.SubmitPeriodicJob(c.upstreamURL, "bundle-periodic", s.config.BundleInterval, func(ctx context.Context) error {
s.generateAndUploadBundle(ctx, c)
return nil
})
}