-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
EDIT: This branch is the iteration target. Implementing a per-host circuit breaker state using shared memory and semaphores #54
Open
kyewei
wants to merge
23
commits into
main
Choose a base branch
from
circuit-breaker-per-host
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
f224e95
Initial commit implementing a per-host circuit breaker state using sh…
kyewei 711ffa9
Refactor to include some suggestions:
kyewei cad8497
Refactor naming, some shared dependencies
kyewei 9faca9a
Refactor: split acquire_memory up into separate functions
kyewei a59e4a9
Refactored: added tests, created superclass SharedMemoryObject and in…
kyewei e3bba16
Separate out SysV and non-SysV classes, use #increment, fix style for…
kyewei 51762bb
Enum implements Forwardable, fix bugs in SharedMemoryOBject implement…
kyewei b1bb06c
Made #shared private, removed uses of it
kyewei 56c103a
Use keyword parameters for init, test by importing modules, initializ…
kyewei 1b178f5
Rebased, small changes had to be done
kyewei 7103df0
Redistribute under Simple and SysV namespace
kyewei 7ab2f36
Change API a bit
kyewei 2e9cd7a
Change to State from Enum, updated to match merged PR, removed Mutex …
kyewei 4896c3c
Rename SharedMemoryObject to SysVSharedMemory and made it a mixin mod…
kyewei 1aa5cf4
Small Changes
kyewei 6e49d3d
Unified use of #synchronize, removed potentially unsafe lock and unlo…
kyewei 882da9d
Changed naming to initialize_memory and bind_initialize_memory_callback
kyewei 3e99639
Nitpick
kyewei 2f09204
Cleanup lock, unlock, delete memory code, rename to cleanup_memory
kyewei 5e55f6e
Refactored resizing and initializing code, reduced size from 300~ lin…
kyewei a35d0d7
Made fallback boot-time instead of runtime, remove unneeded functions
kyewei 2bbd717
Small changes to code
kyewei d6ce4aa
Abstract away passing in symbols for data_layout, remove unless
kyewei File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
/.bundle/ | ||
/lib/semian/*.so | ||
/lib/semian/*.bundle | ||
/lib/**/*.so | ||
/lib/**/*.bundle | ||
/tmp/* | ||
*.gem | ||
/html/ | ||
|
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <sys/types.h> | ||
#include <sys/ipc.h> | ||
#include <sys/sem.h> | ||
#include <sys/time.h> | ||
#include <errno.h> | ||
#include <string.h> | ||
|
||
#include <ruby.h> | ||
#include <ruby/util.h> | ||
#include <ruby/io.h> | ||
|
||
#include <openssl/sha.h> | ||
|
||
#include <stdio.h> | ||
|
||
#include <sys/shm.h> | ||
#include <unistd.h> | ||
|
||
#include <math.h> | ||
|
||
union semun { | ||
int val; /* Value for SETVAL */ | ||
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ | ||
unsigned short *array; /* Array for GETALL, SETALL */ | ||
struct seminfo *__buf; /* Buffer for IPC_INFO | ||
(Linux-specific) */ | ||
}; | ||
|
||
#if defined(HAVE_RB_THREAD_CALL_WITHOUT_GVL) && defined(HAVE_RUBY_THREAD_H) | ||
// 2.0 | ||
#include <ruby/thread.h> | ||
#define WITHOUT_GVL(fn,a,ubf,b) rb_thread_call_without_gvl((fn),(a),(ubf),(b)) | ||
#elif defined(HAVE_RB_THREAD_BLOCKING_REGION) | ||
// 1.9 | ||
typedef VALUE (*my_blocking_fn_t)(void*); | ||
#define WITHOUT_GVL(fn,a,ubf,b) rb_thread_blocking_region((my_blocking_fn_t)(fn),(a),(ubf),(b)) | ||
#endif | ||
|
||
VALUE eSyscall, eTimeout, eInternal; | ||
|
||
key_t | ||
generate_key(const char *name); | ||
|
||
void | ||
raise_semian_syscall_error(const char *syscall, int error_num); | ||
|
||
void | ||
set_semaphore_permissions(int sem_id, int permissions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
#include "semian_shared_memory_object.h" | ||
|
||
typedef struct { | ||
int value; | ||
} semian_int; | ||
|
||
static void semian_integer_initialize_memory (size_t byte_size, void *dest, void *prev_data, size_t prev_data_byte_size); | ||
static VALUE semian_integer_bind_initialize_memory_callback(VALUE self); | ||
static VALUE semian_integer_get_value(VALUE self); | ||
static VALUE semian_integer_set_value(VALUE self, VALUE num); | ||
static VALUE semian_integer_increment(int argc, VALUE *argv, VALUE self); | ||
|
||
static void | ||
semian_integer_initialize_memory (size_t byte_size, void *dest, void *prev_data, size_t prev_data_byte_size) | ||
{ | ||
semian_int *ptr = dest; | ||
semian_int *old = prev_data; | ||
if (prev_data){ | ||
ptr->value = old->value; | ||
} else { | ||
ptr->value=0; | ||
} | ||
} | ||
|
||
static VALUE | ||
semian_integer_bind_initialize_memory_callback(VALUE self) | ||
{ | ||
semian_shm_object *ptr; | ||
TypedData_Get_Struct(self, semian_shm_object, &semian_shm_object_type, ptr); | ||
ptr->initialize_memory = &semian_integer_initialize_memory; | ||
return self; | ||
} | ||
|
||
static VALUE | ||
semian_integer_get_value(VALUE self) | ||
{ | ||
semian_shm_object *ptr; | ||
TypedData_Get_Struct(self, semian_shm_object, &semian_shm_object_type, ptr); | ||
if (0 == ptr->shm_address) | ||
return Qnil; | ||
int value = ((semian_int *)(ptr->shm_address))->value; | ||
return INT2NUM(value); | ||
} | ||
|
||
static VALUE | ||
semian_integer_set_value(VALUE self, VALUE num) | ||
{ | ||
semian_shm_object *ptr; | ||
TypedData_Get_Struct(self, semian_shm_object, &semian_shm_object_type, ptr); | ||
if (0 == ptr->shm_address) | ||
return Qnil; | ||
if (TYPE(num) != T_FIXNUM && TYPE(num) != T_FLOAT) | ||
return Qnil; | ||
((semian_int *)(ptr->shm_address))->value = NUM2INT(num); | ||
return num; | ||
} | ||
|
||
static VALUE | ||
semian_integer_reset(VALUE self) | ||
{ | ||
return semian_integer_set_value(self, INT2NUM(0)); | ||
} | ||
|
||
static VALUE | ||
semian_integer_increment(int argc, VALUE *argv, VALUE self) | ||
{ | ||
VALUE num; | ||
rb_scan_args(argc, argv, "01", &num); | ||
if (num == Qnil) | ||
num = INT2NUM(1); | ||
|
||
semian_shm_object *ptr; | ||
TypedData_Get_Struct(self, semian_shm_object, &semian_shm_object_type, ptr); | ||
if (0 == ptr->shm_address) | ||
return Qnil; | ||
if (TYPE(num) != T_FIXNUM && TYPE(num) != T_FLOAT) | ||
return Qnil; | ||
((semian_int *)(ptr->shm_address))->value += NUM2INT(num); | ||
return self; | ||
} | ||
|
||
static VALUE | ||
semian_integer_calculate_byte_size(VALUE klass) | ||
{ | ||
return SIZET2NUM(sizeof(int)); | ||
} | ||
|
||
void | ||
Init_semian_integer (void) | ||
{ | ||
// Bind methods to Integer | ||
VALUE cSemianModule = rb_const_get(rb_cObject, rb_intern("Semian")); | ||
VALUE cSysVSharedMemory = rb_const_get(cSemianModule, rb_intern("SysVSharedMemory")); | ||
VALUE cSysVModule = rb_const_get(cSemianModule, rb_intern("SysV")); | ||
VALUE cInteger = rb_const_get(cSysVModule, rb_intern("Integer")); | ||
|
||
semian_shm_object_replace_alloc(cSysVSharedMemory, cInteger); | ||
|
||
rb_define_private_method(cInteger, "bind_initialize_memory_callback", semian_integer_bind_initialize_memory_callback, 0); | ||
define_method_with_synchronize(cInteger, "value", semian_integer_get_value, 0); | ||
define_method_with_synchronize(cInteger, "value=", semian_integer_set_value, 1); | ||
define_method_with_synchronize(cInteger, "reset", semian_integer_reset, 0); | ||
define_method_with_synchronize(cInteger, "increment", semian_integer_increment, -1); | ||
rb_define_method(cInteger, "calculate_byte_size", semian_integer_calculate_byte_size, 0); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should consider doing a follow-up where we implement the blocking ones ourselves (where they're needed) for OS X support.