Skip to content

Commit

Permalink
use configor
Browse files Browse the repository at this point in the history
  • Loading branch information
tonicmuroq committed Apr 15, 2021
1 parent eb2c6f5 commit a3f5da4
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 14 deletions.
3 changes: 1 addition & 2 deletions engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package engine
import (
"context"
"os"
"time"

engineapi "github.com/docker/docker/client"
"github.com/projecteru2/agent/common"
Expand Down Expand Up @@ -130,7 +129,7 @@ func (e *Engine) crash() error {
return err
}
container.Healthy = false
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(e.config.GlobalConnectionTimeout)*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), e.config.GlobalConnectionTimeout)
defer cancel()
if err := e.store.SetContainerStatus(ctx, container, e.node); err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion engine/health_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (e *Engine) checkOneContainer(container *types.Container) {
container.Healthy = checkSingleContainerHealthy(container, timeout)
}

cctx, cancel := context.WithTimeout(context.Background(), time.Duration(e.config.GlobalConnectionTimeout)*time.Second)
cctx, cancel := context.WithTimeout(context.Background(), e.config.GlobalConnectionTimeout)
defer cancel()
if err := e.store.SetContainerStatus(cctx, container, e.node); err != nil {
log.Errorf("[checkOneContainer] update deploy status failed %v", err)
Expand Down
5 changes: 2 additions & 3 deletions engine/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"math"
"time"

enginetypes "github.com/docker/docker/api/types"
enginecontainer "github.com/docker/docker/api/types/container"
Expand All @@ -29,7 +28,7 @@ func (e *Engine) listContainers(all bool, extend map[string]string) ([]enginetyp
f := getFilter(extend)
opts := enginetypes.ContainerListOptions{Filters: f, All: all}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(e.config.GlobalConnectionTimeout)*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), e.config.GlobalConnectionTimeout)
defer cancel()
return e.docker.ContainerList(ctx, opts)
}
Expand All @@ -41,7 +40,7 @@ func (e *Engine) activated(f bool) error {

func (e *Engine) detectContainer(id string) (*types.Container, error) {
// 标准化为 inspect 的数据
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(e.config.GlobalConnectionTimeout)*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), e.config.GlobalConnectionTimeout)
defer cancel()
c, err := e.docker.ContainerInspect(ctx, id)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions engine/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package engine

import (
"context"
"time"

coreutils "github.com/projecteru2/core/utils"
log "github.com/sirupsen/logrus"
Expand All @@ -27,7 +26,7 @@ func (e *Engine) load() error {
e.attach(c)
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(e.config.GlobalConnectionTimeout)*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), e.config.GlobalConnectionTimeout)
defer cancel()
if err := e.store.SetContainerStatus(ctx, c, e.node); err != nil {
log.Errorf("[load] update deploy status failed %v", err)
Expand Down
4 changes: 2 additions & 2 deletions selfmon/selfmon.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func (m *Selfmon) initNodeStatus(ctx context.Context) {
go func() {
defer close(nodes)
// Get all nodes which are active status, and regardless of pod.
cctx, cancel := context.WithTimeout(ctx, time.Duration(m.config.GlobalConnectionTimeout)*time.Second)
cctx, cancel := context.WithTimeout(ctx, m.config.GlobalConnectionTimeout)
defer cancel()
podNodes, err := m.rpc.ListPodNodes(cctx, &pb.ListNodesOptions{})
if err != nil {
Expand Down Expand Up @@ -164,7 +164,7 @@ func (m *Selfmon) dealNodeStatusMessage(message *pb.NodeStatusStreamMessage) {
}

// TODO maybe we need a distributed lock to control concurrency
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(m.config.GlobalConnectionTimeout)*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), m.config.GlobalConnectionTimeout)
defer cancel()
if _, err := m.rpc.SetNode(ctx, &pb.SetNodeOptions{
Nodename: message.Nodename,
Expand Down
5 changes: 2 additions & 3 deletions store/core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package corestore

import (
"context"
"time"

pb "github.com/projecteru2/core/rpc/gen"
"github.com/projecteru2/core/types"
Expand All @@ -12,7 +11,7 @@ import (
func (c *CoreStore) GetNode(nodename string) (*types.Node, error) {
client := c.client.GetRPCClient()

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.config.GlobalConnectionTimeout)*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), c.config.GlobalConnectionTimeout)
defer cancel()
resp, err := client.GetNode(ctx, &pb.GetNodeOptions{Nodename: nodename})
if err != nil {
Expand Down Expand Up @@ -49,7 +48,7 @@ func (c *CoreStore) UpdateNode(node *types.Node) error {
opts.StatusOpt = types.TriFalse
}

ctx, cancel := context.WithTimeout(context.Background(), time.Duration(c.config.GlobalConnectionTimeout)*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), c.config.GlobalConnectionTimeout)
defer cancel()
_, err := client.SetNode(ctx, opts)
return err
Expand Down
3 changes: 2 additions & 1 deletion types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package types

import (
"os"
"time"

coretypes "github.com/projecteru2/core/types"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -53,7 +54,7 @@ type Config struct {
HealthCheck HealthCheckConfig `yaml:"healcheck"`
Etcd coretypes.EtcdConfig `yaml:"etcd"`

GlobalConnectionTimeout int `yaml:"global_connection_timeout" default:"5"`
GlobalConnectionTimeout time.Duration `yaml:"global_connection_timeout" default:"5s"`
}

// PrepareConfig 从cli覆写并做准备
Expand Down

0 comments on commit a3f5da4

Please sign in to comment.