refactor: move const in config

This commit is contained in:
2026-05-23 17:17:35 +03:00
parent 8a3da3b017
commit 593475f2cf
6 changed files with 24 additions and 24 deletions
+2 -1
View File
@@ -1,3 +1,4 @@
PORT=6756 PORT=6756
LOG_LEVEL=DEBUG LOG_LEVEL=DEBUG
MODE=DEV MODE=DEV
HEARTBEAT=5
+2 -2
View File
@@ -59,9 +59,9 @@ func (a *App) Run() {
a.log.Info().Msg("migrations applied successfully") a.log.Info().Msg("migrations applied successfully")
hubStore := store.NewHubStore(DBConn) hubStore := store.NewHubStore(DBConn)
hubService := hub_service.NewHubService(hubStore, a.log) hubService := hub_service.NewHubService(hubStore, a.cfg.Heartbeat, a.log)
statusNotifier := notifier.NewStatusNotifier() statusNotifier := notifier.NewStatusNotifier()
connManger := connection_manager.NewConnectionManager(hubStore, statusNotifier, a.log) connManger := connection_manager.NewConnectionManager(hubStore, statusNotifier, a.cfg.Heartbeat*1000, a.log)
a.log.Info().Msg("starting hub service") a.log.Info().Msg("starting hub service")
@@ -8,17 +8,16 @@ import (
"google.golang.org/grpc/metadata" "google.golang.org/grpc/metadata"
) )
const heartbeatTimeoutMS = 6000
type ConnectionManager struct { type ConnectionManager struct {
heartbeat heartbeatStore heartbeat heartbeatStore
log zerolog.Logger log zerolog.Logger
status statusNotifier status statusNotifier
agentConnStore *AgentConnStore agentConnStore *AgentConnStore
heartbeatTimeout int
} }
func NewConnectionManager(heartbeat heartbeatStore, status statusNotifier, logger zerolog.Logger) *ConnectionManager { func NewConnectionManager(heartbeat heartbeatStore, status statusNotifier, heartbeatTimeoutMS int, logger zerolog.Logger) *ConnectionManager {
return &ConnectionManager{heartbeat: heartbeat, log: logger, status: status, agentConnStore: NewAgentConnStore()} return &ConnectionManager{heartbeat: heartbeat, log: logger, status: status, agentConnStore: NewAgentConnStore(), heartbeatTimeout: heartbeatTimeoutMS + 600}
} }
func (c *ConnectionManager) NewConnection(stream StreamConn) error { func (c *ConnectionManager) NewConnection(stream StreamConn) error {
@@ -32,7 +31,7 @@ func (c *ConnectionManager) NewConnection(stream StreamConn) error {
status := c.status.New(AgentID) status := c.status.New(AgentID)
agent := newAgentConnection(AgentID, stream, c.heartbeat, status, heartbeatTimeoutMS, c.log) agent := newAgentConnection(AgentID, stream, c.heartbeat, status, c.heartbeatTimeout, c.log)
c.agentConnStore.Add(AgentID, agent) c.agentConnStore.Add(AgentID, agent)
defer c.agentConnStore.Delete(AgentID) defer c.agentConnStore.Delete(AgentID)
@@ -24,7 +24,7 @@ func newConnectionManagerTestHarness(t *testing.T) *connectionManagerTestHarness
heartbeat := &heartBeatMock{doneCh: make(chan struct{}, 2)} heartbeat := &heartBeatMock{doneCh: make(chan struct{}, 2)}
status := &statusNotifierMock{agentIDCh: make(chan string, 1)} status := &statusNotifierMock{agentIDCh: make(chan string, 1)}
manager := NewConnectionManager(heartbeat, status, zerolog.New(nil)) manager := NewConnectionManager(heartbeat, status, 10000, zerolog.New(nil))
return &connectionManagerTestHarness{manager: manager, status: status, heartbeat: heartbeat} return &connectionManagerTestHarness{manager: manager, status: status, heartbeat: heartbeat}
} }
+7 -8
View File
@@ -12,8 +12,6 @@ import (
"github.com/rs/zerolog" "github.com/rs/zerolog"
) )
const HEARTBEAT = 5
type Store interface { type Store interface {
NewAgent(ctx context.Context, agent domainHub.CreateAgentModel) error NewAgent(ctx context.Context, agent domainHub.CreateAgentModel) error
GetAgentByAgentID(ctx context.Context, AgentID string) (domainHub.AgentModel, error) GetAgentByAgentID(ctx context.Context, AgentID string) (domainHub.AgentModel, error)
@@ -21,12 +19,13 @@ type Store interface {
} }
type HubService struct { type HubService struct {
store Store store Store
log zerolog.Logger log zerolog.Logger
heartbeatTimeout int
} }
func NewHubService(store Store, logger zerolog.Logger) *HubService { func NewHubService(store Store, heartbeatTimeout int, logger zerolog.Logger) *HubService {
return &HubService{log: logger, store: store} return &HubService{log: logger, store: store, heartbeatTimeout: heartbeatTimeout}
} }
func (h *HubService) RegisterAgent(ctx context.Context, data domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error) { func (h *HubService) RegisterAgent(ctx context.Context, data domain.RegisterAgentRequest) (domain.RegisterAgentResponse, error) {
@@ -47,7 +46,7 @@ func (h *HubService) RegisterAgent(ctx context.Context, data domain.RegisterAgen
return domain.RegisterAgentResponse{}, fmt.Errorf("update agent in db: %w", err) return domain.RegisterAgentResponse{}, fmt.Errorf("update agent in db: %w", err)
} }
h.log.Debug().Str("agentId", agent.AgentID).Msg("agent updated successfully") h.log.Debug().Str("agentId", agent.AgentID).Msg("agent updated successfully")
return domain.RegisterAgentResponse{AgentID: agent.AgentID, Heartbeat: HEARTBEAT}, nil return domain.RegisterAgentResponse{AgentID: agent.AgentID, Heartbeat: h.heartbeatTimeout}, nil
} }
AgentID, err := hasher.MakeID(data.Host, data.AgentName) AgentID, err := hasher.MakeID(data.Host, data.AgentName)
@@ -62,5 +61,5 @@ func (h *HubService) RegisterAgent(ctx context.Context, data domain.RegisterAgen
if err := h.store.NewAgent(ctx, agentStore); err != nil { if err := h.store.NewAgent(ctx, agentStore); err != nil {
return domain.RegisterAgentResponse{}, fmt.Errorf("insert new agent: %w", err) return domain.RegisterAgentResponse{}, fmt.Errorf("insert new agent: %w", err)
} }
return domain.RegisterAgentResponse{AgentID: AgentID, Heartbeat: HEARTBEAT}, nil return domain.RegisterAgentResponse{AgentID: AgentID, Heartbeat: h.heartbeatTimeout}, nil
} }
+4 -3
View File
@@ -9,9 +9,10 @@ import (
) )
type Config struct { type Config struct {
LogLevel string `env:"LOG_LEVEL" env-default:"INFO"` LogLevel string `env:"LOG_LEVEL" env-default:"INFO"`
Mode string `env:"MODE" env-default:"DEV"` Mode string `env:"MODE" env-default:"DEV"`
Port int `env:"PORT" env-default:"9000"` Port int `env:"PORT" env-default:"9000"`
Heartbeat int `env:"HEARTBEAT" env-default:"5"`
} }
func NewConfig() (*Config, error) { func NewConfig() (*Config, error) {