{"id":19298644,"url":"https://github.com/ibbd-dev/go-cond-strategy","last_synced_at":"2025-07-01T13:07:05.362Z","repository":{"id":117525260,"uuid":"74662549","full_name":"ibbd-dev/go-cond-strategy","owner":"ibbd-dev","description":"Golang条件策略框架及服务降级框架","archived":false,"fork":false,"pushed_at":"2016-11-29T13:59:15.000Z","size":21,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-24T01:15:26.969Z","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/ibbd-dev.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":"2016-11-24T10:26:40.000Z","updated_at":"2024-08-21T07:53:04.000Z","dependencies_parsed_at":null,"dependency_job_id":"7c9de099-7b4a-4477-8d69-16fdab9b8e18","html_url":"https://github.com/ibbd-dev/go-cond-strategy","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/ibbd-dev/go-cond-strategy","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-cond-strategy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-cond-strategy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-cond-strategy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-cond-strategy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ibbd-dev","download_url":"https://codeload.github.com/ibbd-dev/go-cond-strategy/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ibbd-dev%2Fgo-cond-strategy/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262969886,"owners_count":23392529,"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-11-09T23:08:37.843Z","updated_at":"2025-07-01T13:07:05.332Z","avatar_url":"https://github.com/ibbd-dev.png","language":"Go","readme":"# go-cond-strategy\n\nGolang条件策略框架及服务降级框架，根据相关指标，自动对相关的服务进行开启关闭，或者降级，例如原来按100%写日志，降级为50%的概率写日志。\n\n服务降级之后，当条件不再满足的时候，应该能自动恢复。\n\n## Install \n\n```sh\n# 条件策略框架\ngo get -u github.com/ibbd-dev/go-cond-strategy\n```\n\n## 主要概念\n\n- **事件**：例如将从接收到请求到返回数据，定义为一个访问事件，使用时需要先对事件的名字进行初始化。事件分为两类：\n  - **计数事件**：只需要统计发生次数的事件，例如错误的发生次数。对应CountEvent函数\n  - **耗时事件**：这类时间是有开始和结束之分，可以统计次数和耗时。对应BeginEvent和End函数\n- **指标**：指标都含有一个时间段的特征，我们实现的是1分钟和5分钟的指标，具体指标又分为：\n  - **次数Count**：事件在统计时间段内发生的次数\n  - **平均耗时AvgTime**：每次事件的平均消耗事件，耗时事件才有该指标\n- **策略**：当指标满足某些条件时，可以执行相应的动作，否则可以执行另外一组动作，就是策略。例如当某事件的平均耗时很高时，系统能自动进行降级，或者当某事件的发生次数达到某个阀值的时候，自动发送邮件等。\n\n策略Check函数可以返回一个级别（定义了5个可用等级, 从Level1到Level5），当级别发生改变的时候，就会执行Action动作。例如，我们可以将系统的压力分成5个级别，外部可以定义在什么级别开启什么服务或者关闭什么服务等。\n\n如果我们只使用其中的两个级别（也定义两个常量：StatusYes, StatusNo），就可以变成true or false的策略。\n\n## 实现思路\n\n指标：\n\n- 事件总次数：一段时间内的事件发生的总次数\n- 事件平均耗时指标：一段时间内完成事件耗时的均值\n\n时间段：\n\n- 1分钟\n- 5分钟\n\n前端可以设置相关指标满足什么条件时，触发什么操作。后台就会周期性计算是否满足触发条件，减少判断的耗时。\n\n## Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\n    \"github.com/ibbd-dev/go-cond-strategy\"\n)\n\nfunc main() {\n\t// 初始化事件\n\teventName := \"access\"\n\tcondStrategy.InitEvent(eventName)\n\n\n\t// 配置策略\n\tvar hello int\n\tstrategy := condStrategy.NewStrategy(func(m *condStrategy.TEventsMetric) condStrategy.TLevel {\n\t\tprintln(\"第1个策略...\")\n\t\tif m.Events[eventName].OneMinute.Count \u003e 300 {\n\t\t\treturn StatusYes\n\t\t}\n\t\treturn StatusNo\n\n\t}, func(status condStrategy.TLevel) {\n\t\tif status == condStrategy.StatusYes {\n\t\t\thello = 1  // Do somethings...\n\t\t} else {\n\t\t\thello = 2  // Do somethings else...\n\t\t}\n\t})\n\n\t// 模拟数据统计数据\n\tfor i := 0; i \u003c 400; i++ {\n\t\tev := BeginEvent(eventName)\n\t\tev.End()\n\t}\n\n\t// 等待指标的更新\n\ttime.Sleep(time.Minute)\n\ttime.Sleep(time.Second)\n\n\t// 更新指标\n\tif hello != 1 {\n\t\tprintln(\"error\")\n\t}\n}\n```\n\n## 性能数据\n\nBeginEvent和End的性能数据\n\n```\n// 同时记录1分钟数据和5分钟数据\nBenchmarkEvent-4    20000000            73.6 ns/op\n\n// 只记录1分钟数据，5分钟数据在后续统计时补上\nBenchmarkEvent-4    20000000            61.7 ns/op\n\n```\n\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibbd-dev%2Fgo-cond-strategy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fibbd-dev%2Fgo-cond-strategy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fibbd-dev%2Fgo-cond-strategy/lists"}