Skip to content

Commit

Permalink
Merge pull request #7 from easyops-cn/jax/time_sleep
Browse files Browse the repository at this point in the history
chore(): 优化sleep config
  • Loading branch information
revisegoal authored Jul 22, 2024
2 parents 338a45b + d7ac496 commit f04b221
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
3 changes: 2 additions & 1 deletion net/net_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/shirou/gopsutil/v3/net/sleepconfig"
"io"
"io/ioutil"
"net"
Expand Down Expand Up @@ -672,7 +673,7 @@ func getProcInodesAllWithContext(ctx context.Context, root string, max int) (map
ret := make(map[string][]inodeMap)

for i, pid := range pids {
TimeSleep(i)
sleepconfig.TimeSleep(i)
t, err := getProcInodes(root, pid, max)
if err != nil {
// skip if permission error or no longer exists
Expand Down
6 changes: 3 additions & 3 deletions net/net_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package net
import (
"context"
"fmt"
"github.com/shirou/gopsutil/v3/net/sleepconfig"
"net"
"os"
"syscall"
Expand Down Expand Up @@ -413,8 +414,7 @@ func getTCPConnections(family uint32) ([]ConnectionStat, error) {
return nil, fmt.Errorf("faimly must be required")
}

for i := 0; ; i++ {
TimeSleep(i)
for {
switch family {
case kindTCP4.family:
if len(buf) > 0 {
Expand Down Expand Up @@ -465,7 +465,7 @@ func getTCPConnections(family uint32) ([]ConnectionStat, error) {
}

for i := 0; i < length; i++ {
TimeSleep(i)
sleepconfig.TimeSleep(i)
switch family {
case kindTCP4.family:
mibs := (*mibTCPRowOwnerPid)(unsafe.Pointer(&buf[index]))
Expand Down
5 changes: 3 additions & 2 deletions net/netlink/inetdiag.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ package netlink
import (
"bytes"
"encoding/binary"
"github.com/shirou/gopsutil/v3/net/sleepconfig"
"hash/fnv"
"io"
"net"
Expand Down Expand Up @@ -162,8 +163,8 @@ done:
return nil, err
}

for _, m := range msgs {

for i, m := range msgs {
sleepconfig.TimeSleep(i)
if m.Header.Type == syscall.NLMSG_DONE {
break done
}
Expand Down
7 changes: 6 additions & 1 deletion net/sleep.go → net/sleepconfig/sleepconfig.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package net
package sleepconfig

import (
"go.easyops.local/slog"
Expand All @@ -9,19 +9,24 @@ import (
var config SleepConfig

type SleepConfig struct {
enable bool
sleepMs atomic.Int64
loopBeforeSleep atomic.Int32
logger slog.Logger
}

func InitSleepConfig(ms int, loop int, logger slog.Logger) {
config = SleepConfig{}
config.enable = true
config.sleepMs.Store(int64(ms))
config.loopBeforeSleep.Store(int32(loop))
config.logger = logger
}

func TimeSleep(i int) {
if !config.enable {
return
}
j := int(config.loopBeforeSleep.Load())
if j == 0 || (i+1)%j != 0 {
return
Expand Down

0 comments on commit f04b221

Please sign in to comment.