From 58238b0ead34234435aa0f86275bfd5d532108fe Mon Sep 17 00:00:00 2001 From: Luigi Date: Sun, 10 Mar 2024 16:12:44 +0100 Subject: [PATCH] chore: add typesense client config, remove duplicate, improve error handling --- internal/container/container.go | 19 +++++++++++++++---- internal/x/typesense/schema.go | 8 +++++++- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/internal/container/container.go b/internal/container/container.go index 2bb181c..f4f32c8 100644 --- a/internal/container/container.go +++ b/internal/container/container.go @@ -1,7 +1,9 @@ package container import ( + "context" "net/http" + "time" "go.uber.org/zap" @@ -106,9 +108,7 @@ func (c *Container) GetFeedRepository() app.FeedRepository { return c.feedRepository } - client := typesense.NewClient( - typesense.WithServer(c.FeedRepositoryHost), - typesense.WithAPIKey(c.FeedRepositoryKey)) + client := c.GetTypesenseClient() c.feedRepository = typesenseRepo.NewFeedRepository(client) @@ -120,9 +120,20 @@ func (c *Container) GetTypesenseClient() *typesense.Client { return c.typesenseClient } + typesenseConnTimeout := 1 * time.Hour + + // TODO: Export it in a x/typesense pkg as wrapper with check config logic client := typesense.NewClient( typesense.WithServer(c.FeedRepositoryHost), - typesense.WithAPIKey(c.FeedRepositoryKey)) + typesense.WithAPIKey(c.FeedRepositoryKey), + typesense.WithConnectionTimeout(typesenseConnTimeout), + typesense.WithCircuitBreakerInterval(typesenseConnTimeout), + ) + + c.GetLogger().Info("Waiting typesense healthcheck...") + if _, err := client.Health(context.Background(), typesenseConnTimeout); err != nil { + panic(err) + } c.typesenseClient = client diff --git a/internal/x/typesense/schema.go b/internal/x/typesense/schema.go index a1ca5f5..3454e90 100644 --- a/internal/x/typesense/schema.go +++ b/internal/x/typesense/schema.go @@ -18,6 +18,7 @@ func CreateOrUpdateCollection(client *typesense.Client, schema *api.CollectionSc if strings.Contains(err.Error(), "already exists") { return updateCollection(client, schema) } + return err } return nil @@ -28,7 +29,12 @@ func updateCollection(client *typesense.Client, schema *api.CollectionSchema) er Fields: schema.Fields, } - if _, err := client.Collection(schema.Name).Update(context.Background(), u); err != nil{ + if _, err := client.Collection(schema.Name).Update(context.Background(), u); err != nil { + if strings.Contains(err.Error(), "is already part of the schema") { + // TODO: capture the log of error + return nil + } + return err }