{"id":24991897,"url":"https://github.com/davidleitw/sliding","last_synced_at":"2025-07-19T12:33:18.595Z","repository":{"id":103943276,"uuid":"437113977","full_name":"davidleitw/Sliding","owner":"davidleitw","description":"一個基於 sliding window 概念的輕量級 go library.","archived":false,"fork":false,"pushed_at":"2021-12-22T19:50:31.000Z","size":18,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-29T13:15:45.701Z","etag":null,"topics":["golang","library","qps","rate-limiter","sliding-window","sliding-windows","time"],"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/davidleitw.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,"zenodo":null}},"created_at":"2021-12-10T21:05:52.000Z","updated_at":"2024-08-17T13:50:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"d55b735a-60fd-4123-a64a-62d20ab1db34","html_url":"https://github.com/davidleitw/Sliding","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/davidleitw%2FSliding","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidleitw%2FSliding/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidleitw%2FSliding/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidleitw%2FSliding/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/davidleitw","download_url":"https://codeload.github.com/davidleitw/Sliding/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/davidleitw%2FSliding/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259219640,"owners_count":22823571,"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","library","qps","rate-limiter","sliding-window","sliding-windows","time"],"created_at":"2025-02-04T13:52:55.280Z","updated_at":"2025-06-11T07:09:23.142Z","avatar_url":"https://github.com/davidleitw.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# \u003cimg src=\"https://img.icons8.com/color/96/000000/chessboard.png\"/\u003e Sliding\n\n## \u003cimg src=\"https://img.icons8.com/color/48/000000/queen.png\"/\u003e 簡介\n\n`Sliding` 是一個基於 `sliding window algorithm` 的輕量級 `library`。開發它的靈感來自於一些微服務限流專案(Ex: [guava](https://github.com/google/guava),[Sentinal](https://github.com/alibaba/Sentinel)) 中關於統計 `QPS` 的作法，透過將一些簡單的操作封裝，可以有效的省去開發的成本。\n\n對於一些需要統計單位時間內發生事件的場景，都可以利用 `Sliding` 去簡單的統計資料，`Sliding` 可以自訂 `Upload function`，用來處理 `window` 滑動後，原本位於 `window` 的資料要怎麼處理，寫入資料庫，或者根據設定的 `threshold` 去觸發某些行為等等。\n\n#### Installation\n\n```go\ngo get github.com/davidleitw/Sliding/pkg/slidingwindow\n```\n\n#### include\n\n```go\nimport \"github.com/davidleitw/Sliding/pkg/slidingwindow\"\n```\n## \u003cimg src=\"https://img.icons8.com/color/48/000000/king.png\"/\u003e 概念\n\n\n在建立 `Sliding window` 的時候可以指定 `windowSize` 與 `windowLength`。\n\n- `windowSize`: 單一窗口時間，用 `ms` 為單位\n- `windowLength`: 總共有幾個 `window`\n\n創立一個 `windowSize=125ms`, `windowLength=8` 的 `Sliding Window` 可以參考以下範例，這樣一輪總共有 `1000ms = 1s`。\n\n```go\nslw := slidingwindow.NewSlidingWindows(125, 8, nil)\n```\n\n![](https://i.imgur.com/7GQW2su.png)\n\n可以簡單用上圖的例子來說明 `Sliding window` 的運作原理\n\n假設今天要紀錄的資料發生在`487ms`，我們可以透過 `(487 / windowSize) % wnidowLength` 來求出 `index=3`，我們也可以透過 `487 - (487 % windowLength)` 來算出這輪的 `start = 375`\n\n那 `Sliding window` 創立之後怎麼滑動呢? 可以用下面的圖片說明，一開始的步驟都像上面，但是不一樣的地方在於算出來的 `start=48763` 會跟 `windows[index].start` 先比大小，如果發現新出來的 `start` 大於原本 `window` 的 `start`，就會更新相關的資料，在 `Sliding` 內會分成兩個部份執行\n\n- 更新 `window` 的 `start`\n- 執行使用者設定的 `upload function`，處理 `window` 更新前存在內部的統計資料，會在後續的範例中看到詳細的用法\n\n![](https://i.imgur.com/EQSFTXK.png)\n\n## \u003cimg src=\"https://img.icons8.com/color/48/000000/knight.png\"/\u003e 範例\n\n動工中\n\n## \u003cimg src=\"https://img.icons8.com/color/48/000000/pawn.png\"/\u003e 參考文章\n\n動工中\n\n## TODO\n\n- 思考兩個使用範例\n    - 整合 InfluxDB, Grafana 呈現\n- 思考 window 的更新問題，尤其是超過兩輪沒有更新時如何補上空缺的資料","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidleitw%2Fsliding","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdavidleitw%2Fsliding","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdavidleitw%2Fsliding/lists"}