Skip to content

Commit c83159e

Browse files
author
Michael Kipper
committed
Review fixes
1 parent e93e9f0 commit c83159e

File tree

10 files changed

+53
-57
lines changed

10 files changed

+53
-57
lines changed

ext/semian/simple_integer.c

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,12 @@
88

99
static const rb_data_type_t semian_simple_integer_type;
1010

11-
static int* get_value(VALUE self) {
12-
const char* name = to_s(rb_iv_get(self, "@name"));
13-
if (name == NULL) {
14-
rb_raise(rb_eArgError, "could not get object name");
15-
}
16-
17-
key_t key = generate_key(name);
11+
static semian_simple_integer_shared_t* get_value(VALUE self) {
12+
semian_simple_integer_t *res;
13+
TypedData_Get_Struct(self, semian_simple_integer_t, &semian_simple_integer_type, res);
1814

1915
const int permissions = 0664;
20-
int shmid = shmget(key, sizeof(int), IPC_CREAT | permissions);
16+
int shmid = shmget(res->key, sizeof(semian_simple_integer_shared_t), IPC_CREAT | permissions);
2117
if (shmid == -1) {
2218
rb_raise(rb_eArgError, "could not create shared memory (%s)", strerror(errno));
2319
}
@@ -27,7 +23,7 @@ static int* get_value(VALUE self) {
2723
rb_raise(rb_eArgError, "could not get shared memory (%s)", strerror(errno));
2824
}
2925

30-
return (int*)val;
26+
return (semian_simple_integer_shared_t*)val;
3127
}
3228

3329
void Init_SimpleInteger()
@@ -37,8 +33,8 @@ void Init_SimpleInteger()
3733
VALUE cSimpleInteger = rb_const_get(cSimple, rb_intern("Integer"));
3834

3935
rb_define_alloc_func(cSimpleInteger, semian_simple_integer_alloc);
40-
rb_define_method(cSimpleInteger, "initialize_simple_integer", semian_simple_integer_initialize, 0);
41-
rb_define_method(cSimpleInteger, "increment", semian_simple_integer_increment, 1);
36+
rb_define_method(cSimpleInteger, "initialize_simple_integer", semian_simple_integer_initialize, 1);
37+
rb_define_method(cSimpleInteger, "increment", semian_simple_integer_increment, -1);
4238
rb_define_method(cSimpleInteger, "reset", semian_simple_integer_reset, 0);
4339
rb_define_method(cSimpleInteger, "value", semian_simple_integer_value_get, 0);
4440
rb_define_method(cSimpleInteger, "value=", semian_simple_integer_value_set, 1);
@@ -52,39 +48,53 @@ VALUE semian_simple_integer_alloc(VALUE klass)
5248
}
5349

5450

55-
VALUE semian_simple_integer_initialize(VALUE self)
51+
VALUE semian_simple_integer_initialize(VALUE self, VALUE name)
5652
{
57-
int* data = get_value(self);
58-
*data = 0;
53+
semian_simple_integer_t *res;
54+
TypedData_Get_Struct(self, semian_simple_integer_t, &semian_simple_integer_type, res);
55+
res->key = generate_key(to_s(name));
56+
57+
semian_simple_integer_shared_t* data = get_value(self);
58+
data->val = 0;
5959

6060
return self;
6161
}
6262

63-
VALUE semian_simple_integer_increment(VALUE self, VALUE val) {
64-
int *data = get_value(self);
65-
*data += rb_num2int(val);
63+
VALUE semian_simple_integer_increment(int argc, VALUE *argv, VALUE self) {
64+
// This is definitely the worst API ever.
65+
// https://silverhammermba.github.io/emberb/c/#parsing-arguments
66+
VALUE val;
67+
rb_scan_args(argc, argv, "01", &val);
68+
69+
semian_simple_integer_shared_t *data = get_value(self);
70+
71+
if (NIL_P(val)) {
72+
data->val += 1;
73+
} else {
74+
data->val += RB_NUM2INT(val);
75+
}
6676

67-
return RB_INT2NUM(*data);
77+
return RB_INT2NUM(data->val);
6878
}
6979

7080
VALUE semian_simple_integer_reset(VALUE self) {
71-
int *data = get_value(self);
72-
*data = 0;
81+
semian_simple_integer_shared_t *data = get_value(self);
82+
data->val = 0;
7383

74-
return RB_INT2NUM(*data);
84+
return RB_INT2NUM(data->val);
7585
}
7686

7787
VALUE semian_simple_integer_value_get(VALUE self) {
78-
int *data = get_value(self);
79-
return RB_INT2NUM(*data);
88+
semian_simple_integer_shared_t *data = get_value(self);
89+
return RB_INT2NUM(data->val);
8090
}
8191

8292
VALUE semian_simple_integer_value_set(VALUE self, VALUE val) {
83-
int *data = get_value(self);
93+
semian_simple_integer_shared_t *data = get_value(self);
8494

8595
// TODO(michaelkipper): Check for respond_to?(:to_i) before calling.
8696
VALUE to_i = rb_funcall(val, rb_intern("to_i"), 0);
87-
*data = RB_NUM2INT(to_i);
97+
data->val = RB_NUM2INT(to_i);
8898

89-
return RB_INT2NUM(*data);
99+
return RB_INT2NUM(data->val);
90100
}

ext/semian/simple_integer.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
void Init_SimpleInteger();
77

88
VALUE semian_simple_integer_alloc(VALUE klass);
9-
VALUE semian_simple_integer_initialize(VALUE self);
10-
VALUE semian_simple_integer_increment(VALUE self, VALUE val);
9+
VALUE semian_simple_integer_initialize(VALUE self, VALUE name);
10+
VALUE semian_simple_integer_increment(int argc, VALUE *argv, VALUE self);
1111
VALUE semian_simple_integer_reset(VALUE self);
1212
VALUE semian_simple_integer_value_get(VALUE self);
1313
VALUE semian_simple_integer_value_set(VALUE self, VALUE val);

ext/semian/sysv_semaphores.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ raise_semian_syscall_error(const char *syscall, int error_num)
2929
void
3030
initialize_semaphore_set(semian_resource_t* res, const char* id_str, long permissions, int tickets, double quota)
3131
{
32+
3233
res->key = generate_key(id_str);
3334
res->strkey = (char*) malloc((2 /*for 0x*/+ sizeof(uint64_t) /*actual key*/+ 1 /*null*/) * sizeof(char));
3435
sprintf(res->strkey, "0x%08x", (unsigned int) res->key);
@@ -51,10 +52,6 @@ initialize_semaphore_set(semian_resource_t* res, const char* id_str, long permis
5152
}
5253
}
5354

54-
# if DEBUG
55-
printf("[DEBUG] Init semaphore '%s' (key %s) to sem_id %d\n", res->name, res->strkey, res->sem_id);
56-
# endif
57-
5855
set_semaphore_permissions(res->sem_id, permissions);
5956

6057
/*

ext/semian/types.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ typedef struct {
5858
uint64_t key;
5959
} semian_simple_integer_t;
6060

61+
// Shared simple integer structure
62+
typedef struct {
63+
int val;
64+
} semian_simple_integer_shared_t;
65+
66+
// Internal simple sliding window structure
67+
typedef struct {
68+
uint64_t key;
69+
} semian_simple_sliding_window_t;
70+
6171
// Shared simple sliding window structure
6272
typedef struct {
6373
int max_size;
@@ -67,9 +77,4 @@ typedef struct {
6777
int data[SLIDING_WINDOW_MAX_SIZE];
6878
} semian_simple_sliding_window_shared_t;
6979

70-
// Internal simple sliding window structure
71-
typedef struct {
72-
uint64_t key;
73-
} semian_simple_sliding_window_t;
74-
7580
#endif // SEMIAN_TYPES_H

lib/semian/circuit_breaker.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@ def initialize(name, exceptions:, success_threshold:, error_threshold:, error_ti
2323
end
2424

2525
def acquire(resource = nil, &block)
26-
puts "[DEBUG] #{Time.now} - Acquiring resource '#{resource}'"
27-
2826
return yield if disabled?
2927
transition_to_half_open if transition_to_half_open?
3028

@@ -113,7 +111,6 @@ def error_threshold_reached?
113111
end
114112

115113
def error_timeout_expired?
116-
puts "[DEBUG] Checking error_timeout_expired? #{@errors.last}"
117114
last_error_time = @errors.last
118115
return false unless last_error_time
119116
Time.at(last_error_time) + @error_timeout < Time.now
@@ -124,7 +121,6 @@ def push_error(error)
124121
end
125122

126123
def push_time(window, time: Time.now)
127-
puts "[DEBUG] push_time(#{time.to_i}) - rejecting before #{time.to_i - @error_timeout}"
128124
window.reject! { |err_time| err_time + @error_timeout < time.to_i }
129125
window << time.to_i
130126
end

lib/semian/simple_integer.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ class Integer #:nodoc:
66
attr_accessor :value
77

88
def initialize(name)
9-
@name = name
10-
initialize_simple_integer if respond_to?(:initialize_simple_integer)
9+
initialize_simple_integer(name) if respond_to?(:initialize_simple_integer)
1110
reset
1211
end
1312

14-
def increment(val)
13+
def increment(val = 1)
1514
@value += val
1615
end
1716

test/circuit_breaker_test.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ def setup
1111
rescue
1212
nil
1313
end
14-
15-
puts "[DEBUG] Registering semian :testing at #{Time.now} (#{Time.now.to_i})"
1614
Semian.register(:testing, tickets: 1, exceptions: [SomeError], error_threshold: 2, error_timeout: 5, success_threshold: 1)
1715
@resource = Semian[:testing]
1816
end
@@ -42,14 +40,8 @@ def test_after_error_timeout_is_elapsed_requests_are_attempted_again
4240
end
4341

4442
def test_until_success_threshold_is_reached_a_single_error_will_reopen_the_circuit
45-
puts @strio.string
46-
puts "[DEBUG] Half open circuit..."
4743
half_open_cicuit!
48-
puts @strio.string
49-
puts "[DEBUG] Trigger error..."
5044
trigger_error!
51-
puts @strio.string
52-
puts "[DEBUG] Assert circuit opened..."
5345
assert_circuit_opened
5446
assert_match(/State transition from open to half_open/, @strio.string)
5547
end

test/redis_test.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,7 @@ def test_circuit_breaker_on_query
272272
client.get('foo')
273273
end
274274

275-
puts "[DEBUG] #1 Time.now = #{Time.now}"
276275
Timecop.travel(ERROR_TIMEOUT + 1) do
277-
puts "[DEBUG] #2 Time.now = #{Time.now}"
278276
assert_equal '2', client.get('foo')
279277
end
280278
end

test/resource_test.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require 'test_helper'
2-
32
require 'objspace'
43

54
class TestResource < Minitest::Test
@@ -490,7 +489,7 @@ def test_multiple_register_with_fork
490489

491490
def test_memsize
492491
r = create_resource :testing, tickets: 1
493-
puts "mkipper: Resource size is #{ObjectSpace.memsize_of(r)} byte(s)"
492+
assert_equal 128, ObjectSpace.memsize_of(r)
494493
end
495494

496495
def create_resource(*args)

test/simple_integer_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ def test_access_value
2626
def test_increment
2727
@integer.increment(4)
2828
assert_equal(4, @integer.value)
29-
@integer.increment(1)
29+
@integer.increment
3030
assert_equal(5, @integer.value)
3131
@integer.increment(-2)
3232
assert_equal(3, @integer.value)

0 commit comments

Comments
 (0)