Skip to content

Commit 2b33073

Browse files
authored
Address feature flag timeout issues (#3221)
1 parent c93230a commit 2b33073

File tree

3 files changed

+34
-25
lines changed

3 files changed

+34
-25
lines changed

cmd/docs/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ import (
66
"path/filepath"
77
"regexp"
88
"strings"
9+
"testing"
910

1011
"github.com/confluentinc/cli/v4/internal"
1112
"github.com/confluentinc/cli/v4/pkg/config"
1213
"github.com/confluentinc/cli/v4/pkg/docs"
1314
pversion "github.com/confluentinc/cli/v4/pkg/version"
15+
testserver "github.com/confluentinc/cli/v4/test/test-server"
1416
)
1517

1618
// Auto-generate documentation files for all CLI commands. Documentation uses reStructured Text (ReST) format, and is
@@ -19,6 +21,10 @@ import (
1921
// This code is adapted from https://github.com/spf13/cobra/blob/master/doc/rest_docs.md
2022

2123
func main() {
24+
// Set up test server for feature flags called by the code
25+
testBackend := testserver.StartTestCloudServer(&testing.T{}, true)
26+
defer testBackend.Close()
27+
2228
// Prevent printing the user's $HOME in docs when generating confluent local services kafka
2329
home, err := os.UserHomeDir()
2430
if err != nil {

cmd/lint/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@ import (
55
"fmt"
66
"os"
77
"strings"
8+
"testing"
89

910
"github.com/client9/gospell"
1011

1112
"github.com/confluentinc/cli/v4/internal"
1213
"github.com/confluentinc/cli/v4/pkg/config"
1314
"github.com/confluentinc/cli/v4/pkg/linter"
1415
pversion "github.com/confluentinc/cli/v4/pkg/version"
16+
testserver "github.com/confluentinc/cli/v4/test/test-server"
1517
)
1618

1719
var commandRules = []linter.CommandRule{
@@ -367,6 +369,10 @@ func init() {
367369
}
368370

369371
func main() {
372+
// Set up test server for feature flags called by the code
373+
testBackend := testserver.StartTestCloudServer(&testing.T{}, true)
374+
defer testBackend.Close()
375+
370376
flag.Parse()
371377

372378
vocab, err := gospell.NewGoSpell(affFile, dicFile)

pkg/featureflags/feature_flags.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
const (
2828
baseURL = "%s/ldapi/sdk/eval/%s/"
2929
userPath = "users/%s"
30+
timeout = 15 * time.Second
3031
)
3132

3233
const (
@@ -46,16 +47,15 @@ const (
4647
var Manager launchDarklyManager
4748

4849
type launchDarklyManager struct {
49-
cliClient *sling.Sling
50-
ccloudClient *sling.Sling
51-
Command *cobra.Command
52-
flags []string
53-
hideTimeoutWarning bool
54-
isDisabled bool
55-
timeoutWarningPrinted bool
56-
version *version.Version
57-
latestCliFlags map[string]any
58-
latestCCloudFlags map[string]any
50+
cliClient *sling.Sling
51+
ccloudClient *sling.Sling
52+
Command *cobra.Command
53+
flags []string
54+
isDisabled bool
55+
isTest bool
56+
version *version.Version
57+
latestCliFlags map[string]any
58+
latestCCloudFlags map[string]any
5959
}
6060

6161
func Init(cfg *config.Config) {
@@ -76,12 +76,11 @@ func Init(cfg *config.Config) {
7676
}
7777

7878
Manager = launchDarklyManager{
79-
cliClient: sling.New().Base(cliBasePath),
80-
ccloudClient: sling.New().Base(ccloudBasePath),
81-
hideTimeoutWarning: cfg.IsTest,
82-
isDisabled: cfg.DisableFeatureFlags,
83-
timeoutWarningPrinted: false,
84-
version: cfg.Version,
79+
cliClient: sling.New().Client(&http.Client{Timeout: timeout}).Base(cliBasePath),
80+
ccloudClient: sling.New().Client(&http.Client{Timeout: timeout}).Base(ccloudBasePath),
81+
isDisabled: cfg.DisableFeatureFlags || !cfg.IsCloudLogin(),
82+
isTest: cfg.IsTest,
83+
version: cfg.Version,
8584
}
8685
}
8786

@@ -155,7 +154,7 @@ func (ld *launchDarklyManager) generalVariation(key string, ctx *config.Context,
155154
} else if ld.areCurrentFlagsAvailable(client, key) { // if the flag is not cached but we've already retrieved the newest flag values for the current user, check those maps first
156155
flagVals = ld.getCurrentFlags(client)
157156
if shouldCache {
158-
writeFlagsToConfig(ctx, key, flagVals, user, client)
157+
ld.writeFlagsToConfig(ctx, key, flagVals, user, client)
159158
}
160159
} else {
161160
flagVals, err = ld.fetchFlags(user, client)
@@ -164,7 +163,7 @@ func (ld *launchDarklyManager) generalVariation(key string, ctx *config.Context,
164163
return defaultVal
165164
}
166165
if shouldCache {
167-
writeFlagsToConfig(ctx, key, flagVals, user, client)
166+
ld.writeFlagsToConfig(ctx, key, flagVals, user, client)
168167
}
169168
}
170169
if _, ok := flagVals[key]; ok {
@@ -197,11 +196,9 @@ func (ld *launchDarklyManager) fetchFlags(user lduser.User, client config.Launch
197196
}
198197
if err != nil {
199198
log.CliLogger.Debug(resp)
200-
if !ld.hideTimeoutWarning && !ld.timeoutWarningPrinted {
201-
output.ErrPrintln(false, "[WARN] Failed to fetch feature flags.")
202-
output.ErrPrintln(false, errors.ComposeSuggestionsMessage("Check connectivity to https://confluent.cloud or disable feature flags using `confluent configuration update disable_feature_flags true`."))
203-
ld.timeoutWarningPrinted = true
204-
}
199+
output.ErrPrintln(false, "[WARN] Failed to fetch feature flags.")
200+
output.ErrPrintln(false, errors.ComposeSuggestionsMessage("Check connectivity to https://confluent.cloud or disable feature flags using `confluent configuration update disable_feature_flags true`."))
201+
ld.isDisabled = true // disable flags for the rest of the command if any request times out
205202

206203
return flagVals, fmt.Errorf("error fetching feature flags: %w", err)
207204
}
@@ -296,8 +293,8 @@ func (ld *launchDarklyManager) contextToLDUser(ctx *config.Context) lduser.User
296293
return userBuilder.Build()
297294
}
298295

299-
func writeFlagsToConfig(ctx *config.Context, key string, vals map[string]any, user lduser.User, client config.LaunchDarklyClient) {
300-
if ctx == nil {
296+
func (ld *launchDarklyManager) writeFlagsToConfig(ctx *config.Context, key string, vals map[string]any, user lduser.User, client config.LaunchDarklyClient) {
297+
if ctx == nil || ld.isTest {
301298
return
302299
}
303300

0 commit comments

Comments
 (0)