Skip to content

client.call should start reactor for console debugging workflows #3

Open
@matti

Description

@matti

currently if you try to do what all the best programmers do - use repl and try things out until it works and then you copy/paste the code - you can't do it because:

[1] pry(main)> c = Async::Redis::Client.new(Async::IO::Endpoint.tcp(ENV['REDIS_HOST'], 6379))
=> #<Async::Redis::Client:0x000055f0a598b3b8
 @endpoint=#<Async::IO::HostEndpoint:0x000055f0a598b458 @options={}, @specification=["redis", 6379, nil, 1]>,
 @pool=#<Async::Redis::Pool:0x000055f0a598b340 @active=0, @available=#<Async::Notification:0x000055f0a598b2f0 @waiting=[]>, @constructor=#<Proc:0x000055f0a598b278@/usr/local/bundle/gems/async-redis-0.3.1/lib/async/redis/client.rb:109>, @limit=nil, @resources=[]>,
 @protocol=Async::Redis::Protocol::RESP>
[2] pry(main)> c.call "BLPOP", "SLEEP", 1
RuntimeError: No async task available!
from /usr/local/bundle/gems/async-1.12.0/lib/async/task.rb:161:in `current'

So what about changing this:

def call(*arguments)
  @pool.acquire do |connection|
    connection.write_request(arguments)
    return connection.read_response
  end
end

to:

def call(*arguments)
  Async.run do
    @pool.acquire do |connection|
      connection.write_request(arguments)
      return connection.read_response
    end
  end
end

or even:

def call(*arguments)
  done = Async::Notification.new
  Async.run do |t|
    response = nil
    @pool.acquire do |connection|
      connection.write_request(arguments)
      done.signal connection.read_response
    end
    return done.wait
  end
end

note: I didn't even monkeypatch that locally, but I think you get the idea?

Although https://github.com/socketry/async#asyncreactorrun says

The cost of using Async::Reactor.run is minimal for initialization/server setup, but is not ideal for per-connection tasks.

but I think it would be ideal for programmer happiness?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions