Skip to content

Commit

Permalink
Merge pull request #491 from rookisbusy/Alpha
Browse files Browse the repository at this point in the history
feat: core support memory chat
  • Loading branch information
rookisbusy authored Apr 7, 2023
2 parents eecd8ff + 76340cc commit c4c7c56
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 1 deletion.
48 changes: 48 additions & 0 deletions hub/route/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ type Traffic struct {
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) {
uiPath = C.Path.Resolve(path)
}
Expand Down Expand Up @@ -76,6 +81,7 @@ func Start(addr string, tlsAddr string, secret string,
r.Get("/", hello)
r.Get("/logs", getLogs)
r.Get("/traffic", traffic)
r.Get("/memory", memory)
r.Get("/version", version)
r.Mount("/configs", configRouter())
r.Mount("/proxies", proxyRouter())
Expand Down Expand Up @@ -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 string `json:"type"`
Payload string `json:"payload"`
Expand Down
8 changes: 7 additions & 1 deletion tunnel/statistic/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type Manager struct {
uploadTotal *atomic.Int64
downloadTotal *atomic.Int64
pid int
memory uint64
}

func (m *Manager) Join(c tracker) {
Expand All @@ -58,6 +59,10 @@ func (m *Manager) Now() (up int64, down int64) {
return m.uploadBlip.Load(), m.downloadBlip.Load()
}

func (m *Manager) Memory() uint64 {
return m.memory
}

func (m *Manager) Snapshot() *Snapshot {
connections := []tracker{}
m.connections.Range(func(key, value any) bool {
Expand All @@ -76,12 +81,13 @@ func (m *Manager) Snapshot() *Snapshot {
}
return stat.RSS
}
m.memory = getMem()

return &Snapshot{
UploadTotal: m.uploadTotal.Load(),
DownloadTotal: m.downloadTotal.Load(),
Connections: connections,
Memory: getMem(),
Memory: m.memory,
}
}

Expand Down

0 comments on commit c4c7c56

Please sign in to comment.