#163 register services if they are expired

This commit is contained in:
smallnest
2017-10-27 17:30:34 +08:00
parent 1686dca88a
commit 6e35de033e
5 changed files with 67 additions and 10 deletions

View File

@@ -31,7 +31,7 @@ ineffassign:
gocyclo: gocyclo:
@ gocyclo -over 20 $(shell find . -name "*.go" |egrep -v "_testutils/*|vendor/*|pb\.go|_test\.go") @ gocyclo -over 20 $(shell find . -name "*.go" |egrep -v "_testutils/*|vendor/*|pb\.go|_test\.go")
check: staticcheck gosimple ineffassign gocyclo check: staticcheck gosimple ineffassign
doc: doc:
godoc -http=:6060 godoc -http=:6060
@@ -43,10 +43,10 @@ fmt:
go fmt ./... go fmt ./...
build: build:
go build -tags "reuseport kcp quic zookeeper etcd consul ping" ./... go build ./...
buildu: build-all:
go build -tags "reuseport kcp quic zookeeper etcd consul ping" ./... go build -tags "reuseport kcp quic zookeeper etcd consul ping" ./...
test: test:
go test -tags "reuseport kcp quic zookeeper etcd consul ping" ./... go test -race -tags "reuseport kcp quic zookeeper etcd consul ping" ./...

View File

@@ -125,8 +125,6 @@ func (s *Server) Serve(network, address string) (err error) {
// creating a new service goroutine for each. // creating a new service goroutine for each.
// The service goroutines read requests and then call services to reply to them. // The service goroutines read requests and then call services to reply to them.
func (s *Server) serveListener(ln net.Listener) error { func (s *Server) serveListener(ln net.Listener) error {
s.ln = ln
if s.Plugins == nil { if s.Plugins == nil {
s.Plugins = &pluginContainer{} s.Plugins = &pluginContainer{}
} }
@@ -134,6 +132,7 @@ func (s *Server) serveListener(ln net.Listener) error {
var tempDelay time.Duration var tempDelay time.Duration
s.mu.Lock() s.mu.Lock()
s.ln = ln
if s.activeConn == nil { if s.activeConn == nil {
s.activeConn = make(map[net.Conn]struct{}) s.activeConn = make(map[net.Conn]struct{})
} }

View File

@@ -9,6 +9,7 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/docker/libkv" "github.com/docker/libkv"
@@ -33,6 +34,8 @@ type ConsulRegisterPlugin struct {
Metrics metrics.Registry Metrics metrics.Registry
// Registered services // Registered services
Services []string Services []string
metasLock sync.RWMutex
metas map[string]string
UpdateInterval time.Duration UpdateInterval time.Duration
Options *store.Config Options *store.Config
@@ -74,7 +77,16 @@ func (p *ConsulRegisterPlugin) Start() error {
nodePath := fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress) nodePath := fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
kvPaire, err := p.KV.Get(nodePath) kvPaire, err := p.KV.Get(nodePath)
if err != nil { if err != nil {
log.Infof("can't get data of node: %s, because of %v", nodePath, err.Error()) log.Warnf("can't get data of node: %s, will re-create, because of %v", nodePath, err.Error())
p.metasLock.RLock()
meta := p.metas[name]
p.metasLock.RUnlock()
err = p.KV.Put(nodePath, []byte(meta), &store.WriteOptions{TTL: p.UpdateInterval * 2})
if err != nil {
log.Errorf("cannot re-create consul path %s: %v", nodePath, err)
}
} else { } else {
v, _ := url.ParseQuery(string(kvPaire.Value)) v, _ := url.ParseQuery(string(kvPaire.Value))
v.Set("tps", string(data)) v.Set("tps", string(data))
@@ -133,12 +145,19 @@ func (p *ConsulRegisterPlugin) Register(name string, rcvr interface{}, metadata
} }
nodePath = fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress) nodePath = fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
err = p.KV.Put(nodePath, []byte(p.ServiceAddress), &store.WriteOptions{TTL: p.UpdateInterval * 2}) err = p.KV.Put(nodePath, []byte(metadata), &store.WriteOptions{TTL: p.UpdateInterval * 2})
if err != nil { if err != nil {
log.Errorf("cannot create consul path %s: %v", nodePath, err) log.Errorf("cannot create consul path %s: %v", nodePath, err)
return err return err
} }
p.Services = append(p.Services, name) p.Services = append(p.Services, name)
p.metasLock.Lock()
if p.metas == nil {
p.metas = make(map[string]string)
}
p.metas[name] = metadata
p.metasLock.Unlock()
return return
} }

View File

@@ -9,6 +9,7 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/docker/libkv" "github.com/docker/libkv"
@@ -33,6 +34,8 @@ type EtcdRegisterPlugin struct {
Metrics metrics.Registry Metrics metrics.Registry
// Registered services // Registered services
Services []string Services []string
metasLock sync.RWMutex
metas map[string]string
UpdateInterval time.Duration UpdateInterval time.Duration
Options *store.Config Options *store.Config
@@ -71,6 +74,16 @@ func (p *EtcdRegisterPlugin) Start() error {
kvPair, err := p.KV.Get(nodePath) kvPair, err := p.KV.Get(nodePath)
if err != nil { if err != nil {
log.Infof("can't get data of node: %s, because of %v", nodePath, err.Error()) log.Infof("can't get data of node: %s, because of %v", nodePath, err.Error())
p.metasLock.RLock()
meta := p.metas[name]
p.metasLock.RUnlock()
err = p.KV.Put(nodePath, []byte(meta), &store.WriteOptions{TTL: p.UpdateInterval * 2})
if err != nil {
log.Errorf("cannot re-create etcd path %s: %v", nodePath, err)
}
} else { } else {
v, _ := url.ParseQuery(string(kvPair.Value)) v, _ := url.ParseQuery(string(kvPair.Value))
v.Set("tps", string(data)) v.Set("tps", string(data))
@@ -126,12 +139,19 @@ func (p *EtcdRegisterPlugin) Register(name string, rcvr interface{}, metadata st
} }
nodePath = fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress) nodePath = fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
err = p.KV.Put(nodePath, []byte(p.ServiceAddress), &store.WriteOptions{TTL: p.UpdateInterval * 2}) err = p.KV.Put(nodePath, []byte(metadata), &store.WriteOptions{TTL: p.UpdateInterval * 2})
if err != nil { if err != nil {
log.Errorf("cannot create etcd path %s: %v", nodePath, err) log.Errorf("cannot create etcd path %s: %v", nodePath, err)
return err return err
} }
p.Services = append(p.Services, name) p.Services = append(p.Services, name)
p.metasLock.Lock()
if p.metas == nil {
p.metas = make(map[string]string)
}
p.metas[name] = metadata
p.metasLock.Unlock()
return return
} }

View File

@@ -9,6 +9,7 @@ import (
"net/url" "net/url"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/docker/libkv" "github.com/docker/libkv"
@@ -34,6 +35,8 @@ type ZooKeeperRegisterPlugin struct {
Metrics metrics.Registry Metrics metrics.Registry
// Registered services // Registered services
Services []string Services []string
metasLock sync.RWMutex
metas map[string]string
UpdateInterval time.Duration UpdateInterval time.Duration
Options *store.Config Options *store.Config
@@ -76,6 +79,15 @@ func (p *ZooKeeperRegisterPlugin) Start() error {
kvPaire, err := p.KV.Get(nodePath) kvPaire, err := p.KV.Get(nodePath)
if err != nil { if err != nil {
log.Infof("can't get data of node: %s, because of %v", nodePath, err.Error()) log.Infof("can't get data of node: %s, because of %v", nodePath, err.Error())
p.metasLock.RLock()
meta := p.metas[name]
p.metasLock.RUnlock()
err = p.KV.Put(nodePath, []byte(meta), &store.WriteOptions{TTL: p.UpdateInterval * 2})
if err != nil {
log.Errorf("cannot re-create zookeeper path %s: %v", nodePath, err)
}
} else { } else {
v, _ := url.ParseQuery(string(kvPaire.Value)) v, _ := url.ParseQuery(string(kvPaire.Value))
v.Set("tps", string(data)) v.Set("tps", string(data))
@@ -134,12 +146,19 @@ func (p *ZooKeeperRegisterPlugin) Register(name string, rcvr interface{}, metada
} }
nodePath = fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress) nodePath = fmt.Sprintf("%s/%s/%s", p.BasePath, name, p.ServiceAddress)
err = p.KV.Put(nodePath, []byte(p.ServiceAddress), &store.WriteOptions{TTL: p.UpdateInterval * 2}) err = p.KV.Put(nodePath, []byte(metadata), &store.WriteOptions{TTL: p.UpdateInterval * 2})
if err != nil { if err != nil {
log.Errorf("cannot create zk path %s: %v", nodePath, err) log.Errorf("cannot create zk path %s: %v", nodePath, err)
return err return err
} }
p.Services = append(p.Services, name) p.Services = append(p.Services, name)
p.metasLock.Lock()
if p.metas == nil {
p.metas = make(map[string]string)
}
p.metas[name] = metadata
p.metasLock.Unlock()
return return
} }