improvements at NewConn - now w opts, more flexible

master
Paulo Simão 2021-11-17 05:19:31 -03:00
parent ea89184a6f
commit 33db6dfaac
1 changed files with 34 additions and 5 deletions

View File

@ -1,6 +1,7 @@
package smqcli
import (
"crypto/tls"
"encoding/json"
"errors"
"fmt"
@ -8,6 +9,7 @@ import (
"go.digitalcircle.com.br/open/simplemq/lib/random"
"go.digitalcircle.com.br/open/simplemq/lib/types"
"log"
"net/http"
"regexp"
"sync"
"time"
@ -18,7 +20,7 @@ func dbg(s string, p ...interface{}) {
}
type MQConn struct {
url string
opts *MQConnOpts
retry bool
chRetry chan *struct{}
//chReady chan *struct{}
@ -104,7 +106,7 @@ func (c *MQConn) Rpc(s string, pli []byte) ([]byte, error) {
err := c.Write(m)
select {
case <-lchan:
case <-time.After(time.Second * 15):
case <-time.After(time.Second * time.Duration(c.opts.Timeout)):
}
close(lchan)
lchan = nil
@ -190,7 +192,28 @@ func (c *MQConn) Init() error {
c.retry = true
doConn := func() error {
con, _, err := websocket.DefaultDialer.Dial(c.url, nil)
d := websocket.Dialer{
NetDial: nil,
NetDialContext: nil,
Proxy: nil,
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
HandshakeTimeout: 0,
ReadBufferSize: 0,
WriteBufferSize: 0,
WriteBufferPool: nil,
Subprotocols: nil,
EnableCompression: false,
Jar: nil,
}
h := http.Header{}
if c.opts.Headers != nil {
for k, v := range c.opts.Headers {
h.Set(k, v)
}
}
con, _, err := d.Dial(c.opts.Url, h)
if err != nil {
return err
}
@ -225,9 +248,15 @@ func (c *MQConn) Init() error {
return nil
}
func New(u string) (*MQConn, error) {
type MQConnOpts struct {
Url string
Headers map[string]string
Timeout int
}
func New(o *MQConnOpts) (*MQConn, error) {
ret := &MQConn{
url: u,
opts: o,
}
err := ret.Init()
go func() {