Skip to content

Commit

Permalink
Revert "Уменьшил потребеление памяти"
Browse files Browse the repository at this point in the history
This reverts commit 53609e3.
  • Loading branch information
Zagir2000 committed Sep 25, 2023
1 parent 53609e3 commit 139a657
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 25 deletions.
8 changes: 4 additions & 4 deletions cmd/agent/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ func main() {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
// создаем буферизованный канал для отправки результатов
jobs := make(chan []byte, 20)
go Metric.NewСollectMetricGopsutil(ctx, cancel, jobs)
go Metric.NewСollect(ctx, cancel, jobs)
jobs := make(chan []byte, 2)

go Metric.NewСollect(ctx, cancel, jobs)
go Metric.NewСollectMetricGopsutil(ctx, cancel, jobs)
for {
select {
case <-ctx.Done():
Expand All @@ -37,7 +37,7 @@ func main() {
g.Go(func() error {
var mx sync.Mutex
mx.Lock()
err := metricscollect.SendMetricsGor(jobs, flag.runAddr, flag.secretKey)
err := Metric.SendMetricsGor(jobs, flag.runAddr, flag.secretKey)
if err != nil {
return err
}
Expand Down
1 change: 0 additions & 1 deletion cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"log"
"net/http"
_ "net/http/pprof" // подключаем пакет pprof

"github.com/Zagir2000/alert/internal/server/handlers"
"github.com/Zagir2000/alert/internal/server/logger"
Expand Down
11 changes: 5 additions & 6 deletions internal/agent/metricscollect/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func (m *RuntimeMetrics) AddVaueMetricGopsutil() error {
}

func (m *RuntimeMetrics) jsonMetricsToBatch() []byte {
metrics := make([]models.Metrics, 0, len(m.RuntimeMemstats)+2)
var metrics []models.Metrics
for k, v := range m.RuntimeMemstats {
valueGauge := v
jsonGauge := &models.Metrics{
Expand Down Expand Up @@ -147,7 +147,7 @@ func (m *RuntimeMetrics) jsonMetricsToBatch() []byte {
return out
}

func SendMetrics(res []byte, hash, hostpath string) error {
func (m *RuntimeMetrics) SendMetrics(res []byte, hash, hostpath string) error {
url := strings.Join([]string{"http:/", hostpath, "updates/"}, "/")

responseErr := &SendMetricsError{}
Expand Down Expand Up @@ -205,7 +205,7 @@ func (m *RuntimeMetrics) NewСollectMetricGopsutil(ctx context.Context, cancel c
select {
case <-ctx.Done():
return
case <-time.After(time.Duration(1) * time.Second):
default:
err := m.AddVaueMetricGopsutil()
if err != nil {
log.Println("Error in collect metrics 1", err)
Expand Down Expand Up @@ -239,11 +239,10 @@ func gzipCompress(data []byte) ([]byte, error) {
return buf.Bytes(), err
}

func SendMetricsGor(jobs <-chan []byte, runAddr, secretKey string) error {
func (m *RuntimeMetrics) SendMetricsGor(jobs <-chan []byte, runAddr, secretKey string) error {
for j := range jobs {

hash := hash.CrateHash(secretKey, j, sha256.New)
err := SendMetrics(j, hash, runAddr)
err := m.SendMetrics(j, hash, runAddr)
if err != nil {
log.Println("Error in send metrics:", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/agent/metricscollect/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestRuntimeMetrics_SendMetrics(t *testing.T) {
if err != nil {
fmt.Println(err)
}
if err := SendMetrics(res, tt.args.hostpath, ""); (err != nil) != tt.wantErr {
if err := m.SendMetrics(res, tt.args.hostpath, ""); (err != nil) != tt.wantErr {
t.Errorf("RuntimeMetrics.SendMetrics() error = %v, wantErr %v", err, tt.wantErr)
}
})
Expand Down
13 changes: 0 additions & 13 deletions internal/server/handlers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ package handlers

import (
"context"
"net/http/pprof"
_ "net/http/pprof" // подключаем пакет pprof

"github.com/Zagir2000/alert/internal/server/logger"
"github.com/go-chi/chi/v5"
Expand All @@ -20,16 +18,5 @@ func Router(ctx context.Context, log *zap.Logger, newHandStruct *MetricHandlerDB
r.Get("/", gzipMiddleware(newHandStruct.GetAllMetrics(ctx, log)))
r.Get("/value/{metricType}/{metricName}", gzipMiddleware(newHandStruct.GetNowValueMetrics(ctx, log)))
r.Get("/ping", gzipMiddleware(newHandStruct.PingDBConnect(ctx, log)))

r.HandleFunc("/debug/pprof/", pprof.Index)
r.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
r.HandleFunc("/debug/pprof/profile", pprof.Profile)
r.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
r.HandleFunc("/debug/pprof/trace", pprof.Trace)

r.Handle("/debug/pprof/block", pprof.Handler("block"))
r.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
r.Handle("/debug/pprof/heap", pprof.Handler("heap"))
r.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
return r
}

0 comments on commit 139a657

Please sign in to comment.