diff --git a/internal/server/server.go b/internal/server/server.go index f991035..59d3322 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -58,7 +58,6 @@ func Serve(ctx context.Context, addr string, router.Route("/dex", func(r chi.Router) { r.Get("/alertTemplates", alertSvc.HandleListTemplates()) r.Route("/subscriptions", alertsv1.SubscriptionRoutes(sirenClient, shieldClient)) - r.Route("/optimus", optimusv1.Routes(shieldClient)) r.Route("/alerts", alertsv1.AlertRoutes(sirenClient, shieldClient)) r.Route("/optimus", optimusv1.Routes(shieldClient)) r.Route("/projects", projectsv1.Routes(shieldClient)) diff --git a/internal/server/v1/optimus/optimus_client.go b/internal/server/v1/optimus/optimus_client.go index 49b3812..e00810e 100644 --- a/internal/server/v1/optimus/optimus_client.go +++ b/internal/server/v1/optimus/optimus_client.go @@ -6,13 +6,13 @@ import ( "google.golang.org/grpc/credentials/insecure" ) -type OptimusClient struct{} - type OptimusClientBuilder interface { BuildOptimusClient(hostname string) (optimusv1beta1grpc.JobSpecificationServiceClient, error) } -func (*OptimusClient) BuildOptimusClient(hostname string) (optimusv1beta1grpc.JobSpecificationServiceClient, error) { +type clientBuilder struct{} + +func (*clientBuilder) BuildOptimusClient(hostname string) (optimusv1beta1grpc.JobSpecificationServiceClient, error) { optimusConn, err := grpc.Dial(hostname, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return nil, err diff --git a/internal/server/v1/optimus/routes.go b/internal/server/v1/optimus/routes.go index c881e63..4508a45 100644 --- a/internal/server/v1/optimus/routes.go +++ b/internal/server/v1/optimus/routes.go @@ -6,7 +6,7 @@ import ( ) func Routes(shieldClient shieldv1beta1rpc.ShieldServiceClient) func(r chi.Router) { - service := NewService(shieldClient, &OptimusClient{}) + service := NewService(shieldClient, &clientBuilder{}) handler := NewHandler(service) return func(r chi.Router) { diff --git a/internal/server/v1/optimus/service.go b/internal/server/v1/optimus/service.go index 6e55193..5429a12 100644 --- a/internal/server/v1/optimus/service.go +++ b/internal/server/v1/optimus/service.go @@ -2,6 +2,7 @@ package optimus import ( "context" + "sync" optimusv1beta1grpc "buf.build/gen/go/gotocompany/proton/grpc/go/gotocompany/optimus/core/v1beta1/corev1beta1grpc" shieldv1beta1rpc "buf.build/gen/go/gotocompany/proton/grpc/go/gotocompany/shield/v1beta1/shieldv1beta1grpc" @@ -13,15 +14,16 @@ import ( type Service struct { shieldClient shieldv1beta1rpc.ShieldServiceClient - cache *Cache builder OptimusClientBuilder + mu sync.RWMutex + data map[string]optimusv1beta1grpc.JobSpecificationServiceClient } func NewService(shieldClient shieldv1beta1rpc.ShieldServiceClient, builder OptimusClientBuilder) *Service { return &Service{ shieldClient: shieldClient, - cache: NewCache(), builder: builder, + data: make(map[string]optimusv1beta1grpc.JobSpecificationServiceClient), } } @@ -66,7 +68,7 @@ func (svc *Service) ListJobs(ctx context.Context, projectSlug string) ([]*optimu func (svc *Service) getOptimusClient(ctx context.Context, projectSlug string) (optimusv1beta1grpc.JobSpecificationServiceClient, error) { // retrieve hostname from cache - if cl, exists := svc.cache.data[projectSlug]; exists { + if cl, exists := svc.data[projectSlug]; exists { return cl, nil } // retrieve hostname from shield @@ -94,9 +96,9 @@ func (svc *Service) getOptimusClient(ctx context.Context, projectSlug string) (o } // store hostname in cache - svc.cache.mu.Lock() - svc.cache.data[projectSlug] = cl - svc.cache.mu.Unlock() + svc.mu.Lock() + svc.data[projectSlug] = cl + svc.mu.Unlock() return cl, nil }