Skip to content

Commit

Permalink
chore: add typesense client config, remove duplicate, improve error h…
Browse files Browse the repository at this point in the history
…andling
  • Loading branch information
luigibarbato committed Mar 10, 2024
1 parent b489d65 commit 58238b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
19 changes: 15 additions & 4 deletions internal/container/container.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package container

import (
"context"
"net/http"
"time"

"go.uber.org/zap"

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

Expand All @@ -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

Expand Down
8 changes: 7 additions & 1 deletion internal/x/typesense/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 58238b0

Please sign in to comment.