kissmetrics/km — pull request #13 by pazustep (2012-09-06 18:49:54+00:00)
Title: Make KM reasonably threadsafe

Note: I don't really expect this pull request to be merged, but I'd like to let people know this for exists, and to receive some feedback.

This fork removes most global state from the KM class, making it safer to use in multithreaded servers (most JRuby servers, plus puma, thin and soon passenger 4). The main offender is the globally stored user identity, so that's what I've focused on. Original commit message follows below.

----

WARNING: This is a backwards incompatible change. The 'identify'
method is gone, and all methods that previously used the globbaly set
identity now require it to be passed before other arguments. So:

KM::record(action, props) -> KM::record(identity, action, props)
KM::set(data) -> KM::set(identity, data)

KM::alias doesn't require the identity to be set, and keeps the same
signature as before. Internal methods (i.e., generate_query) were
updated accordingly.

Other internal changes include file locking when writing to the query
log file, and copying and truncating (with locks) instead of moving
when processing the log file for sending.