{"id":18319748,"url":"https://github.com/redding/dat-tcp","last_synced_at":"2025-10-24T03:24:33.395Z","repository":{"id":5494810,"uuid":"6693344","full_name":"redding/dat-tcp","owner":"redding","description":"Threaded TCP server","archived":false,"fork":false,"pushed_at":"2018-04-04T21:05:03.000Z","size":147,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-10-07T15:38:44.997Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/redding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-11-14T19:21:06.000Z","updated_at":"2018-10-20T00:47:02.000Z","dependencies_parsed_at":"2022-09-24T00:10:11.355Z","dependency_job_id":null,"html_url":"https://github.com/redding/dat-tcp","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/redding/dat-tcp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdat-tcp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdat-tcp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdat-tcp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdat-tcp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redding","download_url":"https://codeload.github.com/redding/dat-tcp/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fdat-tcp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":280730041,"owners_count":26381119,"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","status":"online","status_checked_at":"2025-10-24T02:00:06.418Z","response_time":73,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-05T18:14:09.400Z","updated_at":"2025-10-24T03:24:33.354Z","avatar_url":"https://github.com/redding.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DatTCP\n\nDatTCP is a generic threaded server implementation using Ruby's `TCPServer`. It is heavily influenced by `GServer` and [Puma](http://puma.io) and built using many of their patterns.\n\n## Usage\n\n```ruby\nclass Worker\n  include DatTCP::Worker\n\n  def work!(socket)\n    message = socket.read\n    socket.write(message)\n  ensure\n    socket.close\n  end\nend\n\nserver = DatTCP::Server.new(Worker)\n```\n\nBuild your own server using `DatTCP::Server` and `DatTCP::Worker`. Define a worker class using the `Worker` mixin and pass it to the server. The server will call the worker's `work!` method for every new connection.\n\nThe server builds many workers which are each run in a separate thread. Each connection is handled by a single worker. The worker and its `work!` method should be threadsafe and expected to be called multiple times (don't use ivars or change global state).\n\n### Starting\n\n```ruby\nserver = DatTCP::Server.new(Worker, :num_workers =\u003e 1)\nserver.listen('localhost', 12000)\nserver.start\n```\n\nCreate an instance of a server and optionally override any default settings. Call `listen` to build a `TCPServer` and bind to an address and port. Finally, call `start` to begin accepting and queueing connections to serve.\n\nThe `start` method returns the thread that is accepting connections.  Typically, you will want to `join` this thread so that it can perpetually accept connections:\n\n```ruby\nserver.start.join\n```\n\nThe server will then continue processing connections until it is signalled to stop or its process is killed.\n\n### Stopping\n\nOnce the server has been started, it can be stopped using the `stop` method. Obviously, this can only be done in the current process if you didn't join the server thread:\n\n```ruby\nserver.stop\n```\n\nIf you plan to join the server thread, it's useful to setup signal traps so you can signal the server to stop:\n\n```ruby\nSignal.trap('TERM'){ server.stop }\nserver.start.join\n```\n\n```sh\n# assume our process id is 12345\n$ kill -TERM 12345\n```\n\n## Customization\n\n### Configuration\n\n* `backlog_size`     - The number of connections that can be pending. These\n                       are connections that haven't been 'accepted' by the\n                       server.\n* `shutdown_timeout` - The number of seconds the server will wait for workers\n                       to finish serving a connection. If they don't finish in\n                       this time, the server will continue shutting down.\n* `num_workers`      - The number of workers (threads) available to handle\n                       connections.\n* `logger`           - A logger to output debug messages to. All messages that\n                       dat-tcp logs are debug level. For the best performance\n                       don't pass a logger.\n* `worker_params`    - Params that are passed to each worker instance. This\n                       provides a way to pass custom data into a worker and\n                       have it available for processing a client scoket. These\n                       are available on every worker and typically shouldn't\n                       be modified.\n\n### Setting TCP server socket options\n\nA DatTCP server allows configuring the TCP server socket it creates. This is done by passing a block to the `listen` method:\n\n```ruby\nserver.listen('localhost', 12000) do |server_socket|\n  server_socket.setsockopt(Socket::SOL_SOCKET, Socket::SO_REUSEADDR, true)\nend\n```\n\n## Benchmarking\n\nDatTCP comes with some scripts for benchmarking it's performance. These generate report text files that should be used to see if any additions or changes have altered it's previous performance. These can be run by doing the following:\n\n```bash\nbundle exec ruby bench/report.rb\n```\n\nThis will both output the results to STDOUT and to a report file. It also generates a server report with some statistics on how long it spent processing.\n\n### Notes\n\n* The bench server is an echo server, it writes back whatever it was sent. Modifying the message sent, from what it currently is, will probably negatively impact performance and can no longer be compared with any historical reports.\n* The calculations should be at a very minute scale (a single request should take around 1ms and probably less). This means it can vary from run to run. I recommend running it ~5 times and keeping the lowest results. In general, requests shouldn't take much longer than a 1ms on average.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem 'dat-tcp'\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install dat-tcp\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fdat-tcp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredding%2Fdat-tcp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fdat-tcp/lists"}