Fix encoding so we split across txt records

This commit is contained in:
Asim 2016-04-28 18:36:59 +01:00 committed by Vasiliy Tolstov
parent 0b029da08c
commit 4bd95bfe91
3 changed files with 113 additions and 232 deletions

View File

@ -6,119 +6,68 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"strings"
"github.com/micro/go-micro/registry"
) )
func encode(buf []byte) string { func encode(txt *mdnsTxt) ([]string, error) {
var b bytes.Buffer b, err := json.Marshal(txt)
defer b.Reset() if err != nil {
return nil, err
}
w := zlib.NewWriter(&b) var buf bytes.Buffer
if _, err := w.Write(buf); err != nil { defer buf.Reset()
return ""
w := zlib.NewWriter(&buf)
if _, err := w.Write(b); err != nil {
return nil, err
} }
w.Close() w.Close()
return hex.EncodeToString(b.Bytes()) encoded := hex.EncodeToString(buf.Bytes())
// individual txt limit
if len(encoded) <= 255 {
return []string{encoded}, nil
}
// split encoded string
var record []string
for len(encoded) > 255 {
record = append(record, encoded[:255])
encoded = encoded[255:]
}
record = append(record, encoded)
return record, nil
} }
func decode(d string) []byte { func decode(record []string) (*mdnsTxt, error) {
hr, err := hex.DecodeString(d) encoded := strings.Join(record, "")
hr, err := hex.DecodeString(encoded)
if err != nil { if err != nil {
return nil return nil, err
} }
br := bytes.NewReader(hr) br := bytes.NewReader(hr)
zr, err := zlib.NewReader(br) zr, err := zlib.NewReader(br)
if err != nil { if err != nil {
return nil return nil, err
} }
rbuf, err := ioutil.ReadAll(zr) rbuf, err := ioutil.ReadAll(zr)
if err != nil { if err != nil {
return nil return nil, err
} }
return rbuf var txt *mdnsTxt
}
if err := json.Unmarshal(rbuf, &txt); err != nil {
func encodeEndpoints(en []*registry.Endpoint) []string { return nil, err
var tags []string }
for _, e := range en {
if b, err := json.Marshal(e); err == nil { return txt, nil
tags = append(tags, "e-"+encode(b))
}
}
return tags
}
func decodeEndpoints(tags []string) []*registry.Endpoint {
var en []*registry.Endpoint
for _, tag := range tags {
if len(tag) == 0 || tag[0] != 'e' || tag[1] != '-' {
continue
}
buf := decode(tag[2:])
var e *registry.Endpoint
if err := json.Unmarshal(buf, &e); err == nil {
en = append(en, e)
}
}
return en
}
func encodeMetadata(md map[string]string) []string {
var tags []string
for k, v := range md {
if b, err := json.Marshal(map[string]string{
k: v,
}); err == nil {
// new encoding
tags = append(tags, "t-"+encode(b))
}
}
return tags
}
func decodeMetadata(tags []string) map[string]string {
md := make(map[string]string)
for _, tag := range tags {
if len(tag) == 0 || tag[0] != 't' || tag[1] != '-' {
continue
}
buf := decode(tag[2:])
var kv map[string]string
// Now unmarshal
if err := json.Unmarshal(buf, &kv); err == nil {
for k, v := range kv {
md[k] = v
}
}
}
return md
}
func encodeVersion(v string) []string {
return []string{
// new encoding,
"v-" + encode([]byte(v)),
}
}
func decodeVersion(tags []string) (string, bool) {
for _, tag := range tags {
if len(tag) < 2 || tag[0] != 'v' || tag[1] != '-' {
continue
}
return string(decode(tag[2:])), true
}
return "", false
} }

View File

@ -1,14 +1,19 @@
package mdns package mdns
import ( import (
"encoding/json"
"testing" "testing"
"github.com/micro/go-micro/registry" "github.com/micro/go-micro/registry"
) )
func TestEncodingEndpoints(t *testing.T) { func TestEncoding(t *testing.T) {
eps := []*registry.Endpoint{ testData := []*mdnsTxt{
&mdnsTxt{
Version: "1.0.0",
Metadata: map[string]string{
"foo": "bar",
},
Endpoints: []*registry.Endpoint{
&registry.Endpoint{ &registry.Endpoint{
Name: "endpoint1", Name: "endpoint1",
Request: &registry.Value{ Request: &registry.Value{
@ -23,125 +28,40 @@ func TestEncodingEndpoints(t *testing.T) {
"foo1": "bar1", "foo1": "bar1",
}, },
}, },
&registry.Endpoint{
Name: "endpoint2",
Request: &registry.Value{
Name: "request",
Type: "request",
},
Response: &registry.Value{
Name: "response",
Type: "response",
},
Metadata: map[string]string{
"foo2": "bar2",
},
},
&registry.Endpoint{
Name: "endpoint3",
Request: &registry.Value{
Name: "request",
Type: "request",
},
Response: &registry.Value{
Name: "response",
Type: "response",
},
Metadata: map[string]string{
"foo3": "bar3",
}, },
}, },
} }
testEp := func(ep *registry.Endpoint, enc string) { for _, d := range testData {
// encode endpoint encoded, err := encode(d)
e := encodeEndpoints([]*registry.Endpoint{ep})
// check there are two tags; old and new
if len(e) != 1 {
t.Fatalf("Expected 1 encoded tag, got %v", e)
}
// check old encoding
var seen bool
for _, en := range e {
if en == enc {
seen = true
break
}
}
if !seen {
t.Fatalf("Expected %s but not found", enc)
}
// decode
d := decodeEndpoints([]string{enc})
if len(d) == 0 {
t.Fatalf("Expected %v got %v", ep, d)
}
// check name
if d[0].Name != ep.Name {
t.Fatalf("Expected ep %s got %s", ep.Name, d[0].Name)
}
// check all the metadata exists
for k, v := range ep.Metadata {
if gv := d[0].Metadata[k]; gv != v {
t.Fatalf("Expected key %s val %s got val %s", k, v, gv)
}
}
}
for _, ep := range eps {
// JSON encoded
jencoded, err := json.Marshal(ep)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
// HEX encoded for _, txt := range encoded {
hencoded := encode(jencoded) if len(txt) > 255 {
// endpoint tag t.Fatalf("One of parts for txt is %d characters", len(txt))
hepTag := "e-" + hencoded
testEp(ep, hepTag)
}
}
func TestEncodingVersion(t *testing.T) {
testData := []struct {
decoded string
encoded string
}{
{"1.0.0", "v-789c32d433d03300040000ffff02ce00ee"},
{"latest", "v-789cca492c492d2e01040000ffff08cc028e"},
}
for _, data := range testData {
e := encodeVersion(data.decoded)
if e[0] != data.encoded {
t.Fatalf("Expected %s got %s", data.encoded, e)
}
d, ok := decodeVersion(e)
if !ok {
t.Fatalf("Unexpected %t for %s", ok, data.encoded)
}
if d != data.decoded {
t.Fatalf("Expected %s got %s", data.decoded, d)
}
d, ok = decodeVersion([]string{data.encoded})
if !ok {
t.Fatalf("Unexpected %t for %s", ok, data.encoded)
}
if d != data.decoded {
t.Fatalf("Expected %s got %s", data.decoded, d)
}
} }
}
decoded, err := decode(encoded)
if err != nil {
t.Fatal(err)
}
if decoded.Version != d.Version {
t.Fatalf("Expected version %s got %s", d.Version, decoded.Version)
}
if len(decoded.Endpoints) != len(d.Endpoints) {
t.Fatalf("Expected %d endpoints, got %d", len(d.Endpoints), len(decoded.Endpoints))
}
for k, v := range d.Metadata {
if val := decoded.Metadata[k]; val != v {
t.Fatalf("Expected %s=%s got %s=%s", k, v, k, val)
}
}
}
} }

34
mdns.go
View File

@ -18,6 +18,12 @@ import (
hash "github.com/mitchellh/hashstructure" hash "github.com/mitchellh/hashstructure"
) )
type mdnsTxt struct {
Version string
Endpoints []*registry.Endpoint
Metadata map[string]string
}
type mdnsEntry struct { type mdnsEntry struct {
hash uint64 hash uint64
id string id string
@ -103,10 +109,16 @@ func (m *mdnsRegistry) Register(service *registry.Service, opts ...registry.Regi
e = &mdnsEntry{hash: h} e = &mdnsEntry{hash: h}
} }
var txt []string txt, err := encode(&mdnsTxt{
txt = append(txt, encodeVersion(service.Version)...) Version: service.Version,
txt = append(txt, encodeMetadata(node.Metadata)...) Endpoints: service.Endpoints,
// txt = append(txt, encodeEndpoints(service.Endpoints)...) Metadata: node.Metadata,
})
if err != nil {
gerr = err
continue
}
// we got here, new node // we got here, new node
s, err := mdns.NewMDNSService( s, err := mdns.NewMDNSService(
@ -195,17 +207,17 @@ func (m *mdnsRegistry) GetService(service string) ([]*registry.Service, error) {
continue continue
} }
version, exists := decodeVersion(e.InfoFields) txt, err := decode(e.InfoFields)
if !exists { if err != nil {
continue continue
} }
s, ok := serviceMap[version] s, ok := serviceMap[txt.Version]
if !ok { if !ok {
s = &registry.Service{ s = &registry.Service{
Name: service, Name: service,
Version: version, Version: txt.Version,
// Endpoints: decodeEndpoints(e.InfoFields), Endpoints: txt.Endpoints,
} }
} }
@ -213,10 +225,10 @@ func (m *mdnsRegistry) GetService(service string) ([]*registry.Service, error) {
Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."), Id: strings.TrimSuffix(e.Name, "."+p.Service+"."+p.Domain+"."),
Address: e.AddrV4.String(), Address: e.AddrV4.String(),
Port: e.Port, Port: e.Port,
Metadata: decodeMetadata(e.InfoFields), Metadata: txt.Metadata,
}) })
serviceMap[version] = s serviceMap[txt.Version] = s
case <-exit: case <-exit:
return return
} }