0%

什麼是Abstract Factory(抽象工廠)呢?

定義: 又稱為Kit模式。提供一個創建系列相關或者互相依賴的Interface,而無需指定其具體的class

簡單例子

注: 以下程式單純用於解釋,並不能實際執行

基於Gin實作Rate Limiter

假設我們有2個APIs,而每個API都需要消耗1個Tokens

urimethoddesc
/api/posts/{id}GETreturn a simple demo message
/pingGETreturn pong

我們先設置一下rate limiter桶子的容量只能放下一個tokens,而tokens則會每秒生產5個。存取API時,若沒有Token可以用,就需要等待token下一次生產並放到桶子裡,才能繼續進行下去。

Token Bucket(令牌桶算法)

什麼是Token Bucket 呢?

簡單來說就是運用Token Bucket的系統會以一個設定的速率往桶子(Bucket)裡面丟令牌(Token)。如果請求(Request)需要被處理時,就必需得Bucket裡的Token。當桶子裡面的沒有Token可以分配/獲取時,也就是說Bucket現在是空的(Token已經被其他令牌拿完了),系統則會拒絕這個請求的服務。

什麼是Factory(工廠)呢?

定義:建立一個接口,讓子類自己決定實現哪一個Factory,其重點是繼承了Simple Factory Patterns的優點,同時解決了它的問題

什麼是Simple Factory(簡單工廠)呢?

簡單而言就是: 由一個工廠來生產全部產品 定義:建立一個接口,讓子類自己決定實現哪一個Factory,重點在於工廠,透過工廠的Static method 進行生成的Object

什麼是Design Pattern(設計模式)呢?

設計模式(Design Pattern) 是對軟體設計中普遍存在(反覆出現)的各種問題,所提出的解決方案。用於描述在各種不同的情況下,如何解決問題的一種方案

What is JWT(Json Web Token)?

The full name of JWT is Json Web Token. According to the definition, JWT is a proposed Internet standard for creating data with optional signature and/or opntional encryption whose payload holds JSON that asserts some number of clamis. It’s used for identity authentication between client and the server that allows accessing resources in the server.

The Structure of JWT

JWT consists of three parts including: