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
a98f447
commit 376dcfd
Showing
2 changed files
with
125 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,114 @@ | ||
package runner | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"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/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, | ||
} | ||
logger := log.FromContext(ctx) | ||
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() { | ||
err := proxyServer.Serve(ctx) | ||
if err != nil { | ||
logger.Errorf(err, "failed to serve") | ||
return | ||
} | ||
}() | ||
proxy.bindAddress = <-urls | ||
return proxy, nil | ||
} | ||
|
||
func (r *runnerProxy) GetModuleContext(ctx context.Context, c *connect.Request[ftlv1.GetModuleContextRequest], c2 *connect.ServerStream[ftlv1.GetModuleContextResponse]) error { | ||
moduleContext, err := r.controllerModuleService.GetModuleContext(ctx, connect.NewRequest(c.Msg)) | ||
if err != nil { | ||
return fmt.Errorf("failed to get module context: %w", err) | ||
} | ||
for { | ||
rcv := moduleContext.Receive() | ||
if rcv { | ||
err := c2.Send(moduleContext.Msg()) | ||
if err != nil { | ||
return fmt.Errorf("failed to send message: %w", err) | ||
} | ||
} else { | ||
return fmt.Errorf("failed to receive message: %w", moduleContext.Err()) | ||
} | ||
} | ||
|
||
} | ||
|
||
func (r *runnerProxy) AcquireLease(ctx context.Context, c *connect.BidiStream[ftlv1.AcquireLeaseRequest, ftlv1.AcquireLeaseResponse]) error { | ||
lease := r.controllerModuleService.AcquireLease(ctx) | ||
for { | ||
req, err := c.Receive() | ||
if err != nil { | ||
return fmt.Errorf("failed to receive message: %w", err) | ||
} | ||
err = lease.Send(req) | ||
if err != nil { | ||
return fmt.Errorf("failed to send message: %w", err) | ||
} | ||
msg, err := lease.Receive() | ||
if err != nil { | ||
return fmt.Errorf("failed to receive response message: %w", err) | ||
} | ||
err = c.Send(msg) | ||
if err != nil { | ||
return fmt.Errorf("failed to send response message: %w", err) | ||
} | ||
} | ||
|
||
} | ||
|
||
func (r *runnerProxy) PublishEvent(ctx context.Context, c *connect.Request[ftlv1.PublishEventRequest]) (*connect.Response[ftlv1.PublishEventResponse], error) { | ||
event, err := r.controllerModuleService.PublishEvent(ctx, connect.NewRequest(c.Msg)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to proxy event: %w", err) | ||
} | ||
return event, nil | ||
} | ||
|
||
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) { | ||
call, err := r.controllerVerbService.Call(ctx, connect.NewRequest(c.Msg)) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to proxy verb: %w", err) | ||
} | ||
return call, nil | ||
} |
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