clean-answer-93696
09/14/2023, 1:55 PM// Get JSON from GrowthBook and deserialize it into GBFeaturesResponse struct
res, err := http.Get("<https://cdn.growthbook.io/api/features/><environment_key>")
if err != nil {
fmt.Printf("Error fetching features from GrowthBook: %s \n", err)
os.Exit(1)
}
var featuresResponse GBFeaturesResponse
err = json.NewDecoder(res.Body).Decode(&featuresResponse)
if err != nil {
fmt.Printf("Error decoding JSON: %s \n", err)
os.Exit(1)
}
features := growthbook.ParseFeatureMap(featuresResponse.Features)
brief-honey-45610
09/15/2023, 4:09 AMhappy-autumn-40938
09/15/2023, 4:57 AMhttp.Get
calls, you could wrap this in a shared service and only update it when you need to. Then you could create single SDK instances per user requests and just tap into that feature repository service.
⢠The Go SDK also supports streaming via SSE (server sent events), which basically handle the scheduled polling for you. You could adapt the strategy above to include a SSE-enabled SDK instance within your shared service and have each user request's private SDK call the shared's Features()
getter.alert-dream-63361
09/16/2023, 3:52 PMGrowthBook
instance, you can call the LoadFeatures
function on it, which starts a background goroutine that receives feature updates over an SSE connection from GrowthBook. Theoretically, you call that once (with the AutoRefresh
flag) and your features are always up to date with whatever's in the GrowthBook web app.
One caveat to that: I've just discovered that there's a bug in the Go SSE package I'm using. It doesn't reconnect properly when the GrowthBook server sends an EOF. I need to fix that, and I'm working on it right now. (This is a common problem with SSE: the server has no timely way of knowing if the client is still there, so it's pretty common to drop the server-side connection and get the client to reconnect. That should normally happen without any trouble, because SSE clients are required to deal with reconnection transparently, but the package I'm using doesn't do it, so I've had to fork it and fix it.)clean-answer-93696
09/17/2023, 5:09 AMalert-dream-63361
09/17/2023, 11:20 AM