GithubHelp home page GithubHelp logo

davidleitw / sliding Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 1.0 18 KB

一個基於 sliding window 概念的輕量級 go library.

License: MIT License

Go 100.00%
sliding-window sliding-windows golang rate-limiter qps library time

sliding's Introduction

Sliding

簡介

Sliding 是一個基於 sliding window algorithm 的輕量級 library。開發它的靈感來自於一些微服務限流專案(Ex: guava,Sentinal) 中關於統計 QPS 的作法,透過將一些簡單的操作封裝,可以有效的省去開發的成本。

對於一些需要統計單位時間內發生事件的場景,都可以利用 Sliding 去簡單的統計資料,Sliding 可以自訂 Upload function,用來處理 window 滑動後,原本位於 window 的資料要怎麼處理,寫入資料庫,或者根據設定的 threshold 去觸發某些行為等等。

Installation

go get github.com/davidleitw/Sliding/pkg/slidingwindow

include

import "github.com/davidleitw/Sliding/pkg/slidingwindow"

概念

在建立 Sliding window 的時候可以指定 windowSizewindowLength

  • windowSize: 單一窗口時間,用 ms 為單位
  • windowLength: 總共有幾個 window

創立一個 windowSize=125ms, windowLength=8Sliding Window 可以參考以下範例,這樣一輪總共有 1000ms = 1s

slw := slidingwindow.NewSlidingWindows(125, 8, nil)

可以簡單用上圖的例子來說明 Sliding window 的運作原理

假設今天要紀錄的資料發生在487ms,我們可以透過 (487 / windowSize) % wnidowLength 來求出 index=3,我們也可以透過 487 - (487 % windowLength) 來算出這輪的 start = 375

Sliding window 創立之後怎麼滑動呢? 可以用下面的圖片說明,一開始的步驟都像上面,但是不一樣的地方在於算出來的 start=48763 會跟 windows[index].start 先比大小,如果發現新出來的 start 大於原本 windowstart,就會更新相關的資料,在 Sliding 內會分成兩個部份執行

  • 更新 windowstart
  • 執行使用者設定的 upload function,處理 window 更新前存在內部的統計資料,會在後續的範例中看到詳細的用法

範例

動工中

參考文章

動工中

TODO

  • 思考兩個使用範例
    • 整合 InfluxDB, Grafana 呈現
  • 思考 window 的更新問題,尤其是超過兩輪沒有更新時如何補上空缺的資料

sliding's People

Contributors

davidleitw avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

russ168

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.