Merge pull request #491 from rookisbusy/Alpha
feat: core support memory chat
This commit is contained in:
commit
e98c5302ff
2 changed files with 55 additions and 1 deletions
|
@ -39,6 +39,11 @@ type Traffic struct {
|
||||||
Down int64 `json:"down"`
|
Down int64 `json:"down"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Memory struct {
|
||||||
|
Inuse uint64 `json:"inuse"`
|
||||||
|
OSLimit uint64 `json:"oslimit"` // maybe we need it in the future
|
||||||
|
}
|
||||||
|
|
||||||
func SetUIPath(path string) {
|
func SetUIPath(path string) {
|
||||||
uiPath = C.Path.Resolve(path)
|
uiPath = C.Path.Resolve(path)
|
||||||
}
|
}
|
||||||
|
@ -76,6 +81,7 @@ func Start(addr string, tlsAddr string, secret string,
|
||||||
r.Get("/", hello)
|
r.Get("/", hello)
|
||||||
r.Get("/logs", getLogs)
|
r.Get("/logs", getLogs)
|
||||||
r.Get("/traffic", traffic)
|
r.Get("/traffic", traffic)
|
||||||
|
r.Get("/memory", memory)
|
||||||
r.Get("/version", version)
|
r.Get("/version", version)
|
||||||
r.Mount("/configs", configRouter())
|
r.Mount("/configs", configRouter())
|
||||||
r.Mount("/proxies", proxyRouter())
|
r.Mount("/proxies", proxyRouter())
|
||||||
|
@ -224,6 +230,48 @@ func traffic(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func memory(w http.ResponseWriter, r *http.Request) {
|
||||||
|
var wsConn *websocket.Conn
|
||||||
|
if websocket.IsWebSocketUpgrade(r) {
|
||||||
|
var err error
|
||||||
|
wsConn, err = upgrader.Upgrade(w, r, nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if wsConn == nil {
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
render.Status(r, http.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
tick := time.NewTicker(time.Second)
|
||||||
|
defer tick.Stop()
|
||||||
|
t := statistic.DefaultManager
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
var err error
|
||||||
|
for range tick.C {
|
||||||
|
buf.Reset()
|
||||||
|
|
||||||
|
if err := json.NewEncoder(buf).Encode(Memory{
|
||||||
|
Inuse: t.Memory(),
|
||||||
|
OSLimit: 0,
|
||||||
|
}); err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
if wsConn == nil {
|
||||||
|
_, err = w.Write(buf.Bytes())
|
||||||
|
w.(http.Flusher).Flush()
|
||||||
|
} else {
|
||||||
|
err = wsConn.WriteMessage(websocket.TextMessage, buf.Bytes())
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type Log struct {
|
type Log struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
Payload string `json:"payload"`
|
Payload string `json:"payload"`
|
||||||
|
|
|
@ -34,6 +34,7 @@ type Manager struct {
|
||||||
uploadTotal *atomic.Int64
|
uploadTotal *atomic.Int64
|
||||||
downloadTotal *atomic.Int64
|
downloadTotal *atomic.Int64
|
||||||
pid int
|
pid int
|
||||||
|
memory uint64
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Manager) Join(c tracker) {
|
func (m *Manager) Join(c tracker) {
|
||||||
|
@ -58,6 +59,10 @@ func (m *Manager) Now() (up int64, down int64) {
|
||||||
return m.uploadBlip.Load(), m.downloadBlip.Load()
|
return m.uploadBlip.Load(), m.downloadBlip.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (m *Manager) Memory() uint64 {
|
||||||
|
return m.memory
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Manager) Snapshot() *Snapshot {
|
func (m *Manager) Snapshot() *Snapshot {
|
||||||
connections := []tracker{}
|
connections := []tracker{}
|
||||||
m.connections.Range(func(key, value any) bool {
|
m.connections.Range(func(key, value any) bool {
|
||||||
|
@ -76,12 +81,13 @@ func (m *Manager) Snapshot() *Snapshot {
|
||||||
}
|
}
|
||||||
return stat.RSS
|
return stat.RSS
|
||||||
}
|
}
|
||||||
|
m.memory = getMem()
|
||||||
|
|
||||||
return &Snapshot{
|
return &Snapshot{
|
||||||
UploadTotal: m.uploadTotal.Load(),
|
UploadTotal: m.uploadTotal.Load(),
|
||||||
DownloadTotal: m.downloadTotal.Load(),
|
DownloadTotal: m.downloadTotal.Load(),
|
||||||
Connections: connections,
|
Connections: connections,
|
||||||
Memory: getMem(),
|
Memory: m.memory,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue