Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
havenwood committed Jun 22, 2022
0 parents commit a69de82
Show file tree
Hide file tree
Showing 20 changed files with 438 additions and 0 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Ruby

on:
push:
branches:
- master

pull_request:

jobs:
build:
runs-on: ubuntu-latest
name: Ruby ${{ matrix.ruby }}
strategy:
matrix:
ruby:
- '3.2.0'

steps:
- uses: actions/checkout@v2
- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true
- name: Run the default task
run: bundle exec rake
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/.bundle/
/pkg/
/tmp/
24 changes: 24 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require:
- rubocop-minitest
- rubocop-rake

AllCops:
TargetRubyVersion: 3.1
NewCops: enable

Metrics/MethodLength:
Enabled: false

Style/Documentation:
Enabled: false

Style/StringLiterals:
Enabled: true
EnforcedStyle: single_quotes

Style/StringLiteralsInInterpolation:
Enabled: true
EnforcedStyle: single_quotes

Layout/LineLength:
Max: 120
12 changes: 12 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

source 'https://rubygems.org'

gemspec

gem 'minitest', '~> 5.0'
gem 'minitest-proveit'
gem 'rake', '~> 13.0'
gem 'rubocop', '~> 1.21'
gem 'rubocop-minitest', require: false
gem 'rubocop-rake', require: false
51 changes: 51 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
PATH
remote: .
specs:
gen_server (0.0.1)

GEM
remote: https://rubygems.org/
specs:
ast (2.4.2)
minitest (5.16.1)
minitest-proveit (1.0.0)
minitest (> 5, < 7)
parallel (1.22.1)
parser (3.1.2.0)
ast (~> 2.4.1)
rainbow (3.1.1)
rake (13.0.6)
regexp_parser (2.5.0)
rexml (3.2.5)
rubocop (1.30.1)
parallel (~> 1.10)
parser (>= 3.1.0.0)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.18.0, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 1.4.0, < 3.0)
rubocop-ast (1.18.0)
parser (>= 3.1.1.0)
rubocop-minitest (0.20.1)
rubocop (>= 0.90, < 2.0)
rubocop-rake (0.6.0)
rubocop (~> 1.0)
ruby-progressbar (1.11.0)
unicode-display_width (2.1.0)

PLATFORMS
x86_64-linux

DEPENDENCIES
gen_server!
minitest (~> 5.0)
minitest-proveit
rake (~> 13.0)
rubocop (~> 1.21)
rubocop-minitest
rubocop-rake

BUNDLED WITH
2.4.0.dev
21 changes: 21 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2022 Shannon Skipper

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# GenServer

This is a Ruby implementation of GenServer using Ractors.

## Installation

Add it to your bundle.
```sh
bundle add gen_server
```

Or just install it directly.
```sh
gem install gen_server
```

## Usage

Check out the `example/` directory for now.
15 changes: 15 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'bundler/gem_tasks'
require 'rake/testtask'
require 'rubocop/rake_task'

Rake::TestTask.new :test do |task|
task.libs << 'test'
task.libs << 'lib'
task.test_files = FileList['test/**/test_*.rb']
end

RuboCop::RakeTask.new

task default: %i[test rubocop]
9 changes: 9 additions & 0 deletions Steepfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# frozen_string_literal: true

target :lib do
signature 'sig'

check 'lib'

library 'securerandom', 'singleton'
end
29 changes: 29 additions & 0 deletions example/basic_stack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative '../lib/gen_server'

class BasicStack
include GenServer

def init(stack)
[:ok, stack]
end

def handle_call(message, _from, (head, *tail))
message => :pop

[:reply, head, tail]
end

def handle_cast((cast, element), state)
cast => :push
state.unshift(element)

[:noreply, element]
end
end

GenServer.start_link(BasicStack, [:hello]) => [:ok, pid]
p GenServer.call(pid, :pop)
p GenServer.cast(pid, %i[push world])
p GenServer.call(pid, :pop)
12 changes: 12 additions & 0 deletions example/noop.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require_relative '../lib/gen_server'

class Noop
include GenServer
end

GenServer.start_link(Noop, []) => [:ok, pid]
p GenServer.call(pid, :foo)
p GenServer.cast(pid, %i[push bar])
p GenServer.call(pid, :foo)
47 changes: 47 additions & 0 deletions example/stack.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# frozen_string_literal: true

require_relative '../lib/gen_server'

class Stack
include GenServer

##
# Client
class << self
def start_link(default = [])
GenServer.start_link(self, default)
end

def push(pid, element)
GenServer.cast(pid, [:push, element])
end

def pop(pid)
GenServer.call(pid, :pop)
end
end

##
# Server (callbacks)
def init(stack)
[:ok, stack]
end

def handle_call(message, _from, (head, *tail))
message => :pop

[:reply, head, tail]
end

def handle_cast((cast, element), state)
cast => :push
state.unshift(element)

[:noreply, element]
end
end

Stack.start_link([:hello]) => [:ok, pid]
p Stack.pop(pid)
p Stack.push(pid, :world)
p Stack.pop(pid)
25 changes: 25 additions & 0 deletions gen_server.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require_relative 'lib/gen_server/version'

Gem::Specification.new do |spec|
spec.name = 'gen_server'
spec.version = GenServer::VERSION
spec.authors = ['Shannon Skipper']
spec.email = ['[email protected]']

spec.summary = 'A Ractor-backed GenServer'
spec.description = 'A GenServer implemented with Ruby Ractors'
spec.homepage = 'https://github.com/havenwood/gen_server'
spec.license = 'MIT'
spec.required_ruby_version = '>= 3.1.0'

spec.files = Dir.chdir(File.expand_path(__dir__)) do
`git ls-files -z`.split("\x0").reject do |f|
(f == __FILE__) || f.match(%r{\A(?:(?:bin|test)/|\.(?:git))})
end
end
spec.executables = spec.files.grep(%r{\Abin/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.metadata['rubygems_mfa_required'] = 'true'
end
55 changes: 55 additions & 0 deletions lib/gen_server.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# frozen_string_literal: true

require 'securerandom'
require 'singleton'
require_relative 'gen_server/registry'
require_relative 'gen_server/version'

module GenServer
def initialize(...)
init(...)
end

def init(...) = nil
def handle_call(...) = [:reply, nil, nil]
def handle_cast(...) = [:noreply, nil]

class << self
def start_link(klass, state = [])
pid = SecureRandom.uuid

actor = Ractor.new(state, name: pid) do |state|
GenServer.receive(state)
end

Registry[pid] = Registry::Info.new(actor:, klass:)

[:ok, pid]
end

def receive(state)
case Ractor.receive
in [:cast, message, klass]
klass.allocate.handle_cast(message, state) => [:noreply, new_state]
receive(new_state)
in [:call, sender, message, klass]
klass.allocate.handle_call(message, sender, state) => [:reply, reply, new_state]
sender.send [:ok, reply]
receive(new_state)
end
end

def cast(pid, message)
Registry.actor(pid).send [:cast, message, Registry.klass(pid)]

:ok
end

def call(pid, message)
Registry.actor(pid).send [:call, Ractor.current, message, Registry.klass(pid)]
Ractor.receive => [:ok, response]

response
end
end
end
29 changes: 29 additions & 0 deletions lib/gen_server/registry.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

require_relative 'registry/info'

module GenServer
class Registry
include Singleton

attr_reader :pids

def initialize
@pids = {}
end

class << self
def []=(pid, info)
instance.pids[pid] = info
end

def actor(pid)
instance.pids.fetch(pid).actor
end

def klass(pid)
instance.pids.fetch(pid).klass
end
end
end
end
Loading

0 comments on commit a69de82

Please sign in to comment.