Skip to content

Commit

Permalink
fix #79
Browse files Browse the repository at this point in the history
  • Loading branch information
sunface committed May 9, 2019
1 parent d602615 commit 9cf2187
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 36 deletions.
41 changes: 17 additions & 24 deletions agent/service/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
type Collector struct {
hash *g.Hash
sync.RWMutex
clients map[string]*tcpClient
key string // 使用的key
clients map[string]*tcpClient // kehuduan
}

func (c *Collector) add(key, addr string) error {
Expand All @@ -32,25 +33,22 @@ func (c *Collector) add(key, addr string) error {
c.hash.Add(key)
ruleKey, err := c.hash.Get(gAgent.appName)
if err != nil {
c.Lock()
delete(c.clients, key)
c.Unlock()
c.hash.Remove(key)
logger.Warn("hash get", zap.String("error", err.Error()))
return err
}
// 链接
if strings.EqualFold(ruleKey, key) {
if !strings.EqualFold(ruleKey, c.key) {
// 查看其他链接,如果有链接就关闭
c.RLock()
for _, oldClient := range c.clients {
if oldClient.isStart {
oldClient.close()
}
oldClient.close()
}
c.RUnlock()
// 新链接启动
go newClient.init(addr)

// 保存key
c.key = ruleKey
}
return nil
}
Expand All @@ -62,19 +60,17 @@ func (c *Collector) add(key, addr string) error {
return err
}

if strings.EqualFold(ruleKey, key) {
if !strings.EqualFold(ruleKey, c.key) {
// 存在并符合链接,检查是否已经建立链接
if !newClient.isStart {
// 查看其他链接,如果有链接就关闭
c.RLock()
for _, oldClient := range c.clients {
if oldClient.isStart {
oldClient.close()
}
}
c.RUnlock()
go newClient.init(addr)
c.RLock()
for _, oldClient := range c.clients {
oldClient.close()
}

c.RUnlock()
go newClient.init(addr)
// 保存key
c.key = ruleKey
}

return nil
Expand All @@ -91,11 +87,8 @@ func (c *Collector) del(key string) error {
c.Lock()
delete(c.clients, key)
c.Unlock()
if client.isStart {
client.close()
}
client.close()
}

return nil
}

Expand Down
6 changes: 1 addition & 5 deletions agent/service/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ func (t *tcpClient) init(addr string) error {
ticker := time.NewTicker(time.Duration(misc.Conf.Collector.Keeplive) * time.Second)

defer func() {
t.isStart = false
if err := recover(); err != nil {
logger.Warn("tcp init", zap.Stack("server"), zap.Any("err", err))
}
Expand All @@ -56,11 +57,6 @@ func (t *tcpClient) init(addr string) error {
return err
}

// 链接不能使用
defer func() {
t.isStart = false
}()

t.isStart = true

// 启动心跳
Expand Down
14 changes: 7 additions & 7 deletions collector/collector.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ common:
admintoken: "tracing.dev"

collector:
addr: "127.0.0.1:8082"
addr: "127.0.0.1:8081"
# addr: "10.77.0.125:8082"
# addr: "10.50.14.236:8082"
timeout: 30
Expand All @@ -19,8 +19,8 @@ etcd:
# - "10.7.24.191:2379"
# - "10.7.24.192:2379"
timeout: 10
reporttime: 5
ttl: 10
reporttime: 10
ttl: 30
reportdir: "/collecotr"

apps:
Expand All @@ -29,10 +29,10 @@ apps:

storage:
cluster:
# - "10.77.0.130:9042"
- "10.77.64.47:9042"
- "10.77.64.46:9042"
- "10.77.64.48:9042"
- "10.77.0.130:9042"
# - "10.77.64.47:9042"
# - "10.77.64.46:9042"
# - "10.77.64.48:9042"
keyspace: "tracing_v1_datacenter"
numconns: 100
spancachelen: 1000
Expand Down

0 comments on commit 9cf2187

Please sign in to comment.