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 package smqcli
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -8,6 +9,7 @@ import (
"go.digitalcircle.com.br/open/simplemq/lib/random" "go.digitalcircle.com.br/open/simplemq/lib/random"
"go.digitalcircle.com.br/open/simplemq/lib/types" "go.digitalcircle.com.br/open/simplemq/lib/types"
"log" "log"
"net/http"
"regexp" "regexp"
"sync" "sync"
"time" "time"
@ -18,7 +20,7 @@ func dbg(s string, p ...interface{}) {
} }
type MQConn struct { type MQConn struct {
url string opts *MQConnOpts
retry bool retry bool
chRetry chan *struct{} chRetry chan *struct{}
//chReady chan *struct{} //chReady chan *struct{}
@ -104,7 +106,7 @@ func (c *MQConn) Rpc(s string, pli []byte) ([]byte, error) {
err := c.Write(m) err := c.Write(m)
select { select {
case <-lchan: case <-lchan:
case <-time.After(time.Second * 15): case <-time.After(time.Second * time.Duration(c.opts.Timeout)):
} }
close(lchan) close(lchan)
lchan = nil lchan = nil
@ -190,7 +192,28 @@ func (c *MQConn) Init() error {
c.retry = true c.retry = true
doConn := func() error { 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 { if err != nil {
return err return err
} }
@ -225,9 +248,15 @@ func (c *MQConn) Init() error {
return nil 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{ ret := &MQConn{
url: u, opts: o,
} }
err := ret.Init() err := ret.Init()
go func() { go func() {