improvements at NewConn - now w opts, more flexible

master
Paulo Simão 2021-11-17 06:14:14 -03:00
parent 33db6dfaac
commit 6bd5a3a5ac
1 changed files with 60 additions and 54 deletions

View File

@ -22,8 +22,6 @@ func dbg(s string, p ...interface{}) {
type MQConn struct { type MQConn struct {
opts *MQConnOpts opts *MQConnOpts
retry bool retry bool
chRetry chan *struct{}
//chReady chan *struct{}
subs sync.Map subs sync.Map
mtx sync.Mutex mtx sync.Mutex
@ -189,8 +187,7 @@ func (c *MQConn) Loop() {
} }
func (c *MQConn) Init() error { func (c *MQConn) Init() error {
c.retry = true if c.retry {
doConn := func() error { doConn := func() error {
d := websocket.Dialer{ d := websocket.Dialer{
NetDial: nil, NetDial: nil,
@ -244,9 +241,14 @@ func (c *MQConn) Init() error {
} }
return true return true
}) })
}
return nil return nil
} }
func (c *MQConn) Close() error {
c.retry = false
return c.conn.Close()
}
type MQConnOpts struct { type MQConnOpts struct {
Url string Url string
@ -257,8 +259,12 @@ type MQConnOpts struct {
func New(o *MQConnOpts) (*MQConn, error) { func New(o *MQConnOpts) (*MQConn, error) {
ret := &MQConn{ ret := &MQConn{
opts: o, opts: o,
retry: true,
} }
err := ret.Init() err := ret.Init()
if err != nil {
return nil, err
}
go func() { go func() {
for ret.retry { for ret.retry {
ret.Loop() ret.Loop()