Skip to content

Commit

Permalink
fix: Support TIGRIS_URI environment variable
Browse files Browse the repository at this point in the history
  • Loading branch information
efirs committed May 18, 2023
1 parent 3a61850 commit d090e80
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
11 changes: 10 additions & 1 deletion driver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ func TestDriverConfigPrecedence(t *testing.T) {
// default
assert.Equal(t, config.Driver{URL: "api.preview.tigrisdata.cloud", Protocol: DefaultProtocol}, *res)

t.Setenv(EnvURI, "host2:234")

res, err = initConfig(&cfg)
assert.NoError(t, err)
assert.Equal(t, config.Driver{
URL: "host2:234",
Protocol: GRPC,
}, *res)

// env have precedence over default
t.Setenv(EnvToken, "token1")
t.Setenv(EnvClientID, "client1")
Expand All @@ -133,7 +142,7 @@ func TestDriverConfigPrecedence(t *testing.T) {
TLS: cTLS,
}, *res)

// config have precedence over config
// config have precedence over env
cfg.URL = "url.config"
cfg.ClientID = "client_id2"
cfg.ClientSecret = "client_secret2"
Expand Down
16 changes: 15 additions & 1 deletion driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -471,11 +471,17 @@ func initConfig(lCfg *config.Driver) (*config.Driver, error) {
if cfg.URL == "" {
cfg.URL = os.Getenv(EnvURL)
}

if cfg.URL == "" {
cfg.URL = os.Getenv(EnvURI)
}

if cfg.URL == "" {
cfg.URL = DefaultURL
}

sURL := cfg.URL

noScheme := !strings.Contains(sURL, "://")
if noScheme {
if DefaultProtocol == "" {
Expand All @@ -495,6 +501,7 @@ func initConfig(lCfg *config.Driver) (*config.Driver, error) {
}

var sec bool

if sec, err = initProto(u.Scheme, &cfg); err != nil {
return nil, err
}
Expand All @@ -511,7 +518,7 @@ func initConfig(lCfg *config.Driver) (*config.Driver, error) {
return &cfg, nil
}

type initDriverFunc func(ctx context.Context, config *config.Driver) (driverWithOptions, Management, Observability, error)
type initDriverFunc func(ctx context.Context, cfg *config.Driver) (driverWithOptions, Management, Observability, error)

var drivers = map[string]initDriverFunc{}

Expand Down Expand Up @@ -541,14 +548,17 @@ func NewDriver(ctx context.Context, cfg *config.Driver) (Driver, error) {
}

wg, ch := startHealthPingLoop(cfg.PingInterval, drv)

return &driver{driverWithOptions: drv, closeCh: ch, closeWg: wg, cfg: cfg}, nil
}

func startHealthPingLoop(cfgInterval time.Duration, drv driverWithOptions) (*sync.WaitGroup, chan struct{}) {
var wg sync.WaitGroup

ch := make(chan struct{})

wg.Add(1)

go func() {
defer wg.Done()

Expand All @@ -566,11 +576,15 @@ func startHealthPingLoop(cfgInterval time.Duration, drv driverWithOptions) (*syn
case <-ch:
return
}

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)

_, _ = drv.Health(ctx)

cancel()
}
}()

return &wg, ch
}

Expand Down
12 changes: 0 additions & 12 deletions driver/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,18 +120,6 @@ func testSearchBasic(t *testing.T, c Driver, mc *mock.MockSearchServer) {
require.Equal(t, &Error{&api.TigrisError{Code: api.Code_DATA_LOSS, Message: "error_stream"}}, sit.Err())

sit.Close()

mc.EXPECT().Search(pm(sReqExp), gomock.Any()).
DoAndReturn(func(r *api.SearchIndexRequest, srv api.Search_SearchServer) error {
return nil
})

sit, err = search.Search(ctx, "c1", sReq)
require.NoError(t, err)

sit.Close()

require.False(t, sit.Next(&r))
})

t.Run("create_or_update_index", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions driver/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
EnvToken = "TIGRIS_TOKEN" //nolint:golint,gosec
EnvProtocol = "TIGRIS_PROTOCOL"
EnvURL = "TIGRIS_URL"
EnvURI = "TIGRIS_URI"
EnvProject = "TIGRIS_PROJECT"
EnvDBBranch = "TIGRIS_DB_BRANCH"

Expand Down

0 comments on commit d090e80

Please sign in to comment.