Skip to content

Instantly share code, notes, and snippets.

@ajvondrak

ajvondrak/.ruby-version

Last active Jun 18, 2020
Embed
What would you like to do?
source 'https://rubygems.org'
gem 'goliath', github: 'postrank-labs/goliath'
gem 'redis'
gem 'hiredis'
# Goliath declares rack (>= 1.2.2), which is too lax now that rack 2 exists.
gem 'rack', '~> 1'
GIT
remote: https://github.com/postrank-labs/goliath.git
revision: bdbf254eca6ff59e5a83d9969db7c5462e06ed61
specs:
goliath (1.0.6)
async-rack
einhorn
em-synchrony (>= 1.0.0)
em-websocket (= 0.3.8)
eventmachine (>= 1.0.0.beta.4)
http_parser.rb (>= 0.6.0)
log4r
multi_json
rack (>= 1.2.2)
rack-contrib
rack-respond_to
GEM
remote: https://rubygems.org/
specs:
addressable (2.7.0)
public_suffix (>= 2.0.2, < 5.0)
async-rack (0.5.1)
rack (~> 1.1)
einhorn (0.7.4)
em-synchrony (1.0.6)
eventmachine (>= 1.0.0.beta.1)
em-websocket (0.3.8)
addressable (>= 2.1.1)
eventmachine (>= 0.12.9)
eventmachine (1.2.7)
hiredis (0.6.3)
http_parser.rb (0.6.0)
log4r (1.1.10)
multi_json (1.14.1)
public_suffix (4.0.5)
rack (1.6.13)
rack-accept-media-types (0.9)
rack-contrib (1.8.0)
rack (~> 1.4)
rack-respond_to (0.9.8)
rack-accept-media-types (>= 0.6)
redis (4.2.1)
PLATFORMS
ruby
DEPENDENCIES
goliath!
hiredis
rack (~> 1)
redis
BUNDLED WITH
2.1.4
require 'goliath'
require 'redis'
class Repro < Goliath::API
use Goliath::Rack::Params
def response(env)
head? ? head : tail
rescue Redis::BaseError => e
[500, {}, e.message]
end
def self.redis
@redis ||= Redis.new(url: ENV['REDIS_URL'], driver: :synchrony)
end
def redis
self.class.redis
end
def id
params['id']
end
def lock
@lock ||= SecureRandom.urlsafe_base64
end
def entries
"#{id}:entries"
end
def head?
redis.set(id, lock, nx: true, ex: 300)
end
def head
redis.pipelined do
redis.rpush(entries, %w[b c d e])
redis.expire(entries, 300)
end
[200, {}, 'a']
end
def tail
response = redis.blpop(entries, timeout: 1)
response ? [200, {}, response.last] : [500, {}, 'timeout']
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.