कम्पोज़ेबल आर्किटेक्चर (TCA, शॉर्ट के लिए) एक संगत में अनुप्रयोगों के निर्माण के लिए एक लाइब्रेरी है और समझने योग्य तरीका, रचना, परीक्षण और एर्गोनॉमिक्स को ध्यान में रखते हुए। इसमें इस्तेम
This file contains 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
extension UIView { | |
func startShimmering(){ | |
let light = UIColor.white.cgColor | |
let alpha = UIColor.white.withAlphaComponent(0.7).cgColor | |
let gradient = CAGradientLayer() | |
gradient.colors = [alpha, light, alpha, alpha, light, alpha] | |
gradient.frame = CGRect(x: -self.bounds.size.width, y: 0, width: 3 * self.bounds.size.width, height: self.bounds.size.height) | |
gradient.startPoint = CGPoint(x: 0.0, y: 0.5) |
This file contains 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
/// RepeatingTimer mimics the API of DispatchSourceTimer but in a way that prevents | |
/// crashes that occur from calling resume multiple times on a timer that is | |
/// already resumed (noted by https://github.com/SiftScience/sift-ios/issues/52 | |
class RepeatingTimer { | |
let timeInterval: TimeInterval | |
init(timeInterval: TimeInterval) { | |
self.timeInterval = timeInterval | |
} |
The following are examples of the four types rate limiters discussed in the accompanying blog post. In the examples below I've used pseudocode-like Ruby, so if you're unfamiliar with Ruby you should be able to easily translate this approach to other languages. Complete examples in Ruby are also provided later in this gist.
In most cases you'll want all these examples to be classes, but I've used simple functions here to keep the code samples brief.
This uses a basic token bucket algorithm and relies on the fact that Redis scripts execute atomically. No other operations can run between fetching the count and writing the new count.
This file contains 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
Latency Comparison Numbers (~2012) | |
---------------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns 3 us | |
Send 1K bytes over 1 Gbps network 10,000 ns 10 us | |
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD |
This file contains 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
extern crate nom; | |
use nom::{ | |
IResult, | |
character::complete::{ | |
multispace0, | |
char, | |
digit1, | |
}, | |
branch::{ |
This file contains 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
// time: [1.8843 µs 1.8897 µs 1.8955 µs] | |
pub fn original(input: &[u8]) -> Option<usize> { | |
let mut idx = 0; | |
'outer: while idx + 13 < input.len() { | |
let mut state = 0; | |
for (next_idx, byte) in input[idx..idx + 14].iter().enumerate().rev() { | |
let bit_idx = byte % 32; | |
if state & (1 << bit_idx) != 0 { | |
idx += next_idx + 1; | |
continue 'outer; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.