{"id":13631697,"url":"https://github.com/socketry/async-io","last_synced_at":"2025-04-17T22:31:34.758Z","repository":{"id":49888782,"uuid":"92013809","full_name":"socketry/async-io","owner":"socketry","description":"Concurrent wrappers for native Ruby IO \u0026 Sockets.","archived":true,"fork":false,"pushed_at":"2024-08-29T21:02:34.000Z","size":633,"stargazers_count":208,"open_issues_count":10,"forks_count":28,"subscribers_count":16,"default_branch":"main","last_synced_at":"2024-10-31T12:57:40.936Z","etag":null,"topics":["concurrency","io","ruby","sockets"],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/socketry.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"license.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-22T05:13:55.000Z","updated_at":"2024-10-15T22:40:51.000Z","dependencies_parsed_at":"2023-02-16T04:00:33.091Z","dependency_job_id":"63399af1-fb4a-42c7-902f-a218f2351296","html_url":"https://github.com/socketry/async-io","commit_stats":{"total_commits":453,"total_committers":12,"mean_commits":37.75,"dds":"0.050772626931567366","last_synced_commit":"f95e31f5722894c5770e5ef1aa12bd6328ca6d25"},"previous_names":[],"tags_count":98,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Fasync-io","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Fasync-io/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Fasync-io/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/socketry%2Fasync-io/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/socketry","download_url":"https://codeload.github.com/socketry/async-io/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223768631,"owners_count":17199356,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["concurrency","io","ruby","sockets"],"created_at":"2024-08-01T22:02:34.884Z","updated_at":"2024-11-08T23:31:28.503Z","avatar_url":"https://github.com/socketry.png","language":"Ruby","readme":"# Async::IO\n\n\u003e [!CAUTION]\n\u003e This library is deprecated and should not be used in new projects. Instead, you should use \u003chttps://github.com/socketry/io-endpoint\u003e for the endpoint-related functionality, and \u003chttps://github.com/socketry/io-stream\u003e for stream/buffering functionality.\n\nAsync::IO provides builds on [async](https://github.com/socketry/async) and provides asynchronous wrappers for `IO`, `Socket`, and related classes.\n\n[![Development Status](https://github.com/socketry/async-io/workflows/Test/badge.svg)](https://github.com/socketry/async-io/actions?workflow=Test)\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n``` ruby\ngem 'async-io'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install async-io\n\n## Usage\n\nBasic echo server (from `spec/async/io/echo_spec.rb`):\n\n``` ruby\nrequire 'async/io'\n\ndef echo_server(endpoint)\n  Async do |task|\n    # This is a synchronous block within the current task:\n    endpoint.accept do |client|\n      # This is an asynchronous block within the current reactor:\n      data = client.read\n\n      # This produces out-of-order responses.\n      task.sleep(rand * 0.01)\n\n      client.write(data.reverse)\n      client.close_write\n    end\n  end\nend\n\ndef echo_client(endpoint, data)\n  Async do |task|\n    endpoint.connect do |peer|\n      peer.write(data)\n      peer.close_write\n\n      message = peer.read\n\n      puts \"Sent #{data}, got response: #{message}\"\n    end\n  end\nend\n\nAsync do\n  endpoint = Async::IO::Endpoint.tcp('0.0.0.0', 9000)\n\n  server = echo_server(endpoint)\n\n  5.times.map do |i|\n    echo_client(endpoint, \"Hello World #{i}\")\n  end.each(\u0026:wait)\n\n  server.stop\nend\n```\n\n### Timeouts\n\nTimeouts add a temporal limit to the execution of your code. If the IO doesn't respond in time, it will fail. Timeouts are high level concerns and you generally shouldn't use them except at the very highest level of your program.\n\n``` ruby\nmessage = task.with_timeout(5) do\n  begin\n    peer.read\n  rescue Async::TimeoutError\n    nil # The timeout was triggered.\n  end\nend\n```\n\nAny `yield` operation can cause a timeout to trigger. Non-`async` functions might not timeout because they are outside the scope of `async`.\n\n#### Wrapper Timeouts\n\nAsynchronous operations may block forever. You can assign a per-wrapper operation timeout duration. All asynchronous operations will be bounded by this timeout.\n\n``` ruby\npeer.timeout = 1\npeer.read # If this takes more than 1 second, Async::TimeoutError will be raised.\n```\n\nThe benefit of this approach is that it applies to all operations. Typically, this would be configured by the user, and set to something pretty high, e.g. 120 seconds.\n\n### Reading Characters\n\nThis example shows how to read one character at a time as the user presses it on the keyboard, and echos it back out as uppercase:\n\n``` ruby\nrequire 'async'\nrequire 'async/io/stream'\nrequire 'io/console'\n\n$stdin.raw!\n$stdin.echo = false\n\nAsync do |task|\n  stdin = Async::IO::Stream.new(\n    Async::IO::Generic.new($stdin)\n  )\n\n  while character = stdin.read(1)\n    $stdout.write character.upcase\n  end\nend\n```\n\n### Deferred Buffering\n\n`Async::IO::Stream.new(..., deferred:true)` creates a deferred stream which increases latency slightly, but reduces the number of total packets sent. It does this by combining all calls `Stream#flush` within a single iteration of the reactor. This is typically more useful on the client side, but can also be useful on the server side when individual packets have high latency. It should be preferable to send one 100 byte packet than 10x 10 byte packets.\n\nServers typically only deal with one request per iteartion of the reactor so it's less useful. Clients which make multiple requests can benefit significantly e.g. HTTP/2 clients can merge many requests into a single packet. Because HTTP/2 recommends disabling Nagle's algorithm, this is often beneficial.\n\n## Contributing\n\nWe welcome contributions to this project.\n\n1.  Fork it.\n2.  Create your feature branch (`git checkout -b my-new-feature`).\n3.  Commit your changes (`git commit -am 'Add some feature'`).\n4.  Push to the branch (`git push origin my-new-feature`).\n5.  Create new Pull Request.\n\n### Developer Certificate of Origin\n\nThis project uses the [Developer Certificate of Origin](https://developercertificate.org/). All contributors to this project must agree to this document to have their contributions accepted.\n\n### Contributor Covenant\n\nThis project is governed by the [Contributor Covenant](https://www.contributor-covenant.org/). All contributors and participants agree to abide by its terms.\n\n## See Also\n\n  - [async](https://github.com/socketry/async) — Asynchronous event-driven reactor.\n  - [async-process](https://github.com/socketry/async-process) — Asynchronous process spawning/waiting.\n  - [async-websocket](https://github.com/socketry/async-websocket) — Asynchronous client and server websockets.\n  - [async-dns](https://github.com/socketry/async-dns) — Asynchronous DNS resolver and server.\n  - [async-rspec](https://github.com/socketry/async-rspec) — Shared contexts for running async specs.\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketry%2Fasync-io","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsocketry%2Fasync-io","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsocketry%2Fasync-io/lists"}