From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS33070 50.56.128.0/17 X-Spam-Status: No, score=-0.2 required=5.0 tests=AWL shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: archivist@yhbt.net Delivered-To: archivist@dcvr.yhbt.net Received: from rubyforge.org (50-56-192-79.static.cloud-ips.com [50.56.192.79]) by dcvr.yhbt.net (Postfix) with ESMTP id 6BB111F435 for ; Mon, 1 Oct 2012 19:36:01 +0000 (UTC) Received: from localhost.localdomain (localhost [127.0.0.1]) by rubyforge.org (Postfix) with ESMTP id C48102E06E; Mon, 1 Oct 2012 19:36:00 +0000 (UTC) X-Original-To: mongrel-unicorn@rubyforge.org Delivered-To: mongrel-unicorn@rubyforge.org Received: from dcvr.yhbt.net (dcvr.yhbt.net [64.71.152.64]) by rubyforge.org (Postfix) with ESMTP id 40E072E06C for ; Mon, 1 Oct 2012 19:31:16 +0000 (UTC) Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id 1D8C51F42E; Mon, 1 Oct 2012 19:31:16 +0000 (UTC) Date: Mon, 1 Oct 2012 19:31:15 +0000 From: Eric Wong To: unicorn list Subject: Re: after_fork and redis Message-ID: <20121001193115.GA23907@dcvr.yhbt.net> References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.21 (2010-09-15) X-BeenThere: mongrel-unicorn@rubyforge.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: mongrel-unicorn-bounces@rubyforge.org Errors-To: mongrel-unicorn-bounces@rubyforge.org bradford wrote: > I'm using unicorn w/ a rails app. I have the following in my > environment.rb $redis = MyApplication::Application.config.redis and in > production.rb I have config.redis = Redis.new(host: "localhost"). > > I've read I'm supposed to $redis = Redis.new(host: "localhost") in > after_fork when preload_app is true. > > When I don't do this, each worker/pid seems to have their own redis > instance. So, why is this needed? Here's the logs of me printing out > $redis.client.inspect when both $redis = Redis.new in the after_fork > and just $redis = Redis.new in the environment.rb. (Disclaimer: I still haven't used Redis, but similar knowledge applies to every TCP-based service) If a Redis client opens a TCP (or any stream) socket connection before forking, all the children that are forked will share that _same_ local TCP socket. Sharing stream sockets across processes/threads is nearly always a bad idea: data streams become interleaved and impossible to separate in either direction. > No after fork > https://gist.github.com/bc2c2a3bda01c35730e2 > > After fork > https://gist.github.com/0c96550660d3926ffe16 > > The only thing different I notice is Connection::TCPSocket:fd is > always 13 w/ the after fork. FD shouldn't really matter. FDs get recycled ASAP once they're closed (and GC can automatically close them). What you want is a different local port for the TCPSocket on every worker process, and the after_fork hook will give you that. Btw, you can demonstrate FD recycling with: require 'socket' loop do c = TCPSocket.new("example.com", 80) p [ :fileno, c.fileno ] p [ :addr, c.addr ] # (local address) c.close sleep 1 # be nice to the remote server end You'll see the same FD recycled over and over, but c.addr will have a different local port. You can see if after_fork is working correctly by checking the output of tools like `ss' or `netstat': (something like: ss -tan | grep -F :$PORT_OF_REDIS) You can also check connections on the Redis server/process itself using "lsof -p $PID_OF_REDIS_DAEMON" _______________________________________________ Unicorn mailing list - mongrel-unicorn@rubyforge.org http://rubyforge.org/mailman/listinfo/mongrel-unicorn Do not quote signatures (like this one) or top post when replying