{"id":13413080,"url":"https://github.com/duanckham/hands","last_synced_at":"2025-03-14T19:31:16.368Z","repository":{"id":57523858,"uuid":"252974027","full_name":"duanckham/hands","owner":"duanckham","description":"Hands is a process controller used to control the execution and return strategies of multiple goroutines.","archived":false,"fork":false,"pushed_at":"2022-04-05T04:12:38.000Z","size":40,"stargazers_count":10,"open_issues_count":1,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-07-31T20:51:54.769Z","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":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/duanckham.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":"2020-04-04T11:04:11.000Z","updated_at":"2022-10-10T08:07:01.000Z","dependencies_parsed_at":"2022-09-15T18:24:30.214Z","dependency_job_id":null,"html_url":"https://github.com/duanckham/hands","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duanckham%2Fhands","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duanckham%2Fhands/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duanckham%2Fhands/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/duanckham%2Fhands/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/duanckham","download_url":"https://codeload.github.com/duanckham/hands/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243635221,"owners_count":20322899,"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":[],"created_at":"2024-07-30T20:01:33.219Z","updated_at":"2025-03-14T19:31:16.053Z","avatar_url":"https://github.com/duanckham.png","language":"Go","readme":"# Hands\n\n[![Mentioned in Awesome Go](https://awesome.re/mentioned-badge.svg)](https://github.com/avelino/awesome-go) \n[![Go Report Card](https://goreportcard.com/badge/github.com/duanckham/hands)](https://goreportcard.com/report/github.com/duanckham/hands)\n[![Build Status](https://travis-ci.com/duanckham/hands.svg?branch=master)](https://travis-ci.com/duanckham/hands)\n[![codecov](https://codecov.io/gh/duanckham/hands/branch/master/graph/badge.svg)](https://codecov.io/gh/duanckham/hands)\n[![LICENSE](https://img.shields.io/github/license/duanckham/hands.svg)](https://github.com/duanckham/hands/blob/master/LICENSE)\n\n#### “Dedicated to Brother Chang”\n\nHands is a process controller used to control the execution and return strategies of multiple goroutines.\n\n## Getting started\n\n### A simple example\n\n```go\nn := 0\ncontroller := hands.New()\n\ncontroller.Do(func(ctx context.Context) error {\n  n++\n  return nil\n})\n\nerr := controller.Run()\nif err != nil {\n  // ...\n}\n\nfmt.Println(n)\n\n// Output:\n// 1\n```\n\nUse the `Do` method to add a task, use the `Run` method to start the task(s).\n\n## TaskOption\n\n`TaskOption` is used to set some metadata for the task.\n\n### `func Priority(priority int32) TaskOption`\n\nUse the `Priority` method to set a priority for a task. The higher the priority, the higher the execution order.\n\n(`hands.P()` is an alias for `hands.Priority()`.)\n\n```go\ncontroller := New()\n\ncontroller.Do(func(ctx context.Context) error {\n  fmt.Println(\"3\")\n  return nil\n}, hands.P(1))\n\ncontroller.Do(func(ctx context.Context) error {\n  fmt.Println(\"2\")\n  return nil\n}, hands.P(2))\n\ncontroller.Do(func(ctx context.Context) error {\n  fmt.Println(\"1\")\n  return nil\n}, hands.P(3))\n\ncontroller.Run()\n\n// Output:\n// 1\n// 2\n// 3\n```\n\n## HandOption\n\nHandOption is used to control the execution strategy of the task.\n\n### `func Fastest() HandOption`\n\n`Fastest()`: When a task is completed, return immediately.\n\n```go\nn := 0\ncontroller := hands.New()\n\ncontroller.Do(func(ctx context.Context) error {\n  time.Sleep(time.Duration(10) * time.Millisecond)\n  n += 1\n  return nil\n})\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 2\n  return nil\n})\n\ncontroller.Run(hands.Fastest())\n\nfmt.Println(n)\n\n// Output:\n// 2\n```\n\n### `func Percentage(percentage float32) HandOption`\n\nWhen a certain percentage of tasks are executed, the results are returned.\n\n```go\nn := 0\ncontroller := hands.New()\n\ncontroller.Do(func(ctx context.Context) error {\n  n++\n  return nil\n})\n\ncontroller.Do(func(ctx context.Context) error {\n  n++\n  return nil\n})\n\ncontroller.Do(func(ctx context.Context) error {\n  n++\n  return nil\n})\n\ncontroller.Do(func(ctx context.Context) error {\n  n++\n  return nil\n})\n\ncontroller.Run(hands.Percentage(0.5))\n\nfmt.Println(n)\n\n// Output:\n// 2\n```\n\n### `func Between(l, r int32) HandOption`\n\n`Between()`: Only execute tasks with a priority within the specified range.\n\n```go\nn := 0\ncontroller := hands.New()\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 1\n  return nil\n}, hands.P(1))\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 2\n  return nil\n}, hands.P(2))\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 3\n  return nil\n}, hands.P(3))\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 4\n  return nil\n}, hands.P(4))\n\ncontroller.Run(hands.Between(2, 3))\n\nfmt.Println(n)\n\n// Output:\n// 5\n```\n\n*Note*: If the use the `controller.Run()` method, tasks outside the `Between()` will not be executed, you can use the `controller.RunAll()` method to allow other priority tasks to be executed asynchronously.\n\n```go\n...\ncontroller.RunAll(hands.Between(2, 3))\n\nfmt.Println(n)\ntime.Sleep(time.Duration(10) * time.Millisecond)\nfmt.Println(n)\n\n// Output:\n// 5\n// 10\n```\n\n### `func In(in []int32) HandOption`\n\n`In()`: Only execute tasks in the specified priority list.\n\n```go\nn := 0\ncontroller := hands.New()\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 1\n  return nil\n}, hands.P(1))\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 2\n  return nil\n}, hands.P(2))\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 3\n  return nil\n}, hands.P(3))\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 4\n  return nil\n}, hands.P(4))\n\ncontroller.Run(hands.In([]int32{2, 4}))\n\nfmt.Println(n)\n\n// Output:\n// 6\n```\n\nYes, the `controller.RunAll()` method can also be used here.\n\n```go\n...\ncontroller.RunAll(hands.In([]int32{2, 4}))\n\nfmt.Println(n)\ntime.Sleep(time.Duration(10) * time.Millisecond)\nfmt.Println(n)\n\n// Output:\n// 6\n// 10\n```\n\n### `func WithContext(ctx context.Context) HandOption`\n\nMake the task use the specified context.\n\n```go\nc, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)\ndefer cancel()\n\ncontroller := hands.New()\n\ncontroller.Do(func(ctx context.Context) error {\n  time.Sleep(time.Duration(100) * time.Millisecond)\n  return nil\n})\n\nerr := controller.Run(hands.WithContext(c))\n\nfmt.Println(err.Error())\n\n// Output:\n// context deadline exceeded\n```\n\n## Callback after all tasks have been executed\n\n```go\nn := 0\ncontroller := hands.New()\n\ncontroller.Do(func(ctx context.Context) error {\n  time.Sleep(time.Duration(10) * time.Millisecond)\n  n++\n  return nil\n})\n\ncontroller.Do(func(ctx context.Context) error {\n  time.Sleep(time.Duration(10) * time.Millisecond)\n  n++\n  return nil\n})\n\ncontroller.Do(func(ctx context.Context) error {\n  n += 5\n  return nil\n})\n\n// Here.\ncontroller.Done(func() {\n  // `True`\n  assert.Equal(t, n, 7)\n})\n\ncontroller.RunAll(Fastest())\n\n// `True`\nassert.Equal(t, n, 5)\ntime.Sleep(time.Duration(500) * time.Millisecond)\n```\n\n---\n\n## License\n[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fduanckham%2Fhands.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fduanckham%2Fhands?ref=badge_large)\n","funding_links":[],"categories":["Goroutines","Goroutines `goroutines的管理和使用`","Relational Databases"],"sub_categories":["Search and Analytic Databases","SQL 查询语句构建库","Advanced Console UIs","检索及分析资料库"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduanckham%2Fhands","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fduanckham%2Fhands","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fduanckham%2Fhands/lists"}