From 8ed295c1fb5866ecadedb855166ed6df49cf4d25 Mon Sep 17 00:00:00 2001 From: Na'aman Hirschfeld Date: Mon, 27 Nov 2023 20:25:31 +0100 Subject: [PATCH] chore: cleanup grpc init and add redis name --- go.sum | 1 - .../internal/connectors/connectors_test.go | 16 ---------------- .../internal/connectors/openai/openai_test.go | 2 -- .../internal/services/integration_test.go | 2 -- services/api-gateway/main.go | 3 +-- .../dashboard-backend/internal/api/ws_test.go | 2 -- .../ptestingclient/ptestingclient_test.go | 6 ------ services/dashboard-backend/main.go | 6 ------ shared/go/rediscache/rediscache.go | 3 +++ 9 files changed, 4 insertions(+), 37 deletions(-) diff --git a/go.sum b/go.sum index 6b0ff0b2..07b56762 100644 --- a/go.sum +++ b/go.sum @@ -659,7 +659,6 @@ github.com/cenkalti/backoff/v4 v4.2.1/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyY github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.3.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/census-instrumentation/opencensus-proto v0.4.1/go.mod h1:4T9NM4+4Vw91VeyqjLS6ao50K5bOcLKN6Q42XnYaRYw= -github.com/cespare/xxhash v1.1.0 h1:a6HrQnmkObjyL+Gs60czilIUGqrzKutQD6XZog3p+ko= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= diff --git a/services/api-gateway/internal/connectors/connectors_test.go b/services/api-gateway/internal/connectors/connectors_test.go index f8decace..6e786cc8 100644 --- a/services/api-gateway/internal/connectors/connectors_test.go +++ b/services/api-gateway/internal/connectors/connectors_test.go @@ -6,8 +6,6 @@ import ( "github.com/basemind-ai/monorepo/shared/go/db/models" "github.com/basemind-ai/monorepo/shared/go/testutils" "github.com/stretchr/testify/assert" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "testing" ) @@ -19,24 +17,11 @@ func TestGetOpenAIConnectorClientPanicsWithoutAddress(t *testing.T) { assert.Panics(t, func() { connectors.GetProviderConnector(models.ModelVendorOPENAI) }) }) - t.Run("panics when transport security is not set", func(t *testing.T) { - t.Setenv("OPENAI_CONNECTOR_ADDRESS", "localhost:50051") - - assert.Panics(t, func() { - connectors.Init( - context.TODO(), - ) - - connectors.GetProviderConnector(models.ModelVendorOPENAI) - }) - }) - t.Run("does not panic when initialized", func(t *testing.T) { t.Setenv("OPENAI_CONNECTOR_ADDRESS", "localhost:50051") connectors.Init( context.TODO(), - grpc.WithTransportCredentials(insecure.NewCredentials()), ) assert.NotPanics( t, @@ -49,7 +34,6 @@ func TestGetOpenAIConnectorClientPanicsWithoutAddress(t *testing.T) { connectors.Init( context.TODO(), - grpc.WithTransportCredentials(insecure.NewCredentials()), ) assert.Panics( diff --git a/services/api-gateway/internal/connectors/openai/openai_test.go b/services/api-gateway/internal/connectors/openai/openai_test.go index 567383a0..6760147c 100644 --- a/services/api-gateway/internal/connectors/openai/openai_test.go +++ b/services/api-gateway/internal/connectors/openai/openai_test.go @@ -6,7 +6,6 @@ import ( "github.com/basemind-ai/monorepo/services/api-gateway/internal/connectors/openai" "github.com/basemind-ai/monorepo/shared/go/testutils" "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "net" "testing" ) @@ -27,7 +26,6 @@ func CreateClientAndService(t *testing.T) (*openai.Client, *testutils.MockOpenAI ) client := openai.New( "", - grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer( func(context.Context, string) (net.Conn, error) { return listener.Dial() diff --git a/services/api-gateway/internal/services/integration_test.go b/services/api-gateway/internal/services/integration_test.go index 3f9bff88..3ffb14fe 100644 --- a/services/api-gateway/internal/services/integration_test.go +++ b/services/api-gateway/internal/services/integration_test.go @@ -19,7 +19,6 @@ import ( "github.com/go-redis/redismock/v9" "github.com/stretchr/testify/assert" "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/metadata" "io" "net" @@ -47,7 +46,6 @@ func createOpenAIService(t *testing.T) *testutils.MockOpenAIService { t.Setenv("OPENAI_CONNECTOR_ADDRESS", "") connectors.Init(context.TODO(), - grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer( func(context.Context, string) (net.Conn, error) { return listener.Dial() diff --git a/services/api-gateway/main.go b/services/api-gateway/main.go index 0a0b5294..a6195803 100644 --- a/services/api-gateway/main.go +++ b/services/api-gateway/main.go @@ -15,7 +15,6 @@ import ( "github.com/rs/zerolog/log" "golang.org/x/sync/errgroup" "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "net" "os" "os/signal" @@ -38,7 +37,7 @@ func main() { logging.Configure(cfg.Environment != "production") // FIXME: this is a temporary work-around for testing - connectors.Init(ctx, grpc.WithTransportCredentials(insecure.NewCredentials())) + connectors.Init(ctx) rediscache.New(cfg.RedisURL) diff --git a/services/dashboard-backend/internal/api/ws_test.go b/services/dashboard-backend/internal/api/ws_test.go index 41d6d827..85cd84b9 100644 --- a/services/dashboard-backend/internal/api/ws_test.go +++ b/services/dashboard-backend/internal/api/ws_test.go @@ -22,7 +22,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "net" "net/http" "net/http/httptest" @@ -91,7 +90,6 @@ func createMockGRPCServer( ) client := ptestingclient.New( "", - grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer( func(context.Context, string) (net.Conn, error) { return listener.Dial() diff --git a/services/dashboard-backend/internal/ptestingclient/ptestingclient_test.go b/services/dashboard-backend/internal/ptestingclient/ptestingclient_test.go index 1d2325d8..40182689 100644 --- a/services/dashboard-backend/internal/ptestingclient/ptestingclient_test.go +++ b/services/dashboard-backend/internal/ptestingclient/ptestingclient_test.go @@ -14,7 +14,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/mock" "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "net" "testing" ) @@ -37,7 +36,6 @@ func createClientAndService( ) client := ptestingclient.New( "", - grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer( func(context.Context, string) (net.Conn, error) { return listener.Dial() @@ -92,7 +90,6 @@ func TestPromptTestingGRPCClient(t *testing.T) { t.Run("dials and returns client", func(t *testing.T) { client := ptestingclient.New( "localhost:50051", - grpc.WithTransportCredentials(insecure.NewCredentials()), ) assert.NotNil(t, client) }) @@ -104,7 +101,6 @@ func TestPromptTestingGRPCClient(t *testing.T) { func() { ptestingclient.Init( context.Background(), - grpc.WithTransportCredentials(insecure.NewCredentials()), ) }, ) @@ -115,7 +111,6 @@ func TestPromptTestingGRPCClient(t *testing.T) { func() { ptestingclient.Init( context.Background(), - grpc.WithTransportCredentials(insecure.NewCredentials()), ) }, ) @@ -127,7 +122,6 @@ func TestPromptTestingGRPCClient(t *testing.T) { t.Setenv("API_GATEWAY_ADDRESS", "localhost:50051") ptestingclient.Init( context.Background(), - grpc.WithTransportCredentials(insecure.NewCredentials()), ) assert.NotPanics(t, func() { ptestingclient.GetClient() diff --git a/services/dashboard-backend/main.go b/services/dashboard-backend/main.go index db348508..0f5a3409 100644 --- a/services/dashboard-backend/main.go +++ b/services/dashboard-backend/main.go @@ -5,14 +5,11 @@ import ( "fmt" "github.com/basemind-ai/monorepo/services/dashboard-backend/internal/api" "github.com/basemind-ai/monorepo/services/dashboard-backend/internal/middleware" - "github.com/basemind-ai/monorepo/services/dashboard-backend/internal/ptestingclient" "github.com/basemind-ai/monorepo/shared/go/config" "github.com/basemind-ai/monorepo/shared/go/db" "github.com/basemind-ai/monorepo/shared/go/logging" "github.com/basemind-ai/monorepo/shared/go/rediscache" "github.com/basemind-ai/monorepo/shared/go/router" - "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "net/http" "os" "os/signal" @@ -40,9 +37,6 @@ func main() { logging.Configure(cfg.Environment != "production") - // FIXME: this is a temporary work-around for testing - ptestingclient.Init(ctx, grpc.WithTransportCredentials(insecure.NewCredentials())) - rediscache.New(cfg.RedisURL) conn, connErr := db.CreateConnection(ctx, cfg.DatabaseURL) diff --git a/shared/go/rediscache/rediscache.go b/shared/go/rediscache/rediscache.go index 3a6ac0aa..c47ee8be 100644 --- a/shared/go/rediscache/rediscache.go +++ b/shared/go/rediscache/rediscache.go @@ -6,6 +6,7 @@ import ( "github.com/basemind-ai/monorepo/shared/go/exc" "github.com/go-redis/cache/v9" "github.com/redis/go-redis/v9" + "os" "sync" "time" ) @@ -25,6 +26,8 @@ func SetClient(c *cache.Cache) { func New(redisURL string) *cache.Cache { once.Do(func() { opt := exc.MustResult(redis.ParseURL(redisURL)) + + opt.ClientName = os.Getenv("SERVICE_NAME") SetClient(cache.New(&cache.Options{ Redis: redis.NewClient(opt), LocalCache: cache.NewTinyLFU(1000, time.Minute),