fix ipv6 addr parsing and using

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
2019-07-17 10:38:50 +03:00
parent d4fefc4b76
commit e688ab0a45
8 changed files with 104 additions and 68 deletions

View File

@@ -13,8 +13,6 @@ import (
"net/http"
"net/url"
"runtime"
"strconv"
"strings"
"sync"
"time"
@@ -614,18 +612,27 @@ func (h *httpBroker) Publish(topic string, msg *Message, opts ...PublishOption)
}
func (h *httpBroker) Subscribe(topic string, handler Handler, opts ...SubscribeOption) (Subscriber, error) {
var err error
var host, port string
options := NewSubscribeOptions(opts...)
// parse address for host, port
parts := strings.Split(h.Address(), ":")
host := strings.Join(parts[:len(parts)-1], ":")
port, _ := strconv.Atoi(parts[len(parts)-1])
host, port, err = net.SplitHostPort(h.Address())
if err != nil {
return nil, err
}
addr, err := maddr.Extract(host)
if err != nil {
return nil, err
}
// ipv6 addr
if addr == "::" {
// ipv6 addr
addr = fmt.Sprintf("[%s]", addr)
}
// create unique id
id := h.id + "." + uuid.New().String()
@@ -638,7 +645,7 @@ func (h *httpBroker) Subscribe(topic string, handler Handler, opts ...SubscribeO
// register service
node := &registry.Node{
Id: id,
Address: fmt.Sprintf("%s:%d", addr, port),
Address: fmt.Sprintf("%s:%s", addr, port),
Metadata: map[string]string{
"secure": fmt.Sprintf("%t", secure),
},