{"id":26540470,"url":"https://github.com/mccutchen/speculatively","last_synced_at":"2025-07-23T21:32:44.794Z","repository":{"id":47073495,"uuid":"133579408","full_name":"mccutchen/speculatively","owner":"mccutchen","description":"Package speculatively provides a simple mechanism to re-execute a task in parallel only after some initial timeout has elapsed.","archived":false,"fork":false,"pushed_at":"2025-02-26T04:10:49.000Z","size":19,"stargazers_count":10,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-26T05:20:39.559Z","etag":null,"topics":["golang","speculative-execution"],"latest_commit_sha":null,"homepage":"https://pkg.go.dev/github.com/mccutchen/speculatively","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/mccutchen.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":"2018-05-15T22:20:23.000Z","updated_at":"2025-02-26T04:10:52.000Z","dependencies_parsed_at":"2024-06-19T17:11:40.315Z","dependency_job_id":"23f6a7d1-9122-4107-bec0-3f8a694de9a6","html_url":"https://github.com/mccutchen/speculatively","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mccutchen%2Fspeculatively","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mccutchen%2Fspeculatively/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mccutchen%2Fspeculatively/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mccutchen%2Fspeculatively/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mccutchen","download_url":"https://codeload.github.com/mccutchen/speculatively/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244890102,"owners_count":20527030,"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":["golang","speculative-execution"],"created_at":"2025-03-22T00:33:36.203Z","updated_at":"2025-03-22T00:33:47.036Z","avatar_url":"https://github.com/mccutchen.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# speculatively\n\n[![Documentation](https://pkg.go.dev/badge/github.com/mccutchen/speculatively)](https://pkg.go.dev/github.com/mccutchen/speculatively)\n[![Build status](https://github.com/mccutchen/speculatively/actions/workflows/test.yaml/badge.svg)](https://github.com/mccutchen/speculatively/actions/workflows/test.yaml)\n[![Code coverage](https://codecov.io/gh/mccutchen/speculatively/branch/main/graph/badge.svg)](https://codecov.io/gh/mccutchen/speculatively)\n[![Go report card](http://goreportcard.com/badge/github.com/mccutchen/speculatively)](https://goreportcard.com/report/github.com/mccutchen/speculatively)\n\nPackage `speculatively` provides a simple mechanism to speculatively execute a\ntask in parallel only after some initial timeout has elapsed:\n\n```go\n// An example task that will wait for a random amount of time before returning\ntask := func(ctx context.Context) (string, error) {\n    delay := time.Duration(float64(250*time.Millisecond) * rand.Float64())\n    select {\n    case \u003c-time.After(delay):\n        return \"success\", nil\n    case \u003c-ctx.Done():\n        return \"timeout\", ctx.Err()\n    }\n}\n\nctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)\ndefer cancel()\n\n// If task doesn't return within 20ms, it will be executed again in parallel\nresult, err := speculatively.Do(ctx, 20*time.Millisecond, task)\n```\n\nThis was inspired by the [\"Defeat your 99th percentile with speculative task\"\nblog post][1], which describes it nicely:\n\n\u003e The inspiration came from BigData world. In Spark when task execution runs\n\u003e suspiciously long the application master starts the same task speculatively\n\u003e on a different executor but it lets the long running tasks to continue. The\n\u003e solution looked elegant:\n\u003e\n\u003e  * Service response time limit is 50ms.\n\u003e\n\u003e  * If the first attempt doesn’t finish within 25ms start a new one, but\n\u003e    keep the first thread running.\n\u003e\n\u003e  * Wait for either thread to finish and take result from the first one\n\u003e    ready.\n\nThe speculative tasks implemented here are similar to \"hedged requests\" as\ndescribed in [The Tail at Scale][2] and implemented in the Query example\nfunction in [Go Concurrency Patterns: Timing out, moving on][3], but they a)\nhave no knowledge of different replicas and b) wait for a caller-controlled\ntimeout before launching additional tasks.\n\n[1]: https://archive.is/QDqM3\n[2]: http://www-inst.eecs.berkeley.edu/~cs252/sp17/papers/TheTailAtScale.pdf\n[3]: https://blog.golang.org/go-concurrency-patterns-timing-out-and\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmccutchen%2Fspeculatively","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmccutchen%2Fspeculatively","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmccutchen%2Fspeculatively/lists"}