Project

forkhandle

0.0
No commit activity in last 3 years
No release in over 3 years
a teeny library / design pattern for managing connections in a process and thread safe fashion
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

NAME

forkhandle

SYNOPSIS

connection = ForkHandle.get(:db_connection){ Db.connect(config) }

DESCRIPTION

managing connection across forks and threads is tricky. most libraries use and icky idiom that requires each and every client to configure it's own forking logic, something like

MyLameLib.after_fork do

  # close handles you should close

end

many libs also do not provide you with per-thread connection, making MT a manual process.

a teeny bit of code can solve both. the concept is simple:

maintain a table of connections scoped by process id and thread id. any miss will trigger auto-scrubbing the table, but only connections from another process (we've been forked) will be closed. this gives

  • per thread connections

  • per process connections

  • auto-matic cleanup after a fork