{"id":15713741,"url":"https://github.com/sam0x17/lake","last_synced_at":"2025-07-19T13:10:49.738Z","repository":{"id":82773131,"uuid":"273659715","full_name":"sam0x17/lake","owner":"sam0x17","description":"A simple connection pooling shard for the crystal language suitable for use with Redis and probably other things","archived":false,"fork":false,"pushed_at":"2020-07-08T01:39:43.000Z","size":19,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-30T18:47:30.962Z","etag":null,"topics":["crystal-lang","crystal-language","crystal-shard","fibers","pooling","redis-client"],"latest_commit_sha":null,"homepage":"","language":"Crystal","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/sam0x17.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-20T07:25:38.000Z","updated_at":"2020-07-08T01:39:45.000Z","dependencies_parsed_at":null,"dependency_job_id":"5e6d50c7-846d-4fe5-9acd-302fcd6a4a22","html_url":"https://github.com/sam0x17/lake","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/sam0x17/lake","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sam0x17%2Flake","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sam0x17%2Flake/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sam0x17%2Flake/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sam0x17%2Flake/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sam0x17","download_url":"https://codeload.github.com/sam0x17/lake/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sam0x17%2Flake/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263421460,"owners_count":23464012,"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":["crystal-lang","crystal-language","crystal-shard","fibers","pooling","redis-client"],"created_at":"2024-10-03T21:33:09.596Z","updated_at":"2025-07-03T23:33:39.727Z","avatar_url":"https://github.com/sam0x17.png","language":"Crystal","funding_links":[],"categories":[],"sub_categories":[],"readme":"# lake\nLake is a generic connection pooling shard for the crystal programming language. With\nLake, you can create a generically typed `Lake(T)` for whatever type of connection\nor object you want to work with.\n\n## Overview\n\nWhen you want to use a connection/object in the pool, simply call `#dip` to\nasynchronously get a reference to a free connection in the pool and use it within\nthe block you provide, or call `#dip_sync` with the same parameters for a synchronous\nversion of `#dip`.\n\n```crystal\nlake = Lake(Redis).new\nlake.dip { |redis| puts redis.get(\"my-key\") }\nlake.dip { |redis| redis.set(\"my-key\", \"cool\") }\n# no guarantee on run order since `#dip` is asynchronous\n```\nAnd using `#dip_sync`...\n\n```crystal\nlake = Lake(Redis).new\nlake.dip_sync { |redis| redis.set(\"my-key\", \"hello\") }\nval = nil\nlake.dip_sync { |redis| val = redis.get(\"my-key\") }\nval.should eq \"hello\"\n```\n\nWhen using things like `redis` where certain connection operations, such as pub/sub,\nmake the connection unusable for a period of time, you can use `#leak` which will\nreturn and remove a connection from the pool and replace it with a new one safely.\n\n```crystal\nlake = Lake(Redis).new\nredis = lake.leak\nspawn do\n  redis.subscribe(\"my-key\") do |on|\n  ...\n```\n\n Connections are returned by `dip` and `dip_sync` on a least-recently-used basis, to\n ensure that we are always minimizing the chance that another operation is currently\n in-progress on the returned connection.\n\nLake maintains a channel for each connection in the pool which it uses to buffer incoming\n`dip` and `dip_sync` requests.\n\nYou can also overload the default constructor which calls `.new` on whatever object you\nhave decided to use as a pool entry type, as well as specify the size of the lake.\n\n```crystal\nlake = Lake(Redis).new(50)\n```\n\nThe second optional parameter allows you to override the default \"factory\" (`T.new`) for\nnewly created pool objects by passing a `-\u003e{ }` block returning a `T`.\n\n```crystal\nlake = Lake(MyClass).new(25, -\u003e{ MyClass.new(\"some_arg\") })\n```\n\nPool objects are initialized at pool creation time using this block, and a new object\nis also initialized each time you call `lake.leak`.\n\nIf you do not specify a pool size, the default is `24` (`Lake::DEFAULT_CAPACITY`).\n\n## How it Works\nA thread-safe queue (channel) is maintained for each object in the pool. When you pass\na block that takes a pool object to `dip_sync` or `dip`, the block is sent over the\nappropriate channel and processed when the pool object is done with any other pending\njobs that were already queued for it specifically. Pool objects are accessed via a\nleast-recently-used pattern to minimize the chances that you are given a pool object\nthat is still busy.\n\nAn event loop is also created for each pool object. The event loop will run until\nthe object is taken out of the pool via `leak` or until `clear` is called.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsam0x17%2Flake","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsam0x17%2Flake","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsam0x17%2Flake/lists"}