Ruby 获取thrift处理程序函数中的对等地址
我正在ruby中实现一个小型thrift 0.6.0服务器,以充当另一个协议的代理角色,该协议具有多个连接,多个客户端连接到单个服务器。我希望能够在服务器端保存每个客户端的数据,并跨多个处理程序函数调用跟踪会话参数 我目前使用Thrift::NonblockingServer,因为SimpleServer似乎不允许并发连接 我知道如何使用TCPSocket::peeraddr,但Thrift::NonblockingServer::IOManager::Worker::run会使用它读取的帧创建一个临时MemoryBufferTransport,并将其作为输入/输出协议传递给处理器,因此信息似乎不会从那里传递下去 有没有干净的方法可以做到这一点? 我正在考虑重新定义上面提到的Thrift::NonblockingServer::IOManager::Worker::run,以便在一个额外的参数中包含fd或其他ID来处理或扩充proto实例,但由于我还必须担心类处理器中生成的一层ruby代码process_*方法,因此它似乎有点沉重 我想知道以前是否有人做过这样的事 谢谢Ruby 获取thrift处理程序函数中的对等地址,ruby,thrift,Ruby,Thrift,我正在ruby中实现一个小型thrift 0.6.0服务器,以充当另一个协议的代理角色,该协议具有多个连接,多个客户端连接到单个服务器。我希望能够在服务器端保存每个客户端的数据,并跨多个处理程序函数调用跟踪会话参数 我目前使用Thrift::NonblockingServer,因为SimpleServer似乎不允许并发连接 我知道如何使用TCPSocket::peeraddr,但Thrift::NonblockingServer::IOManager::Worker::run会使用它读取的帧创建
p、 这与的问题类似。下面是我如何改变Thrift::NonblockingServer::IOManager::Worker::run来支持这一点 几点注意事项: 正如我在这个问题中提到的,我不认为它是干净的,如果没有别的,我将不得不监视未来节俭版本的变化,在这个函数,这是基于0.6。 这是为了配合ruby 1.8编写的,否则我会得到未翻译的addr/port 我是个红宝石新手。。我肯定我做了一些错误的事情,例如,$connections应该是conentry中的@connections还是diff类中的@connections?。 我知道线程本地存储的Thread.current散列 首先,在一些中心模块:
module MyThriftExt
$connections={}
class ConnEntry
attr_reader :addr_info, :attr
def initialize(thrift_fd)
@addr_info=thrift_fd.handle.peeraddr
@attr={}
end
def self.PreHandle(fd)
$connections[fd]=ConnEntry.new(fd) unless $connections[fd].is_a? ConnEntry
# make the connection entry as short-term thread-local variable
# (cleared in postHandle)
Thread.current[:connEntry]=$connections[fd]
end
def self.PostHandle()
Thread.current[:connEntry]=nil
end
def to_s()
"#{addr_info}"
end end end
module Thrift class NonblockingServer
class IOManager
alias :old_remove_connection :remove_connection
def remove_connection(fd)
$connections.delete fd
old_remove_connection(fd)
end
class Worker
# The following is verbatim from thrift 0.6.0 except for the two lines
# marked with "Added"
def run
loop do
cmd, *args = @queue.pop
case cmd
when :shutdown
@logger.debug "#{self} is shutting down, goodbye"
break
when :frame
fd, frame = args
begin
otrans = @transport_factory.get_transport(fd)
oprot = @protocol_factory.get_protocol(otrans)
membuf = MemoryBufferTransport.new(frame)
itrans = @transport_factory.get_transport(membuf)
iprot = @protocol_factory.get_protocol(itrans)
MyThriftExt::ConnEntry.PreHandle(fd) # <<== Added
@processor.process(iprot, oprot)
MyThriftExt::ConnEntry.PostHandle # <<== Added
rescue => e
@logger.error "#{Thread.current.inspect} raised error: #{e.inspect}\n#{e.backtrace.join("\n")}"
end
end
end
end
end
end
end
end
然后,在处理程序中的任何位置,您都可以访问Thread.current[:connEntry].addr\u信息以获取特定于连接的数据,或者在Thread.current[:connEntry].attr哈希中存储与连接有关的任何内容