Skip to content
This repository has been archived by the owner on Oct 25, 2023. It is now read-only.

Commit

Permalink
fix(optimus): removed cache struct
Browse files Browse the repository at this point in the history
  • Loading branch information
Sudheer Pal committed Sep 27, 2023
1 parent 006eb2c commit 5d2c6a7
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 11 deletions.
1 change: 0 additions & 1 deletion internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
6 changes: 3 additions & 3 deletions internal/server/v1/optimus/optimus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/server/v1/optimus/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 8 additions & 6 deletions internal/server/v1/optimus/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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),
}
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

0 comments on commit 5d2c6a7

Please sign in to comment.