{"id":15557402,"url":"https://github.com/icholy/killable","last_synced_at":"2025-08-17T13:31:45.786Z","repository":{"id":29586538,"uuid":"33126409","full_name":"icholy/killable","owner":"icholy","description":"Composable cancellation","archived":false,"fork":false,"pushed_at":"2021-05-28T20:41:27.000Z","size":145,"stargazers_count":46,"open_issues_count":1,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-04T09:01:41.146Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Go","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/icholy.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":"2015-03-30T14:00:56.000Z","updated_at":"2022-04-09T15:35:26.000Z","dependencies_parsed_at":"2022-09-03T18:11:38.533Z","dependency_job_id":null,"html_url":"https://github.com/icholy/killable","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/icholy/killable","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icholy%2Fkillable","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icholy%2Fkillable/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icholy%2Fkillable/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icholy%2Fkillable/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/icholy","download_url":"https://codeload.github.com/icholy/killable/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/icholy%2Fkillable/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270856561,"owners_count":24657688,"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-08-17T02:00:09.016Z","response_time":129,"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-10-02T15:17:41.698Z","updated_at":"2025-08-17T13:31:45.514Z","avatar_url":"https://github.com/icholy.png","language":"Go","readme":"# DEPRECATED - THIS PACKAGE IS BROKEN AND I'M NOT GOING TO FIX IT\n\n# Killable [![Build Status](https://travis-ci.org/icholy/killable.svg?branch=master)](https://travis-ci.org/icholy/killable)\n\n\u003e A package for graceful shutdowns (inspired by tomb)\n\n## States\n\nA `Killable` represents a group of goroutines. It goes through 3 stages:\n\n0. Alive - The goroutines are running\n0. Dying - The goroutines are being signaled to terminate\n0. Dead  - All managed goroutines have terminated\n\n![](images/states.jpg)\n\nThere are two ways a `Killable` can enter the dying state.\n\n0. One of the managed goroutines returns a non `nil` `error`\n0. The `Kill(error)` method is invoked on the `Killable`.\n\n## Managed Goroutines\n\nGoroutines managed by the `Killable` are started with `killable.Go`\n\n\n``` go\nk := killable.New()\n\ngo func() {\n  \u003c-k.Dying()\n  fmt.Println(\"Dying\")\n\n  \u003c-k.Dead()\n  fmt.Println(\"Dead\")\n}()\n\nkillable.Go(k, func() error {\n  time.Sleep(5 * time.Second)\n  fmt.Println(\"Finished sleeping, i'll be dead soon\")\n  return nil\n})\n\nk.Kill(fmt.Errorf(\"it's time to die!\"))\n```\n\n* A `Killable` is not dead until all managed goroutines have returned.\n* If the goroutine returns a non `nil` `error`, the `Killable` starts dying.\n* If the `Killable` is already dying when the `Go` method is invoked, it does not run.\n\n## Defer\n\n`Defer` is similar to the `defer` keyword. \n\n``` go\nfunc Connect(k killable.Killable) (*sql.DB, error) {\n\n  db, err := sql.Open(\"foo\", \"bar\")\n  if err != nil {\n    return nil, err\n  }\n\n  // clean up resources near instantiation\n  killable.Defer(k, func() {\n    db.Close()\n  })\n\n  return db, nil\n}\n```\n\n* Deferred methods are called once the killable is dead.\n* Deferred methods are invoked in the opposite order they were defined (lifo).\n\n## Linking\n\n`Killable`s can be linked to eachother in a parent/child relationship.\n\n* If a child is killed, the parent is also killed.\n* If the parent is killed, it kills all the children.\n* If the `reason` is `ErrKillLocal`, the parent ignores it.\n* The parent doesn't die until all the children are dead\n\n``` go\n\nfunc makeChild(d time.Duration) killable.Killable {\n  k := killable.New()\n\n  killable.Go(k, func() {\n\n    // Sleep will immediately return ErrDying if the Killable\n    // enters the dying state during the sleep\n    // (see source to see how to implement similar methods)\n    if err := killable.Sleep(k, d); err != nil {\n      return err\n    }\n\n    return killable.ErrKill\n  })\n\n  return k\n}\n\nvar (\n  // children\n  k1 = makeChild(4 * time.Second)\n  k2 = makeChild(3 * time.Second)\n  k3 = makeChild(2 * time.Second)\n\n  // parent\n  k4 = killable.New(k1, k2, k3)\n)\n\nkillable.Defer(k4, func() {\n  fmt.Println(\"All children are dead!\")\n})\n\ngo func() {\n  \u003c-k4.Dying()\n  fmt.Println(\"Killing all children\")\n}()\n\n```\n\n![](images/killable.gif)\n\nSee `examples/` directory.\n\nThe methods like `Defer`, `Go`, `Do`, etc ...  have been placed in the packages because the `Killable` type is meant to be embedded. The interface the `Killable` type exposes makes sense without understanding the `killable` package.\n\n## Context\n\nSince Go 1.7, the many standard library functions have support for `context.Context`. The `killable.Killable` interface cannot be changed to conform to the `context.Context` interface because the semantics of the `Err` method. To get around this, `killable.Killable` has a `.Context()`.\n\n``` go\nfunc DoQuery(k killable.Killable, db *sql.DB) error {\n  _, err := db.ExecContext(k.Context(), \"INSERT foo INTO bar\")\n  return err\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficholy%2Fkillable","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ficholy%2Fkillable","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ficholy%2Fkillable/lists"}