Skip to content
Permalink
Browse files

Additional memory testing and fixups:

* DefaultTTL removed
* When TTL == 0, it is automatically removed from expiry conditions
* Additional improvements to new tests

Signed-off-by: Erik Hollensbe <github@hollensbe.org>
  • Loading branch information
erikh committed Nov 16, 2019
1 parent 4951903 commit 688442713a7fa7af3189754273363c84ebf75042
Showing with 10 additions and 14 deletions.
  1. +2 −0 go.sum
  2. +3 −9 registry/memory/memory.go
  3. +2 −2 registry/memory/memory_test.go
  4. +1 −1 registry/memory/options.go
  5. +2 −2 registry/memory/util.go
2 go.sum
@@ -260,6 +260,7 @@ github.com/micro/protoc-gen-micro v1.0.0/go.mod h1:C8ij4DJhapBmypcT00AXdb0cZ675/
github.com/miekg/dns v1.1.3/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.15 h1:CSSIDtllwGLMoA6zjdKnaE6Tx6eVUxQ29LUgGetiDCI=
github.com/miekg/dns v1.1.15/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.22 h1:Jm64b3bO9kP43ddLjL2EY3Io6bmy1qGb9Xxz6TqS6rc=
github.com/miekg/dns v1.1.22/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
@@ -481,6 +482,7 @@ golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3 h1:7TYNF4UdlohbFwpNH04CoPMp1
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190922100055-0a153f010e69/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190924154521-2837fb4f24fe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191110163157-d32e6e3b99c4 h1:Hynbrlo6LbYI3H1IqXpkVDOcX/3HiPdhVEuyj5a59RM=
golang.org/x/sys v0.0.0-20191110163157-d32e6e3b99c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.1-0.20180807135948-17ff2d5776d2/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
@@ -13,8 +13,7 @@ import (

var (
sendEventTime = 10 * time.Millisecond
ttlPruneTime = 1 * time.Minute
DefaultTTL = 1 * time.Minute
ttlPruneTime = time.Second
)

type node struct {
@@ -75,7 +74,7 @@ func (m *Registry) ttlPrune() {
for name, records := range m.records {
for version, record := range records {
for id, n := range record.Nodes {
if time.Since(n.LastSeen) > n.TTL {
if n.TTL != 0 && time.Since(n.LastSeen) > n.TTL {
log.Debugf("Registry TTL expired for node %s of service %s", n.Id, name)
delete(m.records[name][version].Nodes, id)
}
@@ -152,12 +151,7 @@ func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption
o(&options)
}

// if no TTL has been set, set it to DefaultTTL
if options.TTL.Seconds() == 0.0 {
options.TTL = DefaultTTL
}

r := serviceToRecord(s)
r := serviceToRecord(s, options.TTL)

if _, ok := m.records[s.Name]; !ok {
m.records[s.Name] = make(map[string]*record)
@@ -176,7 +176,7 @@ func TestMemoryRegistryTTL(t *testing.T) {
}
}

time.Sleep(time.Millisecond)
time.Sleep(ttlPruneTime * 2)

for name := range testData {
svcs, err := m.GetService(name)
@@ -194,7 +194,7 @@ func TestMemoryRegistryTTL(t *testing.T) {

func TestMemoryRegistryTTLConcurrent(t *testing.T) {
concurrency := 1000
waitTime := time.Second
waitTime := ttlPruneTime * 2
m := NewRegistry()

for _, v := range testData {
@@ -22,7 +22,7 @@ func getServiceRecords(ctx context.Context) map[string]map[string]*record {
}
// go through every version of the service
for _, s := range svc {
services[s.Name][s.Version] = serviceToRecord(s)
services[s.Name][s.Version] = serviceToRecord(s, 0)
}
}

@@ -6,7 +6,7 @@ import (
"github.com/micro/go-micro/registry"
)

func serviceToRecord(s *registry.Service) *record {
func serviceToRecord(s *registry.Service, ttl time.Duration) *record {
metadata := make(map[string]string)
for k, v := range s.Metadata {
metadata[k] = v
@@ -16,7 +16,7 @@ func serviceToRecord(s *registry.Service) *record {
for _, n := range s.Nodes {
nodes[n.Id] = &node{
Node: n,
TTL: DefaultTTL,
TTL: ttl,
LastSeen: time.Now(),
}
}

0 comments on commit 6884427

Please sign in to comment.
You can’t perform that action at this time.