-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
149 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
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
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,6 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package middlewares 适用于 [web.Middleware] 的中间件 | ||
package middlewares |
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,40 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package compress 根据 CPU 占用情况决定是否启用压缩 | ||
package compress | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/issue9/web" | ||
"github.com/shirou/gopsutil/v3/cpu" | ||
) | ||
|
||
// New 根据 CPU 使用率决定是否启用压缩功能 | ||
// | ||
// dur 多少时间检测一次 CPU 的使用率,不能小于 [time.Second]; | ||
// interval 每次检测时读取的时间长度,不能大于 dur; | ||
// percent CPU 的使用率大于此值时将禁用压缩功能; | ||
func New(dur, interval time.Duration, percent float64) web.Plugin { | ||
if dur < interval { | ||
panic("dur 必须大于 interval") | ||
} | ||
|
||
if percent < 0 || percent > 100 { | ||
panic("percent 必须介于 [0,100]") | ||
} | ||
|
||
return web.PluginFunc(func(s web.Server) { | ||
s.Services().AddTicker(web.Phrase("enable compression base on cpu used"), func(now time.Time) error { | ||
if vals, err := cpu.Percent(interval, false); err != nil { | ||
s.Logs().ERROR().Error(err) | ||
} else { | ||
s.SetCompress(vals[0] < percent) | ||
} | ||
|
||
return nil | ||
}, dur, true, false) | ||
}) | ||
} |
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,37 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
package compress | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/issue9/assert/v4" | ||
"github.com/issue9/web/server" | ||
) | ||
|
||
func TestNew(t *testing.T) { | ||
a := assert.New(t, false) | ||
|
||
a.PanicString(func() { | ||
New(time.Microsecond, time.Millisecond, 80) | ||
}, "dur 必须大于 interval") | ||
|
||
a.PanicString(func() { | ||
New(time.Millisecond, time.Microsecond, -1) | ||
}, "percent 必须介于 [0,100]") | ||
|
||
a.PanicString(func() { | ||
New(time.Millisecond, time.Microsecond, 101) | ||
}, "percent 必须介于 [0,100]") | ||
|
||
s, err := server.New("test", "1.0.0", nil) | ||
a.NotError(err).NotNil(s) | ||
s.SetCompress(false) | ||
|
||
a.False(s.CanCompress()) | ||
s.Use(New(time.Second, time.Microsecond, 80)) | ||
a.Wait(time.Second).True(s.CanCompress()) | ||
} |
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,6 @@ | ||
// SPDX-FileCopyrightText: 2024 caixw | ||
// | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package plugins 适用于 [web.Plugin] 的插件 | ||
package plugins |
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