Skip to content

Commit

Permalink
Add IDEClientService
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanp413 authored and roboquat committed Nov 17, 2022
1 parent fb29551 commit cd144de
Show file tree
Hide file tree
Showing 15 changed files with 1,162 additions and 828 deletions.
91 changes: 91 additions & 0 deletions components/public-api-server/pkg/apiv1/ide_client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License-AGPL.txt in the project root for license information.

package apiv1

import (
"context"
"fmt"

connect "github.com/bufbuild/connect-go"
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
"github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1/v1connect"
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
"github.com/gitpod-io/gitpod/public-api-server/pkg/proxy"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
)

func NewIDEClientService(pool proxy.ServerConnectionPool) *IDEClientService {
return &IDEClientService{
connectionPool: pool,
}
}

var _ v1connect.IDEClientServiceHandler = (*IDEClientService)(nil)

type IDEClientService struct {
connectionPool proxy.ServerConnectionPool

v1connect.UnimplementedIDEClientServiceHandler
}

func (this *IDEClientService) SendHeartbeat(ctx context.Context, req *connect.Request[v1.SendHeartbeatRequest]) (*connect.Response[v1.SendHeartbeatResponse], error) {
logger := ctxlogrus.Extract(ctx)

conn, err := getConnection(ctx, this.connectionPool)
if err != nil {
return nil, err
}

workspace, err := conn.GetWorkspace(ctx, req.Msg.GetWorkspaceId())
if err != nil {
logger.WithError(err).Error("Failed to get workspace.")
return nil, proxy.ConvertError(err)
}

if workspace.LatestInstance == nil {
logger.WithError(err).Error("Failed to get latest instance.")
return nil, connect.NewError(connect.CodeFailedPrecondition, fmt.Errorf("instance not found"))
}

err = conn.SendHeartBeat(ctx, &protocol.SendHeartBeatOptions{
InstanceID: workspace.LatestInstance.ID,
WasClosed: false,
})
if err != nil {
return nil, proxy.ConvertError(err)
}

return connect.NewResponse(&v1.SendHeartbeatResponse{}), nil
}

func (this *IDEClientService) SendDidClose(ctx context.Context, req *connect.Request[v1.SendDidCloseRequest]) (*connect.Response[v1.SendDidCloseResponse], error) {
logger := ctxlogrus.Extract(ctx)

conn, err := getConnection(ctx, this.connectionPool)
if err != nil {
return nil, err
}

workspace, err := conn.GetWorkspace(ctx, req.Msg.GetWorkspaceId())
if err != nil {
logger.WithError(err).Error("Failed to get workspace.")
return nil, proxy.ConvertError(err)
}

if workspace.LatestInstance == nil {
logger.WithError(err).Error("Failed to get latest instance.")
return nil, connect.NewError(connect.CodeFailedPrecondition, fmt.Errorf("instance not found"))
}

err = conn.SendHeartBeat(ctx, &protocol.SendHeartBeatOptions{
InstanceID: workspace.LatestInstance.ID,
WasClosed: true,
})
if err != nil {
return nil, proxy.ConvertError(err)
}

return connect.NewResponse(&v1.SendDidCloseResponse{}), nil
}
34 changes: 0 additions & 34 deletions components/public-api-server/pkg/apiv1/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,40 +122,6 @@ func (s *WorkspaceService) ListWorkspaces(ctx context.Context, req *connect.Requ
), nil
}

func (s *WorkspaceService) SendHeartbeat(ctx context.Context, req *connect.Request[v1.SendHeartbeatRequest]) (*connect.Response[v1.SendHeartbeatResponse], error) {
conn, err := getConnection(ctx, s.connectionPool)
if err != nil {
return nil, err
}

err = conn.SendHeartBeat(ctx, &protocol.SendHeartBeatOptions{
InstanceID: req.Msg.GetWorkspaceId(),
WasClosed: false,
})
if err != nil {
return nil, proxy.ConvertError(err)
}

return connect.NewResponse(&v1.SendHeartbeatResponse{}), nil
}

func (s *WorkspaceService) SendCloseSignal(ctx context.Context, req *connect.Request[v1.SendCloseSignalRequest]) (*connect.Response[v1.SendCloseSignalResponse], error) {
conn, err := getConnection(ctx, s.connectionPool)
if err != nil {
return nil, err
}

err = conn.SendHeartBeat(ctx, &protocol.SendHeartBeatOptions{
InstanceID: req.Msg.GetWorkspaceId(),
WasClosed: true,
})
if err != nil {
return nil, proxy.ConvertError(err)
}

return connect.NewResponse(&v1.SendCloseSignalResponse{}), nil
}

func getLimitFromPagination(pagination *v1.Pagination) (int, error) {
const (
defaultLimit = 20
Expand Down
3 changes: 3 additions & 0 deletions components/public-api-server/pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ func register(srv *baseserver.Server, connPool proxy.ServerConnectionPool, expCl
userRoute, userServiceHandler := v1connect.NewUserServiceHandler(apiv1.NewUserService(connPool), handlerOptions...)
srv.HTTPMux().Handle(userRoute, userServiceHandler)

ideClientRoute, ideClientServiceHandler := v1connect.NewIDEClientServiceHandler(apiv1.NewIDEClientService(connPool), handlerOptions...)
srv.HTTPMux().Handle(ideClientRoute, ideClientServiceHandler)

return nil
}

Expand Down
23 changes: 23 additions & 0 deletions components/public-api/gitpod/experimental/v1/ide_client.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
syntax = "proto3";

package gitpod.experimental.v1;

option go_package = "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1";

service IDEClientService {
// SendHeartbeat sends a clientheartbeat signal for a running workspace.
rpc SendHeartbeat(SendHeartbeatRequest) returns (SendHeartbeatResponse) {}

// SendDidClose sends a client close signal for a running workspace.
rpc SendDidClose(SendDidCloseRequest) returns (SendDidCloseResponse) {}
}

message SendHeartbeatRequest {
string workspace_id = 1;
}
message SendHeartbeatResponse {}

message SendDidCloseRequest {
string workspace_id = 1;
}
message SendDidCloseResponse {}
16 changes: 0 additions & 16 deletions components/public-api/gitpod/experimental/v1/workspaces.proto
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ service WorkspacesService {
// NOT_FOUND: the workspace_id is unkown
// FAILED_PRECONDITION: if there's no running instance
rpc StopWorkspace(StopWorkspaceRequest) returns (stream StopWorkspaceResponse) {}

// SendHeartbeat sends a heartbeat signal to a running workspace.
rpc SendHeartbeat(SendHeartbeatRequest) returns (SendHeartbeatResponse) {}

// SendCloseSignal sends a close signal to a running workspace.
rpc SendCloseSignal(SendCloseSignalRequest) returns (SendCloseSignalResponse) {}
}

message ListWorkspacesRequest {
Expand Down Expand Up @@ -91,16 +85,6 @@ message StopWorkspaceRequest {
}
message StopWorkspaceResponse {}

message SendHeartbeatRequest {
string workspace_id = 1;
}
message SendHeartbeatResponse {}

message SendCloseSignalRequest {
string workspace_id = 1;
}
message SendCloseSignalResponse {}

////////////////////////////////
// Shared messages come here
////////////////////////////////
Expand Down
Loading

0 comments on commit cd144de

Please sign in to comment.