From 711a0257fc1d0cf16a02ec326690b8a210562cf8 Mon Sep 17 00:00:00 2001 From: restitux Date: Sun, 1 Jan 2023 13:01:55 -0700 Subject: [PATCH] Changed api runner id to use a UUID --- go.mod | 1 + go.sum | 1 + pipeline_api/pipeline_api.go | 17 ++++++---------- proto/api/v1/get_runner.proto | 2 +- proto/gen/api/v1/get_runner.pb.go | 34 +++++++++++++++---------------- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/go.mod b/go.mod index 8026831..a801bc3 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/docker/docker v20.10.22+incompatible github.com/go-git/go-git/v5 v5.4.3-0.20220529141257-bc1f419cebcf github.com/go-playground/webhooks/v6 v6.0.1 + github.com/google/uuid v1.3.0 github.com/op/go-logging v0.0.0-20160315200505-970db520ece7 golang.org/x/net v0.2.0 google.golang.org/protobuf v1.28.1 diff --git a/go.sum b/go.sum index 6b43f9c..dbde6f7 100644 --- a/go.sum +++ b/go.sum @@ -668,6 +668,7 @@ github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+ github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/uuid v1.2.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/google/wire v0.3.0/go.mod h1:i1DMg/Lu8Sz5yYl25iOdmc5CT5qusaa+zmRWs16741s= github.com/googleapis/enterprise-certificate-proxy v0.0.0-20220520183353-fd19c99a87aa/go.mod h1:17drOmN3MwGY7t0e+Ei9b45FFGA3fBs3x36SsCg1hq8= diff --git a/pipeline_api/pipeline_api.go b/pipeline_api/pipeline_api.go index 2ef389f..2a8babb 100644 --- a/pipeline_api/pipeline_api.go +++ b/pipeline_api/pipeline_api.go @@ -11,6 +11,7 @@ import ( "git.ohea.xyz/cursorius/server/proto/gen/api/v1/apiv1connect" "git.ohea.xyz/cursorius/server/runnermanager" "github.com/bufbuild/connect-go" + "github.com/google/uuid" "github.com/op/go-logging" ) @@ -18,9 +19,7 @@ var log = logging.MustGetLogger("cursorius-server") type ApiServer struct { getRunnerCh chan runnermanager.GetRunnerRequest - allocatedRunners map[int64]RunnerWrapper - currentId int64 - currentIdMutex sync.Mutex + allocatedRunners map[uuid.UUID]RunnerWrapper } type RunnerWrapper struct { @@ -53,15 +52,12 @@ func (s *ApiServer) GetRunner( log.Info("Got runner with tags: %v", runnerTagsStr) - s.currentIdMutex.Lock() - runnerId := s.currentId - s.currentId++ - s.currentIdMutex.Unlock() + runnerUuid := uuid.New() - s.allocatedRunners[runnerId] = RunnerWrapper{runner: response.Runner} + s.allocatedRunners[runnerUuid] = RunnerWrapper{runner: response.Runner} res := connect.NewResponse(&apiv1.GetRunnerResponse{ - RunnerId: runnerId, + Id: runnerUuid.String(), }) res.Header().Set("GetRunner-Version", "v1") return res, nil @@ -84,8 +80,7 @@ func (s *ApiServer) RunCommand( func CreateHandler(mux *http.ServeMux, getRunnerCh chan runnermanager.GetRunnerRequest) { api_server := &ApiServer{ getRunnerCh: getRunnerCh, - allocatedRunners: make(map[int64]RunnerWrapper), - currentId: 0, + allocatedRunners: make(map[uuid.UUID]RunnerWrapper), } path, handler := apiv1connect.NewGetRunnerServiceHandler(api_server) mux.Handle(path, handler) diff --git a/proto/api/v1/get_runner.proto b/proto/api/v1/get_runner.proto index e997fa0..133fa75 100644 --- a/proto/api/v1/get_runner.proto +++ b/proto/api/v1/get_runner.proto @@ -14,7 +14,7 @@ message GetRunnerRequest { } message GetRunnerResponse { - int64 runner_id = 1; + string id = 1; } service GetRunnerService { diff --git a/proto/gen/api/v1/get_runner.pb.go b/proto/gen/api/v1/get_runner.pb.go index a49288b..89eab86 100644 --- a/proto/gen/api/v1/get_runner.pb.go +++ b/proto/gen/api/v1/get_runner.pb.go @@ -127,7 +127,7 @@ type GetRunnerResponse struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - RunnerId int64 `protobuf:"varint,1,opt,name=runner_id,json=runnerId,proto3" json:"runner_id,omitempty"` + Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"` } func (x *GetRunnerResponse) Reset() { @@ -162,11 +162,11 @@ func (*GetRunnerResponse) Descriptor() ([]byte, []int) { return file_api_v1_get_runner_proto_rawDescGZIP(), []int{2} } -func (x *GetRunnerResponse) GetRunnerId() int64 { +func (x *GetRunnerResponse) GetId() string { if x != nil { - return x.RunnerId + return x.Id } - return 0 + return "" } var File_api_v1_get_runner_proto protoreflect.FileDescriptor @@ -183,19 +183,19 @@ var file_api_v1_get_runner_proto_rawDesc = []byte{ 0x12, 0x32, 0x0a, 0x07, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x18, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x4f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x52, 0x07, 0x6f, 0x70, 0x74, - 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x30, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, - 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x1b, 0x0a, 0x09, 0x72, 0x75, 0x6e, - 0x6e, 0x65, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x03, 0x52, 0x08, 0x72, 0x75, - 0x6e, 0x6e, 0x65, 0x72, 0x49, 0x64, 0x32, 0x56, 0x0a, 0x10, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, - 0x6e, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x42, 0x0a, 0x09, 0x47, 0x65, - 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x12, 0x18, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, - 0x2e, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, - 0x74, 0x1a, 0x19, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x75, - 0x6e, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x42, 0x36, - 0x5a, 0x34, 0x67, 0x69, 0x74, 0x2e, 0x6f, 0x68, 0x65, 0x61, 0x2e, 0x78, 0x79, 0x7a, 0x2f, 0x63, - 0x75, 0x72, 0x73, 0x6f, 0x72, 0x69, 0x75, 0x73, 0x2f, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2f, - 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x61, 0x70, 0x69, 0x2f, 0x76, 0x31, - 0x3b, 0x61, 0x70, 0x69, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x23, 0x0a, 0x11, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, + 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, + 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02, 0x69, 0x64, 0x32, 0x56, 0x0a, 0x10, 0x47, 0x65, 0x74, + 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x42, 0x0a, + 0x09, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x12, 0x18, 0x2e, 0x61, 0x70, 0x69, + 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x61, 0x70, 0x69, 0x2e, 0x76, 0x31, 0x2e, 0x47, 0x65, + 0x74, 0x52, 0x75, 0x6e, 0x6e, 0x65, 0x72, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, + 0x00, 0x42, 0x36, 0x5a, 0x34, 0x67, 0x69, 0x74, 0x2e, 0x6f, 0x68, 0x65, 0x61, 0x2e, 0x78, 0x79, + 0x7a, 0x2f, 0x63, 0x75, 0x72, 0x73, 0x6f, 0x72, 0x69, 0x75, 0x73, 0x2f, 0x73, 0x65, 0x72, 0x76, + 0x65, 0x72, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x67, 0x65, 0x6e, 0x2f, 0x61, 0x70, 0x69, + 0x2f, 0x76, 0x31, 0x3b, 0x61, 0x70, 0x69, 0x76, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var (