Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample code from LaunchDarkly "Black Friday" blog post
public List<Comment> getProductComments(Product product) {
// The app contains its own singleton service to hold one instance
// of the LaunchDarkly client.
ldClient = LaunchDarklyService.getClient();
ldUser = LaunchDarklyService.getUserFromContext(context);
boolean showFeature = ldClient.boolVariation("show-comments", user, false);
// If the feature is turned off, just return an empty list of comments
If (!showFeature) {
return Collections.emptyList();
}
// The rest of this method, containing heavy SQL queries, goes here...
}
const LIMIT_FLAG_KEY = "throttle_api_reqs";
const limit_on = ldClient.variation(LIMIT_FLAG_KEY, {}, false);
const limiter = new Bottleneck({ maxConcurrent: limit_on ? 1000 : 10000 });
// First, check whether this request is coming from the USA
var ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
var req_from_USA = GeoLookup.country(ip) == "USA";
// Keep a reference to the existing function...
var oldQueryInventory = queryInventory;
// ... then put a new function in its place (which calls the existing one)
var queryInventory = function(query) {
var options = {
// Give requests from the USA a higher priority
priority: req_from_USA ? 1 : 9, // lower number = higher priority
// Don't let any request hang around for more than 10 seconds.
expiration: 10 * 1000, // set in milliseconds
};
return limiter.schedule(options, oldQueryInventory, query);
};
// Subscribe to change events from the named flag...
ldClient.on("update:" + LIMIT_FLAG_KEY, function(oldValue, newValue) {
// ... and use them to update the limiter configuration.
console.log(LIMIT_FLAG_KEY + " changed:", newValue, "(" + oldValue + ")");
limiter.updateSettings({ maxConcurrent: newValue ? 100 : 10000 });
});
<!-- Single-line “if” doesn’t need closing -->
<?php if $this->checkFlag(“analytics-embed”, false): ?>
<script src=... ></script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment