Hey Growthbook Team, We were getting a lot of init...
# ask-questions
w
Hey Growthbook Team, We were getting a lot of initialization failure for our growthbook setup in flutter, with the error
failed to lookup host
, and wanted to implement a retry mechanism when initialization fails. GBSDKBuilder provides a failure callback but seems like there is no init retry mechanism build around failure handling, our init logic is written in a singleton pattern so creation new object if init fails doesn't seem viable either, so wanted to understand how can we tackle this problem are there any helpful inbuild methods whcih gb provides for init retry?
f
@freezing-postman-69602 can you take a look?
w
@freezing-postman-69602 any updates?
f
Just catching up on this thread now — Thank you for your patience. We do not retry the initial feature fetches as we have several other ways to reload / refresh the features both automatically and manually once the SDK is created. for eg: 1) There's a manual way to set the default features via
gbFeatures
parameter, if only the calls made at initialization are failing. Here's a sample:
Copy code
final defaultFeatures = <String, GBFeature>{
  'feature_key': GBFeature(defaultValue: true),
  // Add your fallback features
};

final sdk = await GBSDKBuilderApp(
  apiKey: 'your_api_key',
  hostURL: 'your_host_url',
  growthBookTrackingCallBack: (experiment, result) {},
  gbFeatures: defaultFeatures, // Fallback features
  onInitializationFailure: (error) {
    // Handle failure
  },
).initialize();
2) Reload Strategies - Automatic vs Manual • if you enable
backgroundSync: true
, the sdk will use SSE to update the features as and when there's a change. • Or Use the method -
refresh()
to trigger this fetch manually. Here's an example of how you can do this with an exponential back-off and a
refreshHandler
.
Copy code
GBSDKBuilderApp(
  // ... other params
  onInitializationFailure: (error) async {
    if (error?.error.toString().contains('failed to lookup host') ?? false) {
      await _retryWithBackoff();
    }
  },
).setRefreshHandler((isSuccess) {
  if (!isSuccess) {
    // Schedule retry
    Future.delayed(Duration(seconds: 5), () => sdk.refresh());
  }
}).initialize();

Future<void> _retryWithBackoff() async {
  for (int i = 0; i < 3; i++) {
    await Future.delayed(Duration(seconds: math.pow(2, i).toInt()));
    await sdk.refresh();
  }
}
I hope this helps! let me know if you've got more questions.
w
okay thanks for the response will try this out.
@freezing-postman-69602 i had a question on when does onInitializationFailure/onFailureCallback actually gets fired? and what is the life cycle of initializaton of gb vs fetching values/feature flag gb?
hey @freezing-postman-69602 any updates on it? ^^
f
I'm sorry this got buried down in the conversations. Were you able to make progress? Or need help with this?