generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: proxy calls through the runner
- Loading branch information
1 parent
657e9ef
commit 41b69b7
Showing
2 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/url" | ||
|
||
"connectrpc.com/connect" | ||
|
||
ftlv1 "github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1" | ||
"github.com/TBD54566975/ftl/backend/protos/xyz/block/ftl/v1/ftlv1connect" | ||
"github.com/TBD54566975/ftl/internal/errors" | ||
"github.com/TBD54566975/ftl/internal/log" | ||
"github.com/TBD54566975/ftl/internal/rpc" | ||
) | ||
|
||
var _ ftlv1connect.VerbServiceHandler = &runnerProxy{} | ||
var _ ftlv1connect.ModuleServiceHandler = &runnerProxy{} | ||
|
||
type runnerProxy struct { | ||
controllerVerbService ftlv1connect.VerbServiceClient | ||
controllerModuleService ftlv1connect.ModuleServiceClient | ||
bindAddress *url.URL | ||
} | ||
|
||
func runProxyServer(ctx context.Context, controllerVerbService ftlv1connect.VerbServiceClient, controllerModuleService ftlv1connect.ModuleServiceClient) (*runnerProxy, error) { | ||
proxy := &runnerProxy{ | ||
controllerVerbService: controllerVerbService, | ||
controllerModuleService: controllerModuleService, | ||
} | ||
parse, err := url.Parse("http://127.0.0.1:0") | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to parse url: %w", err) | ||
} | ||
proxyServer, err := rpc.NewServer(ctx, parse, | ||
rpc.GRPC(ftlv1connect.NewVerbServiceHandler, proxy), | ||
rpc.GRPC(ftlv1connect.NewModuleServiceHandler, proxy), | ||
) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to create server: %w", err) | ||
} | ||
urls := proxyServer.Bind.Subscribe(nil) | ||
go func() { | ||
proxyServer.Serve(ctx) | ||
}() | ||
proxy.bindAddress = <-urls | ||
return proxy, nil | ||
} | ||
|
||
func (r *runnerProxy) GetModuleContext(ctx context.Context, c *connect.Request[ftlv1.ModuleContextRequest], c2 *connect.ServerStream[ftlv1.GetModuleContextResponse]) error { | ||
moduleContext, err := r.controllerModuleService.GetModuleContext(ctx, connect.NewRequest(c.Msg)) | ||
if err != nil { | ||
return err | ||
} | ||
for { | ||
rcv := moduleContext.Receive() | ||
if rcv { | ||
err := c2.Send(moduleContext.Msg()) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
return moduleContext.Err() | ||
} | ||
} | ||
|
||
} | ||
|
||
func (r *runnerProxy) AcquireLease(ctx context.Context, c *connect.BidiStream[ftlv1.AcquireLeaseRequest, ftlv1.AcquireLeaseResponse]) error { | ||
logger := log.FromContext(ctx) | ||
lease := r.controllerModuleService.AcquireLease(ctx) | ||
go func() { | ||
for { | ||
req, err := c.Receive() | ||
if err != nil { | ||
if !errors.Is(err, io.EOF) { | ||
logger.Errorf(err, "Error in lease stream") | ||
} | ||
// We are done | ||
err := lease.CloseRequest() | ||
if err != nil { | ||
logger.Errorf(err, "Failed to close lease request") | ||
return | ||
} | ||
return | ||
} | ||
err = lease.Send(req) | ||
if err != nil { | ||
logger.Errorf(err, "Failed to send lease request") | ||
err := lease.CloseRequest() | ||
if err != nil { | ||
logger.Errorf(err, "Failed to close lease request") | ||
return | ||
} | ||
return | ||
} | ||
} | ||
}() | ||
go func() { | ||
for { | ||
req, err := lease.Receive() | ||
if err != nil { | ||
if !errors.Is(err, io.EOF) { | ||
logger.Errorf(err, "Error in lease stream") | ||
} | ||
// We are done | ||
err := lease.CloseResponse() | ||
if err != nil { | ||
logger.Errorf(err, "Failed to close lease response") | ||
return | ||
} | ||
return | ||
} | ||
err = c.Send(req) | ||
if err != nil { | ||
logger.Errorf(err, "Failed to send lease response") | ||
err := lease.CloseResponse() | ||
if err != nil { | ||
logger.Errorf(err, "Failed to close lease response") | ||
return | ||
} | ||
return | ||
} | ||
} | ||
}() | ||
return nil | ||
} | ||
|
||
func (r *runnerProxy) PublishEvent(ctx context.Context, c *connect.Request[ftlv1.PublishEventRequest]) (*connect.Response[ftlv1.PublishEventResponse], error) { | ||
return r.controllerModuleService.PublishEvent(ctx, connect.NewRequest(c.Msg)) | ||
} | ||
|
||
func (r *runnerProxy) Ping(ctx context.Context, c *connect.Request[ftlv1.PingRequest]) (*connect.Response[ftlv1.PingResponse], error) { | ||
return connect.NewResponse(&ftlv1.PingResponse{}), nil | ||
} | ||
|
||
func (r *runnerProxy) Call(ctx context.Context, c *connect.Request[ftlv1.CallRequest]) (*connect.Response[ftlv1.CallResponse], error) { | ||
return r.controllerVerbService.Call(ctx, connect.NewRequest(c.Msg)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters