Skip to content

Commit 68f8493

Browse files
committed
Make first feature flag call blocking
- First connection now creates cache context and fetches from server - Ensures telemetry respects actual server flag on initial connection - Subsequent connections remain non-blocking with cached value - Double-checked locking pattern to prevent race conditions
1 parent 05f4ea2 commit 68f8493

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

telemetry/featureflag.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,37 @@ func (c *featureFlagCache) getFeatureFlag(ctx context.Context, host string, http
9595
flagCtx, exists := c.contexts[host]
9696
c.mu.RUnlock()
9797

98+
// If context doesn't exist, create it and make initial blocking fetch
9899
if !exists {
99-
return false, nil
100+
c.mu.Lock()
101+
// Double-check after acquiring write lock
102+
flagCtx, exists = c.contexts[host]
103+
if !exists {
104+
flagCtx = &featureFlagContext{
105+
cacheDuration: featureFlagCacheDuration,
106+
fetching: true, // Mark as fetching
107+
}
108+
c.contexts[host] = flagCtx
109+
}
110+
c.mu.Unlock()
111+
112+
// If we just created the context, make the initial blocking fetch
113+
if !exists {
114+
flags, err := fetchFeatureFlags(ctx, host, httpClient)
115+
116+
flagCtx.mu.Lock()
117+
flagCtx.fetching = false
118+
if err == nil {
119+
flagCtx.flags = flags
120+
flagCtx.lastFetched = time.Now()
121+
result := flags[flagName]
122+
flagCtx.mu.Unlock()
123+
return result, nil
124+
}
125+
// On error for first fetch, fail-safe: return false (telemetry disabled)
126+
flagCtx.mu.Unlock()
127+
return false, nil
128+
}
100129
}
101130

102131
// Check if cache is valid (with proper locking)

0 commit comments

Comments
 (0)