-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bow realize current limiting function
- Loading branch information
abericyang@gmail.com
committed
May 14, 2019
1 parent
6177c40
commit 10d96ce
Showing
6 changed files
with
183 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (c) 2019. ENNOO - All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package bow | ||
|
||
import ( | ||
"github.com/ennoo/rivet/utils/log" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
// Limit 限流对象 | ||
type Limit struct { | ||
LimitMillisecond int64 // 请求限定的时间段(毫秒) | ||
LimitCount int // 请求限定的时间段内允许的请求次数 | ||
LimitIntervalMillisecond int64 // 请求允许的最小间隔时间(毫秒),0表示不限 | ||
LimitChan chan int // 限流通道 | ||
Times []int64 // 请求时间数组 | ||
} | ||
|
||
func (l *Limit) timeInit() { | ||
l.Times = make([]int64, 0) | ||
for i := 0; i < l.LimitCount; i++ { | ||
time.Sleep(10 * time.Millisecond) | ||
l.add(time.Now().UnixNano() / 1e6) | ||
} | ||
} | ||
|
||
func (l *Limit) limit() { | ||
for { | ||
timeNow := time.Now().UnixNano() / 1e6 | ||
if len(l.Times) < l.LimitCount { | ||
l.add(time.Now().UnixNano() / 1e6) | ||
} else if timeNow-l.Times[0] > l.LimitMillisecond && timeNow-l.Times[len(l.Times)-1] > l.LimitIntervalMillisecond { | ||
limitChanResult := strconv.Itoa(<-l.LimitChan) | ||
log.Bow.Debug("取出一个元素,放行 <-c = " + limitChanResult) | ||
if len(l.Times) == l.LimitCount { | ||
l.remove() | ||
} | ||
l.add(time.Now().UnixNano() / 1e6) | ||
} else { | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
} | ||
} | ||
|
||
// add 新增一个元素 | ||
func (l *Limit) add(time int64) { | ||
if len(l.Times) < l.LimitCount { | ||
l.Times = append(l.Times, time) | ||
} | ||
} | ||
|
||
// remove 移除第一个元素 | ||
func (l *Limit) remove() { | ||
l.Times = l.Times[1:] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2019. ENNOO - All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package bow | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestLimit(t *testing.T) { | ||
limit := Limit{ | ||
LimitMillisecond: int64(1 * 1000), | ||
LimitCount: 3, | ||
LimitIntervalMillisecond: 150, | ||
LimitChan: make(chan int, 10), | ||
} | ||
fmt.Printf("时间戳(秒):%v;\n", time.Now().Unix()) | ||
fmt.Printf("时间戳(纳秒):%v;\n", time.Now().UnixNano()) | ||
fmt.Printf("时间戳(毫秒):%v;\n", time.Now().UnixNano()/1e6) | ||
fmt.Printf("时间戳(纳秒转换为秒):%v;\n", time.Now().UnixNano()/1e9) | ||
limit.timeInit() | ||
fmt.Printf("%v\n", limit.Times) | ||
time.Sleep(1 * time.Second) | ||
limit.remove() | ||
fmt.Printf("%v\n", limit.Times) | ||
time.Sleep(1 * time.Second) | ||
limit.add(time.Now().UnixNano() / 1e6) | ||
fmt.Printf("%v\n", limit.Times) | ||
go limit.limit() | ||
loop(&limit) | ||
} | ||
|
||
func loop(limit *Limit) { | ||
i := 0 | ||
for i <= 20 { | ||
fmt.Println("被堵住了 c len = ", len(limit.LimitChan)) | ||
limit.LimitChan <- i | ||
fmt.Printf("被放行了 %v\n", limit.Times) | ||
i++ | ||
time.Sleep(100 * time.Millisecond) | ||
} | ||
} | ||
|
||
func TestLimitMap(t *testing.T) { | ||
serviceName := "test" | ||
routeService := RouteService{ | ||
Name: serviceName, | ||
InURI: "hello1", | ||
OutRemote: "http://localhost:8081", | ||
OutURI: "rivet/shunt", | ||
Limit: &Limit{ | ||
LimitMillisecond: int64(1 * 1000), | ||
LimitCount: 3, | ||
LimitIntervalMillisecond: 150, | ||
LimitChan: make(chan int, 10), | ||
}, | ||
} | ||
go routeService.Limit.limit() | ||
loopMap(routeService.Limit) | ||
} | ||
|
||
func loopMap(limit *Limit) { | ||
if nil != limit { | ||
loop(limit) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters