Skip to content

Commit

Permalink
feat(services/systat): 将 handlers/monitor 改为 services/systat
Browse files Browse the repository at this point in the history
不再与 SSE 绑定,而是变为一个服务,由用户自行订阅数据。
  • Loading branch information
caixw committed Dec 20, 2024
1 parent 82df42d commit f08b463
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 110 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@
[![PkgGoDev](https://pkg.go.dev/badge/github.com/issue9/webuse/v7)](https://pkg.go.dev/github.com/issue9/webuse/v7)
[![Go version](https://img.shields.io/github/go-mod/go-version/issue9/webuse)](https://golang.org)

适用于 [web](https://pkg.go.dev/github.com/issue9/web) 的中间件、插件和一些常用的路由函数
适用于 [web](https://pkg.go.dev/github.com/issue9/web) 的中间件、插件、服务以及常用的路由函数

## handlers

位于 [handlers](handlers) 之下:

- debug 调试信息的输出接口;
- monitor 系统状态监视;
- static 静态文件处理;

## 插件
Expand All @@ -39,6 +38,12 @@
- empty 提供了一个不作任何操作的中间件;
- skip 根据条件跳过路由的执行;

## 服务

服务位于 [services](services) 目录之下:

- systat 系统状态监视;

## 安装

```shell
Expand Down
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ require (
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/issue9/assert/v4 v4.3.1
github.com/issue9/cache v0.18.0
github.com/issue9/events v0.9.0
github.com/issue9/events v0.9.1
github.com/issue9/logs/v7 v7.6.4
github.com/issue9/mux/v9 v9.1.0
github.com/issue9/mux/v9 v9.1.2
github.com/issue9/rands/v3 v3.0.1
github.com/issue9/web v0.100.4
github.com/issue9/web v0.100.6
github.com/shirou/gopsutil/v4 v4.24.11
golang.org/x/text v0.21.0
gopkg.in/yaml.v3 v3.0.1
Expand All @@ -29,14 +29,15 @@ require (
github.com/issue9/query/v3 v3.1.3 // indirect
github.com/issue9/scheduled v0.21.3 // indirect
github.com/issue9/sliceutil v0.17.0 // indirect
github.com/issue9/source v0.11.6 // indirect
github.com/issue9/source v0.11.7 // indirect
github.com/issue9/term/v3 v3.3.2 // indirect
github.com/issue9/unique/v2 v2.1.0 // indirect
github.com/jellydator/ttlcache/v3 v3.3.0 // indirect
github.com/klauspost/compress v1.17.11 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/puzpuzpuz/xsync/v3 v3.4.0 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
Expand Down
18 changes: 10 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ github.com/issue9/conv v1.3.5 h1:UWeA+Zqp5vjNDLrmhLhaXPjS1hL8gh4quX6Shk5njKQ=
github.com/issue9/conv v1.3.5/go.mod h1:lkZYMyrmxy+HK//N4eLmwUl9mCwj0zgeTjnxgl/w7hM=
github.com/issue9/errwrap v0.3.2 h1:7KEme9Pfe75M+sIMcPCn/DV90wjnOcRbO4DXVAHj3Fw=
github.com/issue9/errwrap v0.3.2/go.mod h1:KcCLuUGiffjooLCUjL89r1cyO8/HT/VRcQrneO53N3A=
github.com/issue9/events v0.9.0 h1:P1uoLkEeZ1EcV9kTGzhJStglkJ1rNGzFYNhacWg4WS8=
github.com/issue9/events v0.9.0/go.mod h1:7rdiy2zeMKDsgejo0JxYsIXj5fVCVUxX/l/ABmOqfiE=
github.com/issue9/events v0.9.1 h1:AUkLd1aJPO8qWtc37zSJj/ovli2R7MW8+MnQUlMJRU8=
github.com/issue9/events v0.9.1/go.mod h1:r5FlweMrG+e6hWBm9UnbesURvzo02RZb4ju2cwIZFBs=
github.com/issue9/localeutil v0.29.1 h1:C5vxqwcAFa9gzMWDni8KsZ2GYtKD+I20RMlKl78tK/U=
github.com/issue9/localeutil v0.29.1/go.mod h1:daxDbw51s4GWKjz6pxhh8baHzWDJEUUcsVrqaKs/A8o=
github.com/issue9/logs/v7 v7.6.4 h1:NFM4oM2ggLPPZNgDb8PAOSqELtJOx7PTwoFd3o814wk=
github.com/issue9/logs/v7 v7.6.4/go.mod h1:6fIj8OABk/qWm1c3E9f9oXe0FWwRyoZNWh449D391Zk=
github.com/issue9/mux/v9 v9.1.0 h1:KFvCugh4eQf/2QiLVJOa2AzOEdGpG5xe7+JiqZCYluE=
github.com/issue9/mux/v9 v9.1.0/go.mod h1:lhKQh+boCGOP5yCEAQx7tb24nUsxT57ygU0gqD6/Vgg=
github.com/issue9/mux/v9 v9.1.2 h1:Ng5uv8bwsduZV6w14gv0HR78ahBs8Fm0JWlc7Xjtbd4=
github.com/issue9/mux/v9 v9.1.2/go.mod h1:CLtV5ZfIrAmfaC/qxn07wXc3GALI45GYCsdE3c/h9CQ=
github.com/issue9/query/v3 v3.1.3 h1:Y6ETEYXxaKqhpM4lXPKCffhJ72VuKQbrAwgwHlacu0Y=
github.com/issue9/query/v3 v3.1.3/go.mod h1:a/W/+7iel9K+5rRT4AFAKR8+OJeV5axeF6tK9My4lNA=
github.com/issue9/rands/v3 v3.0.1 h1:EnX9WNushGgHCzoL/R5eBPaLfvjLO/c7CGHNgLK0JhY=
Expand All @@ -43,14 +43,14 @@ github.com/issue9/scheduled v0.21.3 h1:22R1jl8rI+wrepHKZA1GfpZ6rpj5DLxVj69f0Vzrw
github.com/issue9/scheduled v0.21.3/go.mod h1:o9Z7g50Ld1pOozYQ4RqRLKDrLcQP/dOhSd84A0c6d2w=
github.com/issue9/sliceutil v0.17.0 h1:EtmVlldkAyGxS0O2TnxcAu3pgLJw74I5eh9DHT7TOZs=
github.com/issue9/sliceutil v0.17.0/go.mod h1:CVpH4f228pICY7JImlztUPe/zf2w0EicEleU9YfFe00=
github.com/issue9/source v0.11.6 h1:ABrURywr1zsoCneDEusLWO+/OCkXfcoS5x8Z2by/clg=
github.com/issue9/source v0.11.6/go.mod h1:78T7Mm8FNHsZbSkET/w02A7a3L9w3OzwR+Tx/BILEA4=
github.com/issue9/source v0.11.7 h1:wyZv2MExD1kem7FGxIy6/iSgDHqNpLox6dAZfP7VzKM=
github.com/issue9/source v0.11.7/go.mod h1:cmLmHZdgj+ONnGNdnJp++94VKXTszNAD99hAYOC22bU=
github.com/issue9/term/v3 v3.3.2 h1:heyRhfA6EMztqh5UHUZyKr/LatFZXbJ7YhE5RI5fPTs=
github.com/issue9/term/v3 v3.3.2/go.mod h1:n5XiDESqvPeaLgHeG7H0JdIXU8/H5IZPEaH++0aHE5Q=
github.com/issue9/unique/v2 v2.1.0 h1:lE9hstenrbBgDM/Iulv7d75qAYpgoDV8rSRoLmEBHOk=
github.com/issue9/unique/v2 v2.1.0/go.mod h1:qZoDKnfu+7Q0yxhifVseRKD2Wea9Tc9zdXwALnFc54A=
github.com/issue9/web v0.100.4 h1:JXZBwcot5cz0P/Ey31ZZJsXXbsMfw1rl8Ilg7+llEeQ=
github.com/issue9/web v0.100.4/go.mod h1:TkimI9/soQkdihGKMh4eyChYLwrQ5IgzXV8CzLUJ04A=
github.com/issue9/web v0.100.6 h1:UwJNhTDR9iATiZV34F5OlLjcBnoJuI1rK4rjTP5CKCE=
github.com/issue9/web v0.100.6/go.mod h1:OKBbmxlkd45CKMGtTb+hOlS46DgSR/decsBOrbRIXdI=
github.com/jellydator/ttlcache/v3 v3.3.0 h1:BdoC9cE81qXfrxeb9eoJi9dWrdhSuwXMAnHTbnBm4Wc=
github.com/jellydator/ttlcache/v3 v3.3.0/go.mod h1:bj2/e0l4jRnQdrnSTaGTsh4GSXvMjQcy41i7th0GVGw=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
Expand All @@ -68,6 +68,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c h1:ncq/mPwQF4JjgDlrVEn3C11VoGHZN7m8qihwgMEtzYw=
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/puzpuzpuz/xsync/v3 v3.4.0 h1:DuVBAdXuGFHv8adVXjWWZ63pJq+NRXOWVXlKDBZ+mJ4=
github.com/puzpuzpuz/xsync/v3 v3.4.0/go.mod h1:VjzYrABPabuM4KyBh1Ftq6u8nhwY5tBPKP9jpmh0nnA=
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
github.com/shirou/gopsutil/v4 v4.24.11 h1:WaU9xqGFKvFfsUv94SXcUPD7rCkU0vr/asVdQOBZNj8=
Expand Down
75 changes: 0 additions & 75 deletions handlers/monitor/monitor.go

This file was deleted.

6 changes: 6 additions & 0 deletions services/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-FileCopyrightText: 2024 caixw
//
// SPDX-License-Identifier: MIT

// Package services 提供一些常用的服务
package services
52 changes: 52 additions & 0 deletions services/systat/systat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-FileCopyrightText: 2024 caixw
//
// SPDX-License-Identifier: MIT

// Package systat 系统状态检测
package systat

import (
"context"
"time"

"github.com/issue9/events"
"github.com/issue9/web"
)

// 系统状态监视服务
type service struct {
events *events.Event[*Stats]
}

// Init 初始化监视系统状态的服务
//
// 这将返回一个用于订阅状态变化的接口,用户可根据该接口订阅信息。
//
// dur 为监视数据的频率;
// interval 为每次监视数据的时间;
func Init(s web.Server, dur, interval time.Duration) events.Subscriber[*Stats] {
srv := &service{
events: events.New[*Stats](),
}

job := func(now time.Time) error {
if srv.events.Len() == 0 { // 没有订阅,就没必要计算状态了。
return nil
}

stat, err := calcState(interval, now)
if err == nil {
srv.events.Publish(true, stat)
}
return err
}

s.Services().AddTicker(web.Phrase("monitor system stat"), job, dur, true, true)

return srv
}

// Subscribe 订阅状态变化的通知
func (s *service) Subscribe(f events.SubscribeFunc[*Stats]) context.CancelFunc {
return s.events.Subscribe(f)
}
37 changes: 20 additions & 17 deletions handlers/monitor/monitor_test.go → services/systat/systat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@
//
// SPDX-License-Identifier: MIT

package monitor
package systat

import (
"context"
"bytes"
"net/http"
"os"
"testing"
"time"

"github.com/issue9/assert/v4"
"github.com/issue9/events"
"github.com/issue9/logs/v7"
"github.com/issue9/web"
"github.com/issue9/web/mimetype/nop"
Expand All @@ -20,9 +21,9 @@ import (
"github.com/issue9/web/server/servertest"
)

var _ web.HandlerFunc = (&Monitor{}).Handle
var _ events.Subscriber[*Stats] = &service{}

func TestMonitor(t *testing.T) {
func TestService(t *testing.T) {
a := assert.New(t, false)
s, err := server.NewHTTP("test", "1.0.0", &server.Options{
HTTPServer: &http.Server{Addr: ":8080"},
Expand All @@ -34,17 +35,19 @@ func TestMonitor(t *testing.T) {
defer servertest.Run(a, s)()
defer s.Close(0)

r := s.Routers().New("def", nil)
m := New(s, time.Second)
r.Get("/stats", m.Handle)

stats := make(chan *sse.Message, 10)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
err = sse.OnMessage(ctx, s.Logs().ERROR(), "http://localhost:8080/stats", nil, stats)
a.When(err == nil, func(a *assert.Assertion) {
s := <-stats
a.Contains(s.Data[0], `"cpu":`).
Contains(s.Data[0], `"mem":`)
}, "返回了错误信息 %v", err)
sub := Init(s, time.Second, time.Second)
o1 := &bytes.Buffer{}
o2 := &bytes.Buffer{}

sub.Subscribe(func(data *Stats) {
o1.Write([]byte(data.Created.String()))
})
sub.Subscribe(func(data *Stats) {
o2.Write([]byte(data.Created.String()))
})

time.Sleep(2 * time.Second)

a.NotZero(o1.Len()).
NotZero(o2.Len())
}
9 changes: 5 additions & 4 deletions handlers/monitor/types.go → services/systat/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: MIT

package monitor
package systat

import (
"os"
Expand All @@ -17,7 +17,8 @@ import (

// Stats 监视的状态信息
type Stats struct {
XMLName struct{} `json:"-" yaml:"-" xml:"stats" cbor:"-"`
XMLName struct{} `json:"-" yaml:"-" xml:"stats" cbor:"-"`

OS *OS `json:"os" yaml:"os" xml:"os" cbor:"os" comment:"os stats"` // 系统级别的状态信息
Process *Process `json:"process" yaml:"process" xml:"process" cbor:"process" comment:"process stats"` // 当前进程的状态信息
Created time.Time `json:"created" yaml:"created" xml:"created" cbor:"created" comment:"created time"` // 此条记录的创建时间
Expand Down Expand Up @@ -46,7 +47,7 @@ type Net struct {
}

func calcState(interval time.Duration, now time.Time) (*Stats, error) {
all, err := calcOS(interval)
os, err := calcOS(interval)
if err != nil {
return nil, err
}
Expand All @@ -56,7 +57,7 @@ func calcState(interval time.Duration, now time.Time) (*Stats, error) {
return nil, err
}

return &Stats{OS: all, Process: p, Created: now}, nil
return &Stats{OS: os, Process: p, Created: now}, nil
}

func calcProcess() (*Process, error) {
Expand Down

0 comments on commit f08b463

Please sign in to comment.