{"id":17892343,"url":"https://github.com/trskop/connection-pool","last_synced_at":"2025-03-23T00:32:33.625Z","repository":{"id":20462418,"uuid":"23739754","full_name":"trskop/connection-pool","owner":"trskop","description":"Connection pool built on top of resource-pool and streaming-commons packages.","archived":false,"fork":false,"pushed_at":"2018-06-17T19:11:39.000Z","size":107,"stargazers_count":7,"open_issues_count":1,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-12-06T19:09:09.177Z","etag":null,"topics":["connection-pool","haskell","resource-pool","tcp","tcp-client","unix-sockets","unix-sockets-client"],"latest_commit_sha":null,"homepage":null,"language":"Haskell","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/trskop.png","metadata":{"files":{"readme":"README.md","changelog":"ChangeLog.md","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":"2014-09-06T16:55:49.000Z","updated_at":"2022-06-01T17:45:19.000Z","dependencies_parsed_at":"2022-07-31T20:47:59.432Z","dependency_job_id":null,"html_url":"https://github.com/trskop/connection-pool","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trskop%2Fconnection-pool","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trskop%2Fconnection-pool/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trskop%2Fconnection-pool/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/trskop%2Fconnection-pool/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/trskop","download_url":"https://codeload.github.com/trskop/connection-pool/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245040235,"owners_count":20551297,"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":["connection-pool","haskell","resource-pool","tcp","tcp-client","unix-sockets","unix-sockets-client"],"created_at":"2024-10-28T14:35:57.991Z","updated_at":"2025-03-23T00:32:32.810Z","avatar_url":"https://github.com/trskop.png","language":"Haskell","funding_links":[],"categories":[],"sub_categories":[],"readme":"Connection Pool\n===============\n\n[![Hackage](http://img.shields.io/hackage/v/connection-pool.svg)\n][Hackage: connection-pool]\n[![Hackage Dependencies](https://img.shields.io/hackage-deps/v/connection-pool.svg)](http://packdeps.haskellers.com/reverse/connection-pool)\n[![Haskell Programming Language](https://img.shields.io/badge/language-Haskell-blue.svg)][Haskell.org]\n[![BSD3 License](http://img.shields.io/badge/license-BSD3-brightgreen.svg)][tl;dr Legal: BSD3]\n\n[![Build](https://travis-ci.org/trskop/connection-pool.svg)](https://travis-ci.org/trskop/connection-pool)\n\n\nDescription\n-----------\n\nConnection pool is a family of specialised resource pools. Currently package\nprovides two\n\n1. pool for TCP client connections,\n2. and pool for UNIX Sockets client connections.\n\nIn addition it can be used to build your own connection pool using provided primitives.\n\nThis package is built on top of [resource-pool][Hackage: resource-pool] and\n[streaming-commons][Hackage: streaming-commons]. The later allows us to use\n[conduit-extra][Hackage: conduit-extra] package for implementation of TCP or\nUNIX Sockets clients.\n\n\nDocumentation\n-------------\n\nStable releases with API documentation are available on\n[Hackage][Hackage: connection-pool]\n\n\nExamples\n--------\n\nSimple code examples, including example from the following section, are\navailable in [example/](example/) directory.\n\n\nTCP Client Example\n------------------\n\nHere is a simple example that demonstrates how TCP client can be created and\nhow connection pool behaves.\n\n```haskell\n{-# LANGUAGE OverloadedStrings #-}\nmodule Main (main)\n  where\n\nimport Control.Concurrent\n    ( forkIO\n    , newEmptyMVar\n    , putMVar\n    , readMVar\n    , threadDelay\n    )\nimport Control.Monad (void, mapM_)\nimport System.Environment (getArgs)\n\nimport Control.Lens ((.~), (\u0026))\nimport Data.ConnectionPool\n    ( createTcpClientPool\n    , numberOfResourcesPerStripe\n    , numberOfStripes\n    , withTcpClientConnection\n    )\nimport Data.Default.Class (Default(def))\nimport Data.Streaming.Network (appWrite, clientSettingsTCP)\n\n\nmain :: IO ()\nmain = do\n    [port, numStripes, numPerStripe] \u003c- getArgs\n    pool \u003c- createTcpClientPool\n        (poolParams numStripes numPerStripe)\n        (clientSettingsTCP (read port) \"127.0.0.1\")\n    thread1 \u003c- newEmptyMVar\n    thread2 \u003c- newEmptyMVar\n    void . forkIO . withTcpClientConnection pool $ \\appData -\u003e do\n        threadDelay 1000\n        appWrite appData \"1: I'm alive!\\n\"\n        putMVar thread1 ()\n    void . forkIO . withTcpClientConnection pool $ \\appData -\u003e do\n        appWrite appData \"2: I'm alive!\\n\"\n        putMVar thread2 ()\n    mapM_ readMVar [thread1, thread2]\n  where\n    poolParams m n =\n        def \u0026 numberOfStripes .~ read m\n            \u0026 numberOfResourcesPerStripe .~ read n\n```\n\nTo test it we can use `socat` or some `netcat` like application. Our test will\nrequire two terminals, in one we will execute `socat` as a server listenting on\nUNIX socket and in the other one we execute above example.\n\nSimple TCP server listening on port `8001` that prints what it receives to\nstdout:\n\n    $ socat TCP4-LISTEN:8001,bind=127.0.0.1,fork -\n\nThe `fork` parameter in the above example is important, otherwise `socat` would\nterminate when client closes its connection.\n\nIf we run above example as:\n\n    $ runghc tcp-example.hs 8001 1 1\n\nWe can see that `socat` received following text:\n\n    1: I'm alive!\n    2: I'm alive!\n\nBut if we increment number of stripes or number of connections (resources) per\nstripe, then we will get:\n\n    2: I'm alive!\n    1: I'm alive!\n\nThe reason for this is that we use `threadDelay 1000` in the first executed\nthread. So when we have only one stripe and one connection per stripe, then we\nhave only one connection in the pool. Therefore when the first thread executes\nand acquires a connection, then all the other threads (the other one in above\nexample) will block. If we have more then one connection available in our pool,\nthen the first thread acquires connection, blocks on `threadDelay` call, but\nthe other thread also acquires connection and prints its output while the first\nthread is still blocked on `threadDelay`. This example demonstrates how\nconnection pool behaves if it reached its capacity and when it has enough free\nresources.\n\n\nLicense\n-------\n\nThe BSD 3-Clause License, see [LICENSE](LICENSE) file for details.\n\n\nContributions\n-------------\n\nContributions, pull requests and bug reports are welcome! Please don't be\nafraid to contact author using GitHub or by e-mail (see `.cabal` file for\nthat).\n\n\n\n[Hackage: conduit-extra]: http://hackage.haskell.org/package/conduit-extra\n[Hackage: connection-pool]: http://hackage.haskell.org/package/connection-pool\n[Hackage: resource-pool]: http://hackage.haskell.org/package/resource-pool\n[Hackage: streaming-commons]: http://hackage.haskell.org/package/streaming-commons\n[Haskell.org]: http://www.haskell.org \"The Haskell Programming Language\"\n[tl;dr Legal: BSD3]: https://tldrlegal.com/license/bsd-3-clause-license-%28revised%29 \"BSD 3-Clause License (Revised)\"\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrskop%2Fconnection-pool","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftrskop%2Fconnection-pool","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftrskop%2Fconnection-pool/lists"}