서버, 클라이언트 config 분리

This commit is contained in:
2023-06-09 11:53:31 +09:00
parent cff643310b
commit 1d87ef3501
4 changed files with 73 additions and 10 deletions

View File

@ -2,7 +2,10 @@ package client
import (
"context"
"encoding/json"
"errors"
"io"
"io/fs"
"os"
"os/exec"
"os/signal"
@ -27,6 +30,15 @@ import (
sigar "github.com/cloudfoundry/gosigar"
)
type clientConfig struct {
GrpcAddress string `json:"grpc_server_address"`
HttpAddress string `json:"http_server_address"`
}
type totalConfig struct {
Client clientConfig `json:"houston_client"`
}
type HoustonClient interface {
SetReportMetrics(map[string]float32)
Shutdown()
@ -152,8 +164,25 @@ func (hc *houstonClient) makeOperationQueryRequest() *protos.OperationQueryReque
}
}
func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) {
client, err := grpc.Dial(grpcAddr, grpc.WithBlock(), grpc.WithTransportCredentials(insecure.NewCredentials()))
func NewClient() (HoustonClient, error) {
bt, err := os.ReadFile("config.json")
if errors.Is(err, fs.ErrNotExist) {
return nil, err
}
var config totalConfig
if err := json.Unmarshal(bt, &config); err != nil {
return nil, err
}
if len(config.Client.GrpcAddress) == 0 {
return nil, errors.New("client.grpc_server_address is missing")
}
if len(config.Client.HttpAddress) == 0 {
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
}
@ -184,7 +213,7 @@ func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) {
client: client,
extraMetrics: unsafe.Pointer(&map[string]float32{}),
deploys: deploys,
httpAddr: httpAddr,
httpAddr: config.Client.HttpAddress,
timestamp: exefi.ModTime().String(),
}
@ -262,7 +291,11 @@ func NewClient(grpcAddr string, httpAddr string) (HoustonClient, error) {
err := hc.withdraw(&wr)
if err == nil {
prog := gatherDeployedPrograms(wr.Name)
hc.deploys[wr.Name] = prog
if len(prog) == 0 {
delete(hc.deploys, wr.Name)
} else {
hc.deploys[wr.Name] = prog
}
op.Refresh(ctx, hc.makeOperationQueryRequest())
} else {
logger.Println(err)