Skip to content

Commit

Permalink
fix: the log core and small improve
Browse files Browse the repository at this point in the history
  • Loading branch information
microup committed Jul 10, 2024
1 parent 24f808e commit 6d6dee2
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 105 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
PROJECT_NAME=vbalancer

.PHONY: all dep build test lint mocks

all: lint test race build
Expand Down Expand Up @@ -26,12 +28,15 @@ lint:
test:
go test -v ./...

race:
race:
go test -race -v ./...

build:
go build -o build/$(PROJECT_NAME) cmd/$(PROJECT_NAME)/$(PROJECT_NAME).go

build-win:
go build -o build/$(PROJECT_NAME).exe cmd/$(PROJECT_NAME)/$(PROJECT_NAME).go

docker-create:
docker build --tag vbalancer . -f Dockerfile

Expand Down
8 changes: 7 additions & 1 deletion cmd/vbalancer/vbalancer.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package main

import (
"context"
"runtime"
"vbalancer/internal/app"
)

func main() {
app.Run()
runtime.GOMAXPROCS(runtime.NumCPU())

ctx := context.Background()

app.Run(ctx)
}
43 changes: 9 additions & 34 deletions internal/app/vbalancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"log"
"os"
"os/signal"
"runtime"
"syscall"

"vbalancer/internal/config"
Expand All @@ -20,11 +19,7 @@ import (
var ErrRecoveredPanic = errors.New("recovered from panic")

// Run this is the function of an application that starts a proxy server.
func Run() {
runtime.GOMAXPROCS(runtime.NumCPU())

ctx := context.Background()

func Run(ctx context.Context) {
cfg := config.New()
if err := cfg.Init(); err != nil {
log.Panicf("failed to initialize configuration: %s", err.Error())
Expand All @@ -39,50 +34,30 @@ func Run() {
defer func() {
if err := recover(); err != nil {
msgErr := fmt.Errorf("%w: %v", ErrRecoveredPanic, err)

logger.Add(types.Fatal, types.ErrRecoverPanic, msgErr)
log.Printf("%v", msgErr)
}
}()

proxy, err := core.YamlToObject(cfg.Proxy, proxy.New())
if err != nil {
logger.Add(types.Fatal, types.ErrCantGetProxyObject, "can't get proxy object")
log.Panicf("can't get proxy object, code: %d", types.ErrCantGetProxyObject)
}

if err = proxy.Init(ctx, logger); err != nil {
logger.Add(types.Fatal, types.ErrCantInitProxy, fmt.Errorf("%w", err))
log.Panicf("%v, code: %d", err, types.ErrCantInitProxy)
}

stopSignal := make(chan os.Signal, 1)
signal.Notify(stopSignal, os.Interrupt, syscall.SIGTERM)
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt, syscall.SIGTERM)

chanListenProxy := make(chan error)

go func() {
logger.Add(types.Info, types.ResultOK, fmt.Sprintf("start server addr on %s", proxy.Port))
chanListenProxy <- proxy.ListenAndServe(ctx)

stopSignal <- syscall.SIGTERM
}()
proxy.ListenAndServe(ctx)

select {
case s := <-interrupt:
logger.Add(types.Info, types.ResultOK, "syscall.SIGTERM", s.String())
case <-ctx.Done():
logger.Add(types.Info, types.ResultOK, "get ctx.Done()...")

case listenErr := <-chanListenProxy:
{
if listenErr != nil {
logger.Add(types.Fatal, types.ErrProxy, fmt.Errorf("the proxy was return err: %w", err))
} else {
log.Printf("the proxy was close")
}
}
case <-stopSignal:
{
logger.Add(types.Info, types.ResultOK, "syscall.SIGTERM...")
}
logger.Add(types.Info, types.ResultOK, "Done.")
case err = <-proxy.Notify():
logger.Add(types.Fatal, types.ErrProxy, fmt.Errorf("the proxy returned err: %w", err))
}
}
27 changes: 19 additions & 8 deletions internal/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type Proxy struct {
Peers *peers.Peers `json:"peers" yaml:"peers"`
// Defien allows configuration of blacklist rules to be passed to the proxy server
Rules *rules.Rules `json:"rules" yaml:"rules"`
// Notify is a channel used to send error notifications
notify chan error
}

func New() *Proxy {
Expand All @@ -61,6 +63,7 @@ func New() *Proxy {
Rules: &rules.Rules{
Blacklist: nil,
},
notify: make(chan error, 1),
}
}

Expand Down Expand Up @@ -90,17 +93,25 @@ func (p *Proxy) Init(ctx context.Context, logger Loger) error {
}

// ListenAndServe starts the proxy server.
func (p *Proxy) ListenAndServe(ctx context.Context) error {
proxySrv, err := net.Listen("tcp", p.Port)
if err != nil {
return fmt.Errorf("%w", err)
}
func (p *Proxy) ListenAndServe(ctx context.Context) {
go func() {
proxySrv, err := net.Listen("tcp", p.Port)
if err != nil {
p.notify <- err
return
}

defer proxySrv.Close()
defer proxySrv.Close()
defer close(p.notify)

p.AcceptConnections(ctx, proxySrv)
p.Logger.Add(types.Info, types.ResultOK, fmt.Sprintf("start proxy on: %s", proxySrv.Addr().String()))

return nil
p.AcceptConnections(ctx, proxySrv)
}()
}

func (p *Proxy) Notify() <-chan error {
return p.notify
}

// AcceptConnections accepts connections from the proxy server.
Expand Down
1 change: 1 addition & 0 deletions internal/proxy/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func TestGetProxyPort(t *testing.T) {
MaxCountConnection: 100,
Peers: nil,
Rules: nil,
notify: make(chan error),
}

for _, tc := range testCases {
Expand Down
5 changes: 0 additions & 5 deletions internal/vlog/log_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,4 @@ type LogFile struct {
FileName string `json:"fileName,omitempty"`
// FileSize is the size of the file in string format.
FileSize string `json:"fileSize,omitempty"`
// Kind is the type of file.
Kind string `json:"kind,omitempty"`
}

// UpdateFileInfo is a type that represents a function that returns a pointer to a FileInfo.
type UpdateFileInfo func() *LogFile
69 changes: 33 additions & 36 deletions internal/vlog/log_files.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,40 @@ import (
"fmt"
"os"
"path/filepath"
"sync/atomic"
"vbalancer/internal/core"
"vbalancer/internal/types"
"vbalancer/internal/version"
)

// newFileLog - function for creating a new file log.
func (v *VLog) newFileLog(newFileName string, isNewFileLog bool) error {
func (v *VLog) newFileLog(isNewFileLog bool) error {
if isNewFileLog {
v.MapLastLogRecords = make([]string, 0)

err := v.Close()
if err != nil {
return err
if err := v.Close(); err != nil {
return fmt.Errorf("%w", err)
}
} else if !isNewFileLog {
if v.fileLog != nil {
_ = v.fileLog.Close()
}
}

var err error
if _, err = os.Stat(v.cfg.DirLog); os.IsNotExist(err) {
if _, err := os.Stat(v.cfg.DirLog); os.IsNotExist(err) {
err = os.Mkdir(v.cfg.DirLog, types.MaskDir)
if err != nil {
return fmt.Errorf("%w", err)
}
}

v.fileLog, err = v.open(newFileName)
var err error
v.fileLog, err = v.open()
if v.fileLog == nil || err != nil {
return fmt.Errorf("%w", err)
}

_, err = v.fileLog.WriteString(v.headerCSV + "\n")
if err != nil {
if _, err = v.fileLog.WriteString(v.headerCSV + "\n"); err != nil {
return fmt.Errorf("%w", err)
}

Expand All @@ -47,7 +46,7 @@ func (v *VLog) newFileLog(newFileName string, isNewFileLog bool) error {

// Close function for closing the file log.
func (v *VLog) Close() error {
v.wgNewLog.Wait()
v.wg.Wait()

if v.fileLog != nil {
if err := v.fileLog.Close(); err != nil {
Expand All @@ -59,17 +58,13 @@ func (v *VLog) Close() error {
}

// open function for opening the file log.
func (v *VLog) open(newFileName string) (*os.File, error) {
v.countToLogID++

func (v *VLog) open() (*os.File, error) {
var fileNameLog string

if newFileName == "" {
timeCreateLogFile := v.startTimeLog.Format("20060102150405")
fileNameLog = fmt.Sprintf("%s_%d_%d.%s", timeCreateLogFile, version.Get(), v.countToLogID, types.LogFileExtension)
} else {
fileNameLog = fmt.Sprintf("%s_%d_%d.%s", newFileName, version.Get(), v.countToLogID, types.LogFileExtension)
}
timeCreateLogFile := v.startTimeLog.Format("20060102150405")
fileNameLog = fmt.Sprintf("%s_%d_%d.%s", timeCreateLogFile, version.Get(), v.idLog, types.LogFileExtension)

atomic.AddUint64(&v.idLog, 1)

fileNameLog = filepath.Join(v.cfg.DirLog, fileNameLog)

Expand All @@ -95,7 +90,6 @@ func (v *VLog) GetCurrentFileLogInfo() *LogFile {
return &LogFile{
FileName: v.fileLog.Name(),
FileSize: core.HumanFileSize(float64(fileInfo.Size())),
Kind: types.LogFileExtension,
}
}

Expand All @@ -107,7 +101,7 @@ func (v *VLog) checkToCreateNewLogFile() error {
}

if fileInfo == nil {
err = v.newFileLog("", false)
err = v.newFileLog(false)
if err != nil {
return fmt.Errorf("%w", err)
}
Expand All @@ -118,25 +112,27 @@ func (v *VLog) checkToCreateNewLogFile() error {
fileSizeBytes := fileInfo.Size()
fileSizeMB := float64(fileSizeBytes) / (types.LengthKilobytesInBytes * types.LengthKilobytesInBytes)

if fileSizeMB > v.cfg.FileSizeMB {
oldFileCSV := v.fileLog.Name()
if fileSizeMB < v.cfg.FileSizeMB {
return nil
}

err = v.newFileLog("", false)
if err != nil {
return fmt.Errorf("%w", err)
}
oldFileCSV := v.fileLog.Name()

err = core.ArchiveFile(oldFileCSV, fmt.Sprintf("_%s.zip", types.LogFileExtension))
if err != nil {
return fmt.Errorf("failed to archive file: %w", err)
}
err = v.newFileLog(false)
if err != nil {
return fmt.Errorf("%w", err)
}

err = core.ArchiveFile(oldFileCSV, fmt.Sprintf("_%s.zip", types.LogFileExtension))
if err != nil {
return fmt.Errorf("failed to archive file: %w", err)
}

fileCsv := filepath.Join(v.cfg.DirLog, fileInfo.Name())
err = os.Remove(fileCsv)
fileCsv := filepath.Join(v.cfg.DirLog, fileInfo.Name())
err = os.Remove(fileCsv)

if err != nil {
return fmt.Errorf("failed to os remove file:%s err:%w", fileCsv, err)
}
if err != nil {
return fmt.Errorf("failed to os remove file:%s err:%w", fileCsv, err)
}

return nil
Expand All @@ -150,6 +146,7 @@ func (v *VLog) removeOldRecordsFromMemory() {
_, v.MapLastLogRecords = v.MapLastLogRecords[0], v.MapLastLogRecords[1:]
xLast, v.MapLastLogRecords = v.MapLastLogRecords[len(v.MapLastLogRecords)-1],
v.MapLastLogRecords[:len(v.MapLastLogRecords)-1]

v.MapLastLogRecords = append(v.MapLastLogRecords, xLast)
}
}
Loading

0 comments on commit 6d6dee2

Please sign in to comment.