문제

Postfix SMTP 액세스 정책 대표단으로 사용될 루비 스크립트를 작성하고 있습니다. 스크립트는 도쿄 폭군 데이터베이스에 액세스해야합니다. 네트워크 연결을 관리하기 위해 EventMachine을 사용하고 있습니다. EventMachine은 새로운 연결이 생성 될 때마다 EventMachine의 처리 루프에 의해 인스턴스화되는 EventMachine :: Connection 클래스가 필요합니다. 따라서 각 연결마다 클래스가 인스턴스화되고 파괴됩니다.

EventMachine :: Connection의 Post_init (예 : 연결이 설정된 직후)에서 Tokyo Tyrant와 연결을 만들고 연결이 종료 된 후 찢어집니다.

내 질문은 이것이 DB에 적절한 방법인지 여부입니다. 즉, 연결하는 모든 yime이 필요하고 완료 한 후에 찢어지고 있습니까? 프로그램 종료 중에 (프로그램이 시작될 때) DB에 연결하는 것이 더 낫지 않습니까? 그렇다면 어떻게 코딩해야합니까?

내 코드는 다음과 같습니다.

require 'rubygems'
require 'eventmachine'
require 'rufus/tokyo/tyrant'

class LineCounter < EM::Connection
  ActionAllow = "action=dunno\n\n"

  def post_init
    puts "Received a new connection"
    @tokyo = Rufus::Tokyo::Tyrant.new('server', 1978)
    @data_received = ""
  end

  def receive_data data
    @data_received << data
    @data_received.lines do |line|
      key = line.split('=')[0]
      value = line.split('=')[1]
      @reverse_client_name = value.strip() if key == 'reverse_client_name'
      @client_address = value.strip() if key == 'client_address'
      @tokyo[@client_address] = @reverse_client_name
    end
    puts @client_address, @reverse_client_name
    send_data ActionAllow
  end

  def unbind
    @tokyo.close
  end
end

EventMachine::run {
  host,port = "127.0.0.1", 9997
  EventMachine::start_server host, port, LineCounter
  puts "Now accepting connections on address #{host}, port #{port}..."
  EventMachine::add_periodic_timer( 10 ) { $stderr.write "*" }
}

그와 관련하여

주권

도움이 되었습니까?

해결책

놀랍게도이 질문에 대한 답은 없습니다.

아마도 필요한 것은 필요에 따라 연결, 사용 및 반환 할 수있는 연결 풀입니다.

class ConnectionPool
  def initialize(&block)
    @pool = [ ]
    @generator = block
  end

  def fetch
    @pool.shift or @generator and @generator.call
  end

  def release(handle)
    @pool.push(handle)
  end

  def use
    if (block_given?)
      handle = fetch

      yield(handle) 

      release(handle)
    end
  end
end

# Declare a pool with an appropriate connection generator
tokyo_pool = ConnectionPool.new do
  Rufus::Tokyo::Tyrant.new('server', 1978)
end

# Fetch/Release cycle
tokyo = tokyo_pool.fetch
tokyo[@client_address] = @reverse_client_name
tokyo_pool.release(tokyo)

# Simple block-method for use
tokyo_pool.use do |tokyo|
  tokyo[@client_address] = @reverse_client_name
end
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top