Skip to content

Release v0.8.0

Compare
Choose a tag to compare
@wallyqs wallyqs released this 22 Sep 17:51
· 19 commits to master since this release
f271ca8

Major version update of the client with internals reworked a lot in order to have structure and API similar to the Asyncio NATS client for Python3.

Includes several bugfixes for reconnection logic, ping interval and flush API, Tornado 5.X support and newer features such as no echo and drain mode support.

Added

  • Drain API (#56)

    This feature allows clients to gracefully disconect, letting the subscribers
    handle any inflight messages that may have been sent by the server already.

    import tornado.gen
    import tornado.concurrent
    
    from nats.io import Client as NATS
    from nats.io.errors import ErrNoServers
    from tornado.ioloop import IOLoop as loop
    
    @tornado.gen.coroutine
    def main():
      nc = NATS()
    
      conn_closed = tornado.concurrent.Future()
    
      def closed_cb():
        conn_closed.set_result(True)
    
      # Set pool servers in the cluster and give a name to the client.
      yield nc.connect("127.0.0.1:4222", closed_cb=closed_cb)
    
      @tornado.gen.coroutine
      def handler(msg):
        # Can check whether client is in draining state
        if nc.is_draining:
            print("[Status  ] Draining, will disconnect soon...")
    
        print("[Received] {}".format(msg.data))
        yield nc.publish(msg.reply, b'I can help')
    
      yield nc.subscribe("help", "workers", cb=handler)
    
      responses = []
    
      @tornado.gen.coroutine
      def send_requests():
        # Send 100 async requests and wait for all the responses.
        for i in range(0, 1000):
          try:
            response = yield nc.request("help", '[{}] help!'.format(i), timeout=0.1)
            responses.append(response)
          except:
            break
    
      loop.current().spawn_callback(send_requests)
      yield tornado.gen.sleep(1)
    
      # Gracefully close the connection and wait for closed callback to be signaled.
      yield nc.drain()
      yield conn_closed
    
      print("Received {} responses".format(len(responses)))
    
    if __name__ == '__main__':
        loop.current().run_sync(main)
  • No echo mode (#55)

    When connected to a NATS Server v1.2.0 or above, a client can now opt to avoid
    receiving messages that it itself has published.

    yield ncA.connect(no_echo=True)
    yield ncB.connect()
    
    @tornado.gen.coroutine
    def handler(msg):
      # Messages sent by `ncA' will not be received.
      print("[Received] ", msg)
    
    yield ncA.subscribe("greetings", cb=handler)
    yield ncA.publish("greetings", b'Hello World!')
    yield ncB.publish("greetings", b'Hello World!')
  • User, Password and Auth Token options (#57)

    Added user, password, token parameters to set the auth credentials for servers that were discovered implicitly.

    # Set user and info credentials
    yield nc.connect("127.0.0.1:4222", user="foo", password="bar")
    
    # Token
    yield nc.connect("127.0.0.1:4222", token="secretoken")

Improved

  • Improved connect API using URLs like in the Go client (#54)

    Now possible to connect to a NATS cluster with any of the following ways:

    # Assume 'nats://' scheme
    await nc.connect("demo.nats.io:4222")
    
    # Complete with scheme a la Go client classic usage.
    await nc.connect("nats://demo.nats.io:4222")
    
    # Use default 4222 port.
    yield nc.connect("demo.nats.io")
    
    # TLS example
    await nc.connect("tls://demo.nats.io:4443")

Fixed

  • Fixed error callback not being called on connect errors (#50)

  • Fixed client not stopping to reconnect against servers after authorization failed more than max_reconnect_attempts (#51)

  • Fixed calling close callback whenever the TCP connection was disconnected.
    Now it will only be called once when the client disconnects and gives up connecting to NATS (#51)

  • Fixed issue when multiple Flush calls would result in unexpected disconnections (3aa4463)

Changed

  • Updated client to work with Tornado 5.X series, which is last version to be compatible with Python 2 (#53)

Deprecated

  • io_loop usage which has been deprecated in Tornado 5.X
  • close_cb is deprecated and recommended to use closed_cb instead to be consistent with the Asyncio NATS client