Uploaded image for project: 'Ruby Driver'
  1. Ruby Driver
  2. RUBY-2165

Yield client to block from constructor

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 2.13.0.rc1
    • Affects Version/s: None
    • Component/s: Dev Exp
    • Fully Compatible

      When using Ruby driver to manage the server deployment, there is often a need to create a client for a particular operation. Currently this requires a begin-ensure block to close the client:

            client = Mongo::Client.new(...)
            begin
              client.database.command(ismaster: 1)
            ensure
              client.close
            end
      

      Instead it would be convenient to have the constructor yield to the provided block and close the client, so that the application code looked like this:

      Mongo::Client.new(...) do |client|
        client.database.command(ismaster: 1)
      end
      

      The constructor already yields self if a block is given, but does not close the client resulting in the client leak if the application actually used the pattern given above. Also this usage is not documented in https://docs.mongodb.com/ruby-driver/current/quick-start/ (which is additionally entirely silent on the need to close the client).

            Assignee:
            neil.shweky@mongodb.com Neil Shweky (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: