Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix 100% cpu #13

Merged
merged 2 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions constant.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package coralogix

import "time"

const (
// MaxLogBufferSize is maximum log buffer size (default=128MiB)
MaxLogBufferSize uint64 = 128 * (1024 * 1024)
Expand All @@ -8,10 +10,10 @@ const (
MaxLogChunkSize uint64 = 1.5 * (1024 * 1024)

// NormalSendSpeedInterval is a bulk send interval in normal mode
NormalSendSpeedInterval float64 = 0.5
NormalSendSpeedInterval = 1 * time.Second
daidokoro marked this conversation as resolved.
Show resolved Hide resolved

// FastSendSpeedInterval is a bulk send interval in fast mode
FastSendSpeedInterval float64 = 0.1
FastSendSpeedInterval = 500 * time.Millisecond

// TimeDelayTimeout is a timeout for time-delay request
TimeDelayTimeout uint = 5
Expand Down
16 changes: 11 additions & 5 deletions manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type LoggerManager struct {
TimeDelta float64 // Time difference between local machine and Coralogix servers
TimeDeltaLastUpdate int // Last time-delta update time
Stopped bool // Is current logger manager stopped
SendInterval time.Duration // Send bulk logs interval
LogsBuffer LogBuffer // Logs buffer
Credentials // Credentials for Coralogix account
Lock sync.WaitGroup // CoralogixLogger manager locker
Expand All @@ -25,6 +26,7 @@ func NewLoggerManager(PrivateKey string, ApplicationName string, SubsystemName s
0,
0,
false,
0,
LogBuffer{},
Credentials{
PrivateKey,
Expand Down Expand Up @@ -126,7 +128,7 @@ func (manager *LoggerManager) SendBulk(SyncTime bool) bool {

// Run should work in separate thread and asynchronously operate with logs
func (manager *LoggerManager) Run() {
var NextSendInterval float64
var NextSendInterval time.Duration

defer manager.Lock.Done()

Expand All @@ -138,14 +140,18 @@ func (manager *LoggerManager) Run() {

manager.SendBulk(manager.SyncTime)

if manager.LogsBuffer.Size() > (MaxLogChunkSize / 2) {
NextSendInterval = FastSendSpeedInterval
if manager.SendInterval > 0 {
NextSendInterval = manager.SendInterval
} else {
NextSendInterval = NormalSendSpeedInterval
if manager.LogsBuffer.Size() > (MaxLogChunkSize / 2) {
NextSendInterval = FastSendSpeedInterval
} else {
NextSendInterval = NormalSendSpeedInterval
}
}

DebugLogger.Printf("Next buffer check is scheduled in %.1f seconds\n", NextSendInterval)
time.Sleep(time.Duration(NextSendInterval) * time.Second)
time.Sleep(NextSendInterval)
}
}

Expand Down