Coder Social home page Coder Social logo

rack-freeze's Introduction

Rack::Freeze

Provides a policy for Rack middleware which should be frozen by default to prevent mutability bugs in a multi-threaded environment.

In Rack 2.1+, you can use Builder#freeze_app instead of this gem.

Build Status Code Climate Coverage Status

Motivation

I found issues due to unexpected state mutation when developing Utopia. It only became apparent when running in production using multi-threaded passenger. Freezing the middleware (and related state) allowed me to identify these issues, find other issues, and helps prevent these issues in the future.

Ideally, this concept would be part of rack. However, regardless of whether Rack adopts a policy on immutable middleware, this gem provides the tools necessary to implement such a policy transparently on top of existing rack middleware where possible.

Installation

Add this line to your application's Gemfile:

gem 'rack-freeze'

Usage

Add this to your config.ru:

require 'rack/freeze'

Now all your middleware will be frozen by default.

What bugs does this fix?

It guarantees as much as is possible, that middleware won't mutate during a request.

# This modifies `Rack::Builder#use` and `Rack::Builder#to_app` to generate a frozen stack of middleware.
require 'rack/freeze'

class NonThreadSafeMiddleware
	def initialize(app)
		@app = app
		@state = 0
	end
	
	def call(env)
		@state += 1
		
		return @app.call(env)
	end
end

use NonThreadSafeMiddleware

As NonThreadSafeMiddleware mutates it's state @state += 1, it will raise a RuntimeError. In a multi-threaded web-server, unprotected mutation of internal state will lead to undefined behavior. 5 out of 4 dentists agree that multi-threaded programming is hard to get right.

How to write thread-safe middleware?

There are two options: Don't mutate state, or if you need to for the purposes of performance, implement #freeze and use data-structures from concurrent-ruby.

require 'concurrent/map'

# Cache every request based on the path. Don't do this in production :)
class CacheEverythingForever
	def initialize(app)
		@app = app
		@cache_all_the_things = Concurrent::Map.new
	end
	
	def freeze
		return self if frozen?
		
		# Don't freeze @cache_all_the_things
		@app.freeze
		
		super
	end
	
	def call(env)
		# Use the thread-safe `Concurrent::Map` to fetch the value or store it if it doesn't exist already.
		@cache_all_the_things.fetch_or_store(env[Rack::PATH_INFO]) do
			@app.call(env)
		end
	end
end

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Released under the MIT license.

Copyright, 2017, by Samuel G. D. Williams.

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.