Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6648923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T00:42:47+00:00 2026-05-26T00:42:47+00:00

I am implementing a small thrift (0.6.0) server in ruby to play a role

  • 0

I am implementing a small thrift (0.6.0) server in ruby to play a role of proxy to another protocol with several connections (multiple clients) to a single server. I want to be able and keep per-client data on the server side and track “session” parameters across multiple invocations of handler functions.

I currently use Thrift::NonblockingServer as SimpleServer does not seem to allow concurrent connections.

I know how to use TCPSocket::peeraddr but Thrift::NonblockingServer::IOManager::Worker::run creates a temporary MemoryBufferTransport with the frame it read and passes that as the input/output protocol down to the processor so it seems that the info is not passed down from there.

Is there a clean way to do this?
I was thinking of re-defining the above mentioned Thrift::NonblockingServer::IOManager::Worker::run to also include the fd, or other ID at an additional parameter to process or augment the proto instances but as I also have to worry about one layer of generated ruby code (process_* methods in class Processor) it seems a little heavy.

I wonder if someone did anything like this before.

Thanks!

p.s. this is similar problem to this C++ thrift question

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T00:42:47+00:00Added an answer on May 26, 2026 at 12:42 am

    Here is how I went about changing Thrift::NonblockingServer::IOManager::Worker::run to support this.

    Few caveats:

    • As I mentioned in the question I don’t consider it clean (if nothing else I will have to monitor future thrift versions for changes in this function, this is based on 0.6.0).
    • this is written to work with ruby 1.8 (or I would have gotten the non translated addr/port see my other question)
    • I’m a ruby newbie .. I’m sure I’ve done some of this “wrong” (for example, should $connections be @@connections in ConnEntry or a diff class?).
    • I am aware that the Thread.current hash for thread-local storage has a namespace pollution issue

    First, at some central module:

    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
    

    Then at any point in the handler you can access Thread.current[:connEntry].addr_info for connection specific data or store anything regarding the connection in the Thread.current[:connEntry].attr hash.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a small HTTP server with Ruby using Mongrel. My code currently looks
I'm looking for a small and fast library implementing an HTTP server in .NET
I'm trying to make a judgment call in implementing a small-ish SQL Server '08
I am implementing a small queue to handle which process gets to run first.
I am implementing a small database(university Project) and i am facing the following problem.
I'm implementing a small web app widget that shows a legend/key for a diagram
I have noticed that doing actions like implementing an interface, that a small rectangle
I'm implementing a small keypad where the click on a button will add the
I am implementing a small application (observer) that needs to attach itself to the
I am implementing a small demo application which encrypts using AES CTR with OpenSSL

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.