Skip to content

Commit

Permalink
fix: GetModuleContext() was busy looping (#1880)
Browse files Browse the repository at this point in the history
By design, `RetryStreamingServerStream()` reissues the underlying RPC
whenever the stream terminates, and `GetModuleContext()` was exiting
immediately after sending its first response, resulting in a busy loop.
  • Loading branch information
alecthomas authored Jun 26, 2024
1 parent ea30985 commit 94d341c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
37 changes: 20 additions & 17 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ type Config struct {
RunnerTimeout time.Duration `help:"Runner heartbeat timeout." default:"10s"`
ControllerTimeout time.Duration `help:"Controller heartbeat timeout." default:"10s"`
DeploymentReservationTimeout time.Duration `help:"Deployment reservation timeout." default:"120s"`
ModuleUpdateFrequency time.Duration `help:"Frequency to send module updates." default:"30s"`
ArtefactChunkSize int `help:"Size of each chunk streamed to the client." default:"1048576"`
CommonConfig
}
Expand Down Expand Up @@ -678,26 +679,28 @@ func (s *Service) GetModuleContext(ctx context.Context, req *connect.Request[ftl
cm := cf.ConfigFromContext(ctx)
sm := cf.SecretsFromContext(ctx)

configs, err := cm.MapForModule(ctx, name)
if err != nil {
return connect.NewError(connect.CodeInternal, fmt.Errorf("could not get configs: %w", err))
}
secrets, err := sm.MapForModule(ctx, name)
if err != nil {
return connect.NewError(connect.CodeInternal, fmt.Errorf("could not get secrets: %w", err))
}
databases, err := modulecontext.DatabasesFromSecrets(ctx, name, secrets)
if err != nil {
return connect.NewError(connect.CodeInternal, fmt.Errorf("could not get databases: %w", err))
}
for {
configs, err := cm.MapForModule(ctx, name)
if err != nil {
return connect.NewError(connect.CodeInternal, fmt.Errorf("could not get configs: %w", err))
}
secrets, err := sm.MapForModule(ctx, name)
if err != nil {
return connect.NewError(connect.CodeInternal, fmt.Errorf("could not get secrets: %w", err))
}
databases, err := modulecontext.DatabasesFromSecrets(ctx, name, secrets)
if err != nil {
return connect.NewError(connect.CodeInternal, fmt.Errorf("could not get databases: %w", err))
}

response := modulecontext.NewBuilder(name).AddConfigs(configs).AddSecrets(secrets).AddDatabases(databases).Build().ToProto()
response := modulecontext.NewBuilder(name).AddConfigs(configs).AddSecrets(secrets).AddDatabases(databases).Build().ToProto()

if err := resp.Send(response); err != nil {
return connect.NewError(connect.CodeInternal, fmt.Errorf("could not send response: %w", err))
}
if err := resp.Send(response); err != nil {
return connect.NewError(connect.CodeInternal, fmt.Errorf("could not send response: %w", err))
}

return nil
time.Sleep(s.config.ModuleUpdateFrequency)
}
}

// AcquireLease acquires a lease on behalf of a module.
Expand Down
1 change: 1 addition & 0 deletions cmd/ftl/cmd_serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ func (s *serveCmd) Run(ctx context.Context, projConfig projectconfig.Config) err
if err := kong.ApplyDefaults(&config); err != nil {
return err
}
config.ModuleUpdateFrequency = time.Second * 1

scope := fmt.Sprintf("controller%d", i)
controllerCtx := log.ContextWithLogger(ctx, logger.Scope(scope))
Expand Down

0 comments on commit 94d341c

Please sign in to comment.