Skip to content

Commit

Permalink
fix: improve memory pressure from getlogs
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Nov 14, 2024
1 parent b7abbf9 commit 592e4cf
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
54 changes: 33 additions & 21 deletions internal/api/backresthandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -504,54 +504,66 @@ func (s *BackrestHandler) GetLogs(ctx context.Context, req *connect.Request[v1.L
r.Close()
}()

var bufferMu sync.Mutex
var buffer bytes.Buffer
var errChan = make(chan error, 1)
var sendChan = make(chan []byte, 1)
var buffer bytes.Buffer
var bufferMu sync.Mutex

go func() {
data := make([]byte, 4*1024)
for {
n, err := r.Read(data)
if n == 0 {
close(errChan)
break
} else if err != nil && err != io.EOF {
errChan <- fmt.Errorf("failed to read log data: %w", err)
close(errChan)
return
break
}
bufferMu.Lock()
buffer.Write(data[:n])
if buffer.Len() > 128*1024 {
sendChan <- bytes.Clone(buffer.Bytes())
buffer.Reset()
}
bufferMu.Unlock()
}
}()

ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()

flush := func() error {
bufferMu.Lock()
if buffer.Len() > 0 {
if err := resp.Send(&types.BytesValue{Value: buffer.Bytes()}); err != nil {
bufferMu.Unlock()
return fmt.Errorf("failed to send log data: %w", err)
}
bufferMu.Lock()
sendChan <- bytes.Clone(buffer.Bytes())
buffer.Reset()
bufferMu.Unlock()
}
bufferMu.Unlock()
return nil
}
close(sendChan)
}()

ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()

for {
select {
case <-ctx.Done():
return flush()
return nil
case data, ok := <-sendChan:
if !ok {
return nil
}
if err := resp.Send(&types.BytesValue{Value: data}); err != nil {
return err
}
case err := <-errChan:
_ = flush()
return err
case <-ticker.C:
if err := flush(); err != nil {
return err
bufferMu.Lock()
if buffer.Len() > 0 {
if err := resp.Send(&types.BytesValue{Value: bytes.Clone(buffer.Bytes())}); err != nil {
bufferMu.Unlock()
return err
}
buffer.Reset()
}
bufferMu.Unlock()
}
}

Expand Down
9 changes: 1 addition & 8 deletions webui/src/views/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ import { shouldShowSettings } from "../state/configutil";
import { OpSelector } from "../../gen/ts/v1/service_pb";
import { colorForStatus } from "../state/flowdisplayaggregator";
import { getStatusForSelector } from "../state/logstate";
import {
createHashRouter,
Route,
RouterProvider,
Routes,
useNavigate,
useParams,
} from "react-router-dom";
import { Route, Routes, useNavigate, useParams } from "react-router-dom";
import { MainContentAreaTemplate } from "./MainContentArea";

const { Header, Sider } = Layout;
Expand Down
4 changes: 2 additions & 2 deletions webui/src/views/SummaryDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ const SummaryPanel = ({
</Typography.Text>
)}
{summary.backupsFailed30days && (
<Typography.Text type="danger">
<Typography.Text type="danger" style={{ marginRight: "5px" }}>
{summary.backupsFailed30days + ""} failed
</Typography.Text>
)}
{summary.backupsWarningLast30days && (
<Typography.Text type="warning">
<Typography.Text type="warning" style={{ marginRight: "5px" }}>
{summary.backupsWarningLast30days + ""} warning
</Typography.Text>
)}
Expand Down

0 comments on commit 592e4cf

Please sign in to comment.