monitor func 제거하고 grpc dial을 timeout context로 교체

This commit is contained in:
2023-06-11 17:14:44 +09:00
parent 1d525eb7cf
commit 6719d9b6c1
3 changed files with 15 additions and 184 deletions

View File

@ -18,19 +18,15 @@ import (
"sync"
"sync/atomic"
"syscall"
"time"
"unsafe"
"repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/houston/shared"
"repositories.action2quare.com/ayo/houston/shared/protos"
"time"
"github.com/shirou/gopsutil/v3/cpu"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
sigar "github.com/cloudfoundry/gosigar"
)
type clientConfig struct {
@ -99,10 +95,6 @@ type houstonClient struct {
wg sync.WaitGroup
}
func bToMb(b uint64) uint32 {
return uint32(b / 1024 / 1024)
}
func unmarshal[T any](val *T, src map[string]string) {
argval := reflect.ValueOf(val)
for i := 0; i < argval.Elem().Type().NumField(); i++ {
@ -186,11 +178,22 @@ func NewClient() (HoustonClient, error) {
return nil, errors.New("client.http_server_address is missing")
}
client, err := grpc.Dial(config.Client.GrpcAddress, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err != nil {
return nil, err
var client *grpc.ClientConn
for {
logger.Println("grpc.DialContext :", config.Client.GrpcAddress)
dialContext, cancelDial := context.WithTimeout(context.Background(), 15*time.Second)
client, err = grpc.DialContext(dialContext, config.Client.GrpcAddress, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
if err == nil {
cancelDial()
break
}
cancelDial()
logger.Println("grpc.DialContext failed :", err)
time.Sleep(5 * time.Second)
}
logger.Println("grpc.DialContext succeeded")
exefile, err := os.Executable()
if err != nil {
return nil, err
@ -222,37 +225,6 @@ func NewClient() (HoustonClient, error) {
}
ctx, cancel := context.WithCancel(context.Background())
hc.wg.Add(1)
go func() {
defer hc.wg.Done()
// regularly send status
sc := protos.NewMonitorClient(client)
hn, _ := os.Hostname()
mem := sigar.Mem{}
mem.Get()
metrics := &protos.Metrics{
Hostname: hn,
Total: bToMb(mem.Total),
}
for {
select {
case <-ctx.Done():
return
case <-time.After(5 * time.Second):
percent, _ := cpu.Percent(0, false)
metrics.Cpu = float32(percent[0])
metrics.Free = bToMb(mem.ActualFree)
metrics.Metrics = *(*map[string]float32)(atomic.LoadPointer(&hc.extraMetrics))
sc.Report(ctx, metrics, grpc.WaitForReady(true))
mem.Get()
}
}
}()
exitChan := make(chan *exec.Cmd, 10)
operationChan := make(chan *protos.OperationQueryResponse, 10)