r4r 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +110 -0
- data/.gitignore +12 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +39 -0
- data/LICENSE.txt +21 -0
- data/README.md +15 -0
- data/Rakefile +38 -0
- data/bin/console +14 -0
- data/bin/ghpages +19 -0
- data/bin/rake +2 -0
- data/bin/setup +9 -0
- data/ext/r4r/ring_bits_ext/extconf.rb +7 -0
- data/ext/r4r/ring_bits_ext/ring_bits_ext.c +173 -0
- data/ext/r4r/system_clock_ext/extconf.rb +8 -0
- data/ext/r4r/system_clock_ext/system_clock_ext.c +31 -0
- data/lib/r4r.rb +17 -0
- data/lib/r4r/clock.rb +40 -0
- data/lib/r4r/retry.rb +136 -0
- data/lib/r4r/retry_budget.rb +150 -0
- data/lib/r4r/retry_policy.rb +55 -0
- data/lib/r4r/ring_bits.rb +59 -0
- data/lib/r4r/token_bucket.rb +129 -0
- data/lib/r4r/version.rb +3 -0
- data/lib/r4r/windowed_adder.rb +106 -0
- data/r4r.gemspec +43 -0
- metadata +170 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 503aaa9fc4fa100b6bb5e2962146523ebe0141e2
|
4
|
+
data.tar.gz: 5c62f478f3e0b8c8e9974bea14ce0649489298a1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3b54ded1917f54d95a5d16519ddf0c60efd41c744dde4587905fe4b3995a5b35ccfdc0b5d5dfedef28532d9b639b42b47bec4aa2e47669169d1fbd43abc9576
|
7
|
+
data.tar.gz: ae5ed5bc6472c53519b1fda0b506476c21e29f8f9b3e8964938d1603a32c191880ccd431fab60568144aaf95693715295f69e9053a72aa1e07c3402838040159
|
@@ -0,0 +1,110 @@
|
|
1
|
+
version: 2
|
2
|
+
|
3
|
+
shared_ghpages: &shared_ghpages
|
4
|
+
working_directory: ~/repo
|
5
|
+
environment:
|
6
|
+
TERM: dump
|
7
|
+
|
8
|
+
steps:
|
9
|
+
- checkout
|
10
|
+
# Download and cache dependencies
|
11
|
+
- restore_cache:
|
12
|
+
keys:
|
13
|
+
- v1-dependencies-{{ .Environment.CIRCLE_STAGE }}-{{ checksum "Gemfile.lock" }}
|
14
|
+
- v1-dependencies-
|
15
|
+
|
16
|
+
- run:
|
17
|
+
name: install dependencies
|
18
|
+
command: |
|
19
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
20
|
+
|
21
|
+
- save_cache:
|
22
|
+
paths:
|
23
|
+
- ./vendor/bundle
|
24
|
+
key: v1-dependencies-{{ .Environment.CIRCLE_STAGE }}-{{ checksum "Gemfile.lock" }}
|
25
|
+
|
26
|
+
- add-ssh-keys:
|
27
|
+
fingerprints:
|
28
|
+
- "af:38:77:24:f4:c6:33:f2:74:60:e5:cc:76:0e:a7:ad"
|
29
|
+
|
30
|
+
- run:
|
31
|
+
name: publish gh-pages
|
32
|
+
command: |
|
33
|
+
git config --global user.email "ghpages@dmexe.me"
|
34
|
+
git config --global user.name $CIRCLE_USERNAME
|
35
|
+
bin/ghpages
|
36
|
+
|
37
|
+
shared_ruby_build: &shared_ruby_build
|
38
|
+
working_directory: ~/repo
|
39
|
+
environment:
|
40
|
+
TERM: dump
|
41
|
+
|
42
|
+
steps:
|
43
|
+
- checkout
|
44
|
+
# Download and cache dependencies
|
45
|
+
- restore_cache:
|
46
|
+
keys:
|
47
|
+
- v1-dependencies-{{ .Environment.CIRCLE_STAGE }}-{{ checksum "Gemfile.lock" }}
|
48
|
+
- v1-dependencies-
|
49
|
+
|
50
|
+
- run:
|
51
|
+
name: install dependencies
|
52
|
+
command: |
|
53
|
+
bundle install --jobs=4 --retry=3 --path vendor/bundle
|
54
|
+
|
55
|
+
- save_cache:
|
56
|
+
paths:
|
57
|
+
- ./vendor/bundle
|
58
|
+
key: v1-dependencies-{{ .Environment.CIRCLE_STAGE }}-{{ checksum "Gemfile.lock" }}
|
59
|
+
|
60
|
+
- run:
|
61
|
+
name: compile
|
62
|
+
command: |
|
63
|
+
bin/rake compile
|
64
|
+
|
65
|
+
- run:
|
66
|
+
name: run tests
|
67
|
+
command: |
|
68
|
+
bin/rake test TESTOPTS="--ci-dir=$CIRCLE_TEST_REPORTS/reports"
|
69
|
+
|
70
|
+
- run:
|
71
|
+
name: run bench
|
72
|
+
command: |
|
73
|
+
bin/rake bench
|
74
|
+
|
75
|
+
- store_test_results:
|
76
|
+
path: /reports
|
77
|
+
|
78
|
+
- store_artifacts:
|
79
|
+
path: /reports
|
80
|
+
destination: test-results
|
81
|
+
|
82
|
+
jobs:
|
83
|
+
ruby22:
|
84
|
+
<<: *shared_ruby_build
|
85
|
+
docker:
|
86
|
+
- image: ruby:2.2
|
87
|
+
ruby24:
|
88
|
+
<<: *shared_ruby_build
|
89
|
+
docker:
|
90
|
+
- image: ruby:2.4
|
91
|
+
ghpages:
|
92
|
+
<<: *shared_ghpages
|
93
|
+
docker:
|
94
|
+
- image: ruby:2.4
|
95
|
+
|
96
|
+
workflows:
|
97
|
+
version: 2
|
98
|
+
build-test-deploy:
|
99
|
+
jobs:
|
100
|
+
- ruby22
|
101
|
+
- ruby24
|
102
|
+
- ghpages:
|
103
|
+
requires:
|
104
|
+
- ruby22
|
105
|
+
- ruby24
|
106
|
+
filters:
|
107
|
+
branches:
|
108
|
+
only: master
|
109
|
+
|
110
|
+
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
r4r (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
ansi (1.5.0)
|
10
|
+
builder (3.2.3)
|
11
|
+
minitest (5.10.3)
|
12
|
+
minitest-ci (3.3.0)
|
13
|
+
minitest (>= 5.0.6)
|
14
|
+
minitest-reporters (1.1.19)
|
15
|
+
ansi
|
16
|
+
builder
|
17
|
+
minitest (>= 5.0)
|
18
|
+
ruby-progressbar
|
19
|
+
rake (11.3.0)
|
20
|
+
rake-compiler (1.0.4)
|
21
|
+
rake
|
22
|
+
ruby-progressbar (1.9.0)
|
23
|
+
yard (0.9.12)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.15)
|
30
|
+
minitest (~> 5.0)
|
31
|
+
minitest-ci (~> 3.3)
|
32
|
+
minitest-reporters (~> 1.1)
|
33
|
+
r4r!
|
34
|
+
rake (~> 11.0)
|
35
|
+
rake-compiler (~> 1.0)
|
36
|
+
yard (~> 0.9)
|
37
|
+
|
38
|
+
BUNDLED WITH
|
39
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Dmitry Galinsky
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require "rake/testtask"
|
3
|
+
|
4
|
+
Rake::TestTask.new(:test) do |t|
|
5
|
+
t.libs << "test"
|
6
|
+
t.libs << "lib"
|
7
|
+
t.test_files = FileList["test/**/*_test.rb"]
|
8
|
+
end
|
9
|
+
|
10
|
+
Rake::TestTask.new(:bench) do |t|
|
11
|
+
t.libs << "test"
|
12
|
+
t.libs << "lib"
|
13
|
+
t.test_files = FileList["test/**/benchmark_*.rb"]
|
14
|
+
end
|
15
|
+
|
16
|
+
task :default => :test
|
17
|
+
|
18
|
+
require 'rake/extensiontask'
|
19
|
+
spec = Gem::Specification.load('r4r.gemspec')
|
20
|
+
|
21
|
+
Dir.glob("ext/r4r/*").each do |dirname|
|
22
|
+
extname = File.basename dirname
|
23
|
+
|
24
|
+
Rake::ExtensionTask.new do |ext|
|
25
|
+
ext.name = extname
|
26
|
+
ext.ext_dir = dirname
|
27
|
+
ext.lib_dir = 'lib/r4r'
|
28
|
+
ext.gem_spec = spec
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
require 'yard'
|
34
|
+
YARD::Rake::YardocTask.new do |t|
|
35
|
+
t.files = ['lib/**/*.rb', 'ext/**/*.c']
|
36
|
+
t.options = ['-r', 'README.md', '-o', 'docs']
|
37
|
+
t.stats_options = ['--list-undoc']
|
38
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "r4r"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/ghpages
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
#!/bin/bash
|
2
|
+
|
3
|
+
set -ex
|
4
|
+
|
5
|
+
git diff-index --quiet HEAD -- || (
|
6
|
+
echo "Uncommitted changes detected"
|
7
|
+
exit 1
|
8
|
+
)
|
9
|
+
|
10
|
+
bin/rake yard
|
11
|
+
|
12
|
+
git add -f docs
|
13
|
+
git commit -m 'add /docs'
|
14
|
+
|
15
|
+
git push origin `git subtree split --prefix docs HEAD`:gh-pages --force
|
16
|
+
git reset --hard HEAD~1
|
17
|
+
|
18
|
+
echo ""
|
19
|
+
echo "DONE"
|
data/bin/rake
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,173 @@
|
|
1
|
+
#include <ruby.h>
|
2
|
+
#include <stdint.h>
|
3
|
+
#include <stdio.h>
|
4
|
+
#include <stdbool.h>
|
5
|
+
#include <assert.h>
|
6
|
+
|
7
|
+
#define ADDRESS_BITS_PER_WORD 6
|
8
|
+
|
9
|
+
struct ring_bits_ext {
|
10
|
+
size_t size; //the size of the ring bit set
|
11
|
+
uint64_t * words;
|
12
|
+
};
|
13
|
+
|
14
|
+
/**
|
15
|
+
* Free resources allocated for ring_bits_ext.
|
16
|
+
*/
|
17
|
+
static void
|
18
|
+
ring_bits_ext_free(void *p) {
|
19
|
+
struct ring_bits_ext *ptr = p;
|
20
|
+
if (ptr->size > 0) {
|
21
|
+
free(ptr->words);
|
22
|
+
}
|
23
|
+
}
|
24
|
+
|
25
|
+
/**
|
26
|
+
* Given a bit index, return word index containing it.
|
27
|
+
*/
|
28
|
+
size_t
|
29
|
+
ring_bits_ext_word_index(int bit_index) {
|
30
|
+
return bit_index >> ADDRESS_BITS_PER_WORD;
|
31
|
+
}
|
32
|
+
|
33
|
+
/**
|
34
|
+
* Allocates resources for a ring_bits_ext.
|
35
|
+
*/
|
36
|
+
static VALUE
|
37
|
+
ring_bits_ext_alloc(VALUE klass) {
|
38
|
+
VALUE obj;
|
39
|
+
struct ring_bits_ext *ptr;
|
40
|
+
|
41
|
+
obj = Data_Make_Struct(klass, struct ring_bits_ext, NULL, ring_bits_ext_free, ptr);
|
42
|
+
ptr->size = 0;
|
43
|
+
ptr->words = NULL;
|
44
|
+
|
45
|
+
return obj;
|
46
|
+
}
|
47
|
+
|
48
|
+
/**
|
49
|
+
* Inits a new ring_bits_ext object.
|
50
|
+
*
|
51
|
+
* @param [Fixnum] capacity a ring bits buffer size.
|
52
|
+
* @raise [ArgumentError] if capacity is negative
|
53
|
+
*/
|
54
|
+
static VALUE
|
55
|
+
ring_bits_ext_init(VALUE self, VALUE capacity) {
|
56
|
+
struct ring_bits_ext *ptr;
|
57
|
+
int ring_bits_ext_size = NUM2INT(capacity);
|
58
|
+
|
59
|
+
if (0 >= ring_bits_ext_size) {
|
60
|
+
rb_raise(rb_eArgError, "ring bit's size must be positive, got %i", ring_bits_ext_size);
|
61
|
+
return Qnil;
|
62
|
+
}
|
63
|
+
|
64
|
+
size_t count_of_words_required = ring_bits_ext_word_index(ring_bits_ext_size - 1) + 1;
|
65
|
+
|
66
|
+
Data_Get_Struct(self, struct ring_bits_ext, ptr);
|
67
|
+
ptr->size = count_of_words_required << ADDRESS_BITS_PER_WORD;
|
68
|
+
ptr->words = calloc(count_of_words_required, sizeof(uint64_t));
|
69
|
+
|
70
|
+
return self;
|
71
|
+
}
|
72
|
+
|
73
|
+
/**
|
74
|
+
* Returns an actual ring bits capacity.
|
75
|
+
*/
|
76
|
+
static VALUE
|
77
|
+
ring_bits_ext_get_size(VALUE self) {
|
78
|
+
struct ring_bits_ext *ptr;
|
79
|
+
Data_Get_Struct(self, struct ring_bits_ext, ptr);
|
80
|
+
return SIZET2NUM(ptr->size);
|
81
|
+
}
|
82
|
+
|
83
|
+
bool
|
84
|
+
_ring_bits_ext_set(struct ring_bits_ext *ptr, int bit_index, bool value) {
|
85
|
+
assert(bit_index >= 0);
|
86
|
+
|
87
|
+
size_t word_index = ring_bits_ext_word_index(bit_index);
|
88
|
+
|
89
|
+
uint64_t bit_mask = ((uint64_t) 1) << bit_index;
|
90
|
+
bool previous = (ptr->words[word_index] & bit_mask) != 0;
|
91
|
+
|
92
|
+
//fprintf(stderr, "set: word_index: %lu bit_index: %i bit_mask: %llx \n", word_index, bit_index, bit_mask);
|
93
|
+
|
94
|
+
if (value) {
|
95
|
+
ptr->words[word_index] |= bit_mask;
|
96
|
+
} else {
|
97
|
+
ptr->words[word_index] &= ~bit_mask;
|
98
|
+
}
|
99
|
+
|
100
|
+
return previous;
|
101
|
+
}
|
102
|
+
|
103
|
+
/**
|
104
|
+
* Sets the bit at the specified index to value.
|
105
|
+
*
|
106
|
+
* @param [Fixnum] bit_index_value a bit index
|
107
|
+
* @return previous state of bit_index that can be true or false
|
108
|
+
* @raise [ArgumentError] if the specified index is negative
|
109
|
+
*/
|
110
|
+
static VALUE
|
111
|
+
ring_bits_ext_set(VALUE self, VALUE bit_index_value, VALUE value) {
|
112
|
+
struct ring_bits_ext *ptr;
|
113
|
+
Data_Get_Struct(self, struct ring_bits_ext, ptr);
|
114
|
+
|
115
|
+
int bit_index = NUM2INT(bit_index_value);
|
116
|
+
if (0 > bit_index) {
|
117
|
+
rb_raise(rb_eArgError, "ring bit's index must be positive, got %i", bit_index);
|
118
|
+
}
|
119
|
+
|
120
|
+
bool previous = _ring_bits_ext_set(ptr, bit_index, value == Qtrue);
|
121
|
+
return previous ? Qtrue : Qfalse;
|
122
|
+
}
|
123
|
+
|
124
|
+
bool
|
125
|
+
_ring_bits_ext_get(struct ring_bits_ext *ptr, int bit_index) {
|
126
|
+
assert(0 <= bit_index);
|
127
|
+
|
128
|
+
size_t word_index = ring_bits_ext_word_index(bit_index);
|
129
|
+
uint64_t bit_mask = ((uint64_t) 1) << bit_index;
|
130
|
+
|
131
|
+
//fprintf(stderr, "get: word_index: %lu bit_index: %i bit_mask: %llx\n", word_index, bit_index, bit_mask);
|
132
|
+
|
133
|
+
return (ptr->words[word_index] & bit_mask) != 0;
|
134
|
+
}
|
135
|
+
|
136
|
+
/**
|
137
|
+
* Gets the bit at the specified index.
|
138
|
+
*
|
139
|
+
* @param [Fixnum] bit_index_value a bit index
|
140
|
+
* @return state of bit_index that can be true or false
|
141
|
+
* @raise [ArgumentError] if the specified index is negative
|
142
|
+
*/
|
143
|
+
static VALUE
|
144
|
+
ring_bits_ext_get(VALUE self, VALUE bit_index_value) {
|
145
|
+
struct ring_bits_ext *ptr;
|
146
|
+
Data_Get_Struct(self, struct ring_bits_ext, ptr);
|
147
|
+
|
148
|
+
int bit_index = NUM2INT(bit_index_value);
|
149
|
+
if (0 > bit_index) {
|
150
|
+
rb_raise(rb_eArgError, "ring bit's index must be positive, got %i", bit_index);
|
151
|
+
}
|
152
|
+
|
153
|
+
bool value = _ring_bits_ext_get(ptr, bit_index);
|
154
|
+
return value ? Qtrue : Qfalse;
|
155
|
+
}
|
156
|
+
|
157
|
+
/**
|
158
|
+
* Module entry point.
|
159
|
+
*/
|
160
|
+
void
|
161
|
+
Init_ring_bits_ext() {
|
162
|
+
VALUE mR4r;
|
163
|
+
VALUE cRingBitSet;
|
164
|
+
|
165
|
+
mR4r = rb_define_module("R4r");
|
166
|
+
cRingBitSet = rb_define_class_under(mR4r, "RingBitsExt", rb_cObject);
|
167
|
+
|
168
|
+
rb_define_alloc_func(cRingBitSet, ring_bits_ext_alloc);
|
169
|
+
rb_define_method(cRingBitSet, "initialize", ring_bits_ext_init, 1);
|
170
|
+
rb_define_method(cRingBitSet, "size", ring_bits_ext_get_size, 0);
|
171
|
+
rb_define_method(cRingBitSet, "set", ring_bits_ext_set, 2);
|
172
|
+
rb_define_method(cRingBitSet, "get", ring_bits_ext_get, 1);
|
173
|
+
}
|