Created
December 2, 2019 20:17
-
-
Save yozlet/193cac91749f6524d5975674036a9d2e to your computer and use it in GitHub Desktop.
Sample code from LaunchDarkly "Black Friday" blog post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 }); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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