Skip to content

Commit

Permalink
add MaxInboundFramePerSecond for qfe
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiqiangxu committed Mar 26, 2020
1 parent 85f7f5b commit 8c72dd7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
3 changes: 2 additions & 1 deletion conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type ServerBinding struct {
ReadFrameChSize int
WriteFrameChSize int
MaxFrameSize int
MaxCloseRate int // per second
MaxCloseRate int // per second
MaxInboundFramePerSecond int
MaxInboundInflightStreamsPerConn int32 // connection will be closed when exceeded
ListenFunc func(network, address string) (net.Listener, error)
Codec CompressorCodec
Expand Down
19 changes: 19 additions & 0 deletions serveconn.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ func (sc *serveconn) GetWriter() FrameWriter {
var (
// ErrInvalidPacket when packet invalid
ErrInvalidPacket = errors.New("invalid packet")
// ErrInboundFramePerSecondExceeded when max inbound frame per second exceeded
ErrInboundFramePerSecondExceeded = errors.New("inbound frame per second exceeded")
)

type readFrameResult struct {
Expand Down Expand Up @@ -350,8 +352,25 @@ func (sc *serveconn) readFrames() (err error) {
gate := make(gate, 1)
gateDone := gate.Done

checkInBoundQPS := binding.MaxInboundFramePerSecond > 0
var (
lastCheckTS int64
tsCount int
)
for {
req, err := sc.reader.ReadFrame(sc.cs)
if checkInBoundQPS {
nowTS := time.Now().Unix()
if nowTS != lastCheckTS {
tsCount = 1
lastCheckTS = nowTS
} else {
tsCount++
}
if tsCount > binding.MaxInboundFramePerSecond {
err = ErrInboundFramePerSecondExceeded
}
}
if err != nil {
sc.Close()
sc.reader.Finalize()
Expand Down

0 comments on commit 8c72dd7

Please sign in to comment.