{"id":13713520,"url":"https://github.com/cirocosta/http-ctx-cancellation","last_synced_at":"2026-05-02T03:31:51.785Z","repository":{"id":73883840,"uuid":"230296693","full_name":"cirocosta/http-ctx-cancellation","owner":"cirocosta","description":"golang's request timeout from context cancellation exemplified in C","archived":false,"fork":false,"pushed_at":"2019-12-27T23:10:38.000Z","size":352,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-05T03:42:39.732Z","etag":null,"topics":["c","go","http"],"latest_commit_sha":null,"homepage":"https://ops.tips","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/cirocosta.png","metadata":{"files":{"readme":"README.txt","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2019-12-26T16:45:15.000Z","updated_at":"2019-12-28T08:41:55.000Z","dependencies_parsed_at":null,"dependency_job_id":"69bbc79d-6671-4367-a2cf-c1b158997f42","html_url":"https://github.com/cirocosta/http-ctx-cancellation","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/cirocosta/http-ctx-cancellation","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cirocosta%2Fhttp-ctx-cancellation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cirocosta%2Fhttp-ctx-cancellation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cirocosta%2Fhttp-ctx-cancellation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cirocosta%2Fhttp-ctx-cancellation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cirocosta","download_url":"https://codeload.github.com/cirocosta/http-ctx-cancellation/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cirocosta%2Fhttp-ctx-cancellation/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32522245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-02T01:12:54.858Z","status":"online","status_checked_at":"2026-05-02T02:00:05.923Z","response_time":132,"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":["c","go","http"],"created_at":"2024-08-02T23:01:38.537Z","updated_at":"2026-05-02T03:31:51.770Z","avatar_url":"https://github.com/cirocosta.png","language":"C","funding_links":[],"categories":["Repositories"],"sub_categories":[],"readme":"WHAT'S THIS\n\n\tthis repo is intended to play around w/ context cancellation in Go in\n\tthe \"context\" of http requests.\n\t\n\n\tit contains:\n\n\t\t- http client in C that w/ a timeout of `n` seconds \n\t\t  - plain use of epoll and timerfd\n\t\t- http client in Go w/ a timeout of `n` seconds \n\t\t  - using ctx cancellation\n\t\t- http server in Go that sleeps for N seconds \n\n\tby making the server not respond on a given amount of time, we can\n\tsimulate cases where you'd want to timeout the request.\n\n\tusing `go`, one can do so via `http.NewRequestWithContext()`, giving it\n\ta context that gets cancelled after a given deadline is reached.\n\n\tunder the hood, such cancellation is done through a socket shutdown\n\t(plain `close(2)`) on a nonblocking socket that's part of an epoll\n\tfacility\n\n\n\n\t\tstrace -f -e 'trace=!futex,nanosleep' ./http\n\n\n\t\t1. non-blocking socket gets created\n\t\t\t[pid 17751] socket(AF_INET, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, IPPROTO_IP) = 3\n\t\t\t[pid 17751] setsockopt(3, SOL_SOCKET, SO_BROADCAST, [1], 4) = 0\n\t\t\t[pid 17751] connect(3, {sa_family=AF_INET, sin_port=htons(1337), sin_addr=inet_addr(\"127.0.0.1\")}, 16) = -1 \n\t\t\t\tEINPROGRESS (Operation now in progress)\n\n\t\t2. socket gets added to epoll facility\n\n\t\t\t[pid 17751] epoll_ctl(4, EPOLL_CTL_ADD, 3, {\n\t\t\t\tEPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLET, {u32=812239112, u64=139655968835848}}) = 0\n\n\t\t3. wait on the fds that were added - once `connect` finishes, we\n\t\t   should get an event for that fd\n\n\t\t\t[pid 17751] epoll_pwait(4, [{\n\t\t\t\tEPOLLOUT, {u32=812239112, u64=139655968835848}}], 128, 0, NULL, 824634156712) = 1\n\n\t\t4. check if there were any errors in the conn\n\n\t\t\t[pid 17751] getsockopt(3, SOL_SOCKET, SO_ERROR,  \u003cunfinished ...\u003e\n\n\t\t5. write to the socket\n\n\t\t\t[pid 17751] write(3, \"GET / HTTP/1.1\\r\\nHost: localhost:\"..., 95\n\n\t\t6. try to read from it\n\n\t\t\t[pid 17757] read(3,  \u003cunfinished ...\u003e\n\t\t\t[pid 17757] \u003c... read resumed\u003e 0xc00011e000, 4096) \n\t\t\t\t-1 EAGAIN (Resource temporarily unavailable)\n\t\t\t\t// ...\n\n\t\t7. would block, so lets continue waiting ..\n\n\t\t8. deadline reached, all we gotta do is remove from the set and\n\t\t   close it\n\n\t\t\t[pid 17755] epoll_ctl(4, EPOLL_CTL_DEL, 3, 0xc000132984) = 0\n\t\t\t[pid 17755] close(3)                    = 0\n\n\n\n\tnaturally, we can implement that in C - see ./http-client.c\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcirocosta%2Fhttp-ctx-cancellation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcirocosta%2Fhttp-ctx-cancellation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcirocosta%2Fhttp-ctx-cancellation/lists"}