hub.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. package tinymq
  2. import (
  3. "errors"
  4. "fmt"
  5. "log"
  6. "math/rand"
  7. "net"
  8. "regexp"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "time"
  13. "git.me9.top/git/tinymq/config"
  14. "git.me9.top/git/tinymq/conn"
  15. "git.me9.top/git/tinymq/conn/tpv2"
  16. "git.me9.top/git/tinymq/conn/wsv2"
  17. )
  18. // 类似一个插座的功能,管理多个连接
  19. // 一个hub即可以是客户端,同时也可以是服务端
  20. // 为了简化流程和让通讯更加迅速,不再重发和缓存结果,采用超时的方式告诉应用层。
  21. // 截取部分字符串
  22. func subStr(str string, length int) string {
  23. if len(str) <= length {
  24. return str
  25. }
  26. return str[0:length] + "..."
  27. }
  28. type Hub struct {
  29. sync.Mutex
  30. cf *config.Config
  31. globalID uint16
  32. channel string // 本地频道信息
  33. connects sync.Map // map[*Line]bool(true) //记录当前的连接,方便查找
  34. subscribes sync.Map // [cmd]->[]*SubscribeData //注册绑定频道的函数,用于响应请求
  35. msgCache sync.Map // map[uint16]*GetMsg //请求的回应记录,key为id
  36. // 客户端需要用的函数
  37. connectHostFunc ConnectHostFunc // 获取对应频道的一个连接地址
  38. authFunc AuthFunc // 获取认证信息,用于发送给对方
  39. // 服务端需要用的函数
  40. checkAuthFunc CheckAuthFunc // 核对认证是否合法
  41. // 连接状态变化时调用的函数
  42. connectStatusFunc ConnectStatusFunc
  43. // 上次清理异常连接时间戳
  44. lastCleanDeadConnect int64
  45. }
  46. // 清理异常连接
  47. func (h *Hub) cleanDeadConnect() {
  48. h.Lock()
  49. defer h.Unlock()
  50. now := time.Now().UnixMilli()
  51. if now-h.lastCleanDeadConnect > int64(h.cf.CleanDeadConnectWait) {
  52. h.lastCleanDeadConnect = now
  53. h.connects.Range(func(key, _ any) bool {
  54. line := key.(*Line)
  55. if line.state != Connected && now-line.updated.UnixMilli() > int64(h.cf.CleanDeadConnectWait) {
  56. h.connects.Delete(key)
  57. }
  58. return true
  59. })
  60. }
  61. }
  62. // 获取通讯消息ID号
  63. func (h *Hub) GetID() uint16 {
  64. h.Lock()
  65. defer h.Unlock()
  66. h.globalID++
  67. if h.globalID <= 0 || h.globalID >= config.ID_MAX {
  68. h.globalID = 1
  69. }
  70. for {
  71. // 检查是否在请求队列中存在对应的id
  72. if _, ok := h.msgCache.Load(h.globalID); ok {
  73. h.globalID++
  74. if h.globalID <= 0 || h.globalID >= config.ID_MAX {
  75. h.globalID = 1
  76. }
  77. } else {
  78. break
  79. }
  80. }
  81. return h.globalID
  82. }
  83. // 注册频道,其中频道为正则表达式字符串
  84. func (h *Hub) Subscribe(channel *regexp.Regexp, cmd string, backFunc SubscribeBack) (err error) {
  85. if channel == nil {
  86. return errors.New("channel can not be nil")
  87. }
  88. reg := &SubscribeData{
  89. Channel: channel,
  90. Cmd: cmd,
  91. BackFunc: backFunc,
  92. }
  93. sub, ok := h.subscribes.Load(cmd)
  94. if ok {
  95. h.subscribes.Store(cmd, append(sub.([]*SubscribeData), reg))
  96. return
  97. }
  98. regs := make([]*SubscribeData, 1)
  99. regs[0] = reg
  100. h.subscribes.Store(cmd, regs)
  101. return
  102. }
  103. // 获取当前在线的数量
  104. func (h *Hub) ConnectNum() int {
  105. var count int
  106. h.connects.Range(func(key, _ any) bool {
  107. if key.(*Line).state == Connected {
  108. count++
  109. }
  110. return true
  111. })
  112. return count
  113. }
  114. // 获取所有的在线连接频道
  115. func (h *Hub) AllChannel() []string {
  116. cs := make([]string, 0)
  117. h.connects.Range(func(key, _ any) bool {
  118. line := key.(*Line)
  119. if line.state == Connected {
  120. cs = append(cs, line.channel)
  121. }
  122. return true
  123. })
  124. return cs
  125. }
  126. // 获取所有连接频道和连接时长
  127. // 为了避免定义数据结构麻烦,采用|隔开
  128. func (h *Hub) AllChannelTime() []string {
  129. cs := make([]string, 0)
  130. h.connects.Range(func(key, value any) bool {
  131. line := key.(*Line)
  132. if line.state == Connected {
  133. ti := time.Since(value.(time.Time)).Milliseconds()
  134. cs = append(cs, line.channel+"|"+strconv.FormatInt(ti, 10))
  135. }
  136. return true
  137. })
  138. return cs
  139. }
  140. // 获取频道并通过函数过滤,如果返回 false 将终止
  141. func (h *Hub) ChannelToFunc(fn func(string) bool) {
  142. h.connects.Range(func(key, _ any) bool {
  143. line := key.(*Line)
  144. if line.state == Connected {
  145. return fn(line.channel)
  146. }
  147. return true
  148. })
  149. }
  150. // 从 channel 获取连接
  151. func (h *Hub) ChannelToLine(channel string) (line *Line) {
  152. h.connects.Range(func(key, _ any) bool {
  153. l := key.(*Line)
  154. if l.channel == channel {
  155. line = l
  156. return false
  157. }
  158. return true
  159. })
  160. return
  161. }
  162. // 返回请求结果
  163. func (h *Hub) outResponse(response *ResponseData) {
  164. defer recover() //避免管道已经关闭而引起panic
  165. id := response.Id
  166. t, ok := h.msgCache.Load(id)
  167. if ok {
  168. // 删除数据缓存
  169. h.msgCache.Delete(id)
  170. gm := t.(*GetMsg)
  171. // 停止定时器
  172. if !gm.timer.Stop() {
  173. select {
  174. case <-gm.timer.C:
  175. default:
  176. }
  177. }
  178. // 回应数据到上层
  179. gm.out <- response
  180. }
  181. }
  182. // 发送数据到网络接口
  183. // 返回发送的数量
  184. func (h *Hub) sendRequest(gd *GetData) (count int) {
  185. h.connects.Range(func(key, _ any) bool {
  186. conn := key.(*Line)
  187. // 检查连接是否OK
  188. if conn.state != Connected {
  189. return true
  190. }
  191. if gd.Channel.MatchString(conn.channel) {
  192. var id uint16
  193. if gd.backchan != nil {
  194. id = h.GetID()
  195. timeout := gd.Timeout
  196. if timeout <= 0 {
  197. timeout = h.cf.WriteWait
  198. }
  199. fn := func(id uint16, conn *Line) func() {
  200. return func() {
  201. go h.outResponse(&ResponseData{
  202. Id: id,
  203. State: config.GET_TIMEOUT,
  204. Data: []byte(config.GET_TIMEOUT_MSG),
  205. conn: conn,
  206. })
  207. // 检查是否已经很久时间没有使用连接了
  208. if time.Since(conn.lastRead) > time.Duration(h.cf.PingInterval*3)*time.Millisecond {
  209. // 超时关闭当前的连接
  210. log.Println("get message timeout", conn.channel)
  211. // 有可能连接出现问题,断开并重新连接
  212. conn.Close(false)
  213. return
  214. }
  215. }
  216. }(id, conn)
  217. // 将要发送的请求缓存
  218. gm := &GetMsg{
  219. out: gd.backchan,
  220. timer: time.AfterFunc(time.Millisecond*time.Duration(timeout), fn),
  221. }
  222. h.msgCache.Store(id, gm)
  223. }
  224. // 组织数据并发送到Connect
  225. conn.sendRequest <- &RequestData{
  226. Id: id,
  227. Cmd: gd.Cmd,
  228. Data: gd.Data,
  229. timeout: gd.Timeout,
  230. backchan: gd.backchan,
  231. conn: conn,
  232. }
  233. log.Println("[SEND]->", conn.channel, "["+gd.Cmd+"]", subStr(string(gd.Data), 200))
  234. count++
  235. if gd.Max > 0 && count >= gd.Max {
  236. return false
  237. }
  238. }
  239. return true
  240. })
  241. return
  242. }
  243. // 执行网络发送过来的命令
  244. func (h *Hub) requestFromNet(request *RequestData) {
  245. cmd := request.Cmd
  246. channel := request.conn.channel
  247. log.Println("[REQU]<-", channel, "["+cmd+"]", subStr(string(request.Data), 200))
  248. sub, ok := h.subscribes.Load(cmd)
  249. if ok {
  250. subs := sub.([]*SubscribeData)
  251. // 倒序查找是为了新增的频道响应函数优先执行
  252. for i := len(subs) - 1; i >= 0; i-- {
  253. rg := subs[i]
  254. if rg.Channel.MatchString(channel) {
  255. state, data := rg.BackFunc(request)
  256. // NEXT_SUBSCRIBE 表示当前的函数没有处理完成,还需要下个注册函数处理
  257. if state == config.NEXT_SUBSCRIBE {
  258. continue
  259. }
  260. // 如果id为0表示不需要回应
  261. if request.Id != 0 {
  262. request.conn.sendResponse <- &ResponseData{
  263. Id: request.Id,
  264. State: state,
  265. Data: data,
  266. }
  267. log.Println("[RESP]->", channel, "["+cmd+"]", state, subStr(string(data), 200))
  268. }
  269. return
  270. }
  271. }
  272. }
  273. log.Println("[not match command]", channel, cmd)
  274. // 返回没有匹配的消息
  275. request.conn.sendResponse <- &ResponseData{
  276. Id: request.Id,
  277. State: config.NO_MATCH,
  278. Data: []byte(config.NO_MATCH_MSG),
  279. }
  280. }
  281. // 请求频道并获取数据,采用回调的方式返回结果
  282. // 当前调用将会阻塞,直到命令都执行结束,最后返回执行的数量
  283. // 如果 backFunc 返回为 false 则提前结束
  284. // 最大数量和超时时间如果为0的话表示使用默认值
  285. func (h *Hub) GetX(channel *regexp.Regexp, cmd string, data []byte, backFunc GetBack, max int, timeout int) (count int) {
  286. // 排除空频道
  287. if channel == nil {
  288. return 0
  289. }
  290. if timeout <= 0 {
  291. timeout = h.cf.ReadWait
  292. }
  293. gd := &GetData{
  294. Channel: channel,
  295. Cmd: cmd,
  296. Data: data,
  297. Max: max,
  298. Timeout: timeout,
  299. backchan: make(chan *ResponseData, 32),
  300. }
  301. sendMax := h.sendRequest(gd)
  302. if sendMax <= 0 {
  303. return 0
  304. }
  305. // 避免出现异常时线程无法退出
  306. timer := time.NewTimer(time.Millisecond * time.Duration(gd.Timeout+h.cf.WriteWait*2))
  307. defer func() {
  308. if !timer.Stop() {
  309. select {
  310. case <-timer.C:
  311. default:
  312. }
  313. }
  314. close(gd.backchan)
  315. }()
  316. for {
  317. select {
  318. case rp := <-gd.backchan:
  319. if rp == nil || rp.conn == nil {
  320. // 可能是已经退出了
  321. return
  322. }
  323. ch := rp.conn.channel
  324. log.Println("[RECV]<-", ch, "["+gd.Cmd+"]", rp.State, subStr(string(rp.Data), 200))
  325. count++
  326. // 如果这里返回为false这跳出循环
  327. if backFunc != nil && !backFunc(rp) {
  328. return
  329. }
  330. if count >= sendMax {
  331. return
  332. }
  333. case <-timer.C:
  334. return
  335. }
  336. }
  337. // return
  338. }
  339. // 请求频道并获取数据,采用回调的方式返回结果
  340. // 当前调用将会阻塞,直到命令都执行结束,最后返回执行的数量
  341. // 如果 backFunc 返回为 false 则提前结束
  342. func (h *Hub) Get(channel *regexp.Regexp, cmd string, data []byte, backFunc GetBack) (count int) {
  343. return h.GetX(channel, cmd, data, backFunc, 0, 0)
  344. }
  345. // 只获取一个频道的数据,阻塞等待到默认超时间隔
  346. // 如果没有结果将返回 NO_MATCH
  347. func (h *Hub) GetOne(channel *regexp.Regexp, cmd string, data []byte) (response *ResponseData) {
  348. h.GetX(channel, cmd, data, func(rp *ResponseData) (ok bool) {
  349. response = rp
  350. return false
  351. }, 1, 0)
  352. if response == nil {
  353. response = &ResponseData{
  354. State: config.NO_MATCH,
  355. Data: []byte(config.NO_MATCH_MSG),
  356. }
  357. }
  358. return
  359. }
  360. // 只获取一个频道的数据,阻塞等待到指定超时间隔
  361. // 如果没有结果将返回 NO_MATCH
  362. func (h *Hub) GetOneX(channel *regexp.Regexp, cmd string, data []byte, timeout int) (response *ResponseData) {
  363. h.GetX(channel, cmd, data, func(rp *ResponseData) (ok bool) {
  364. response = rp
  365. return false
  366. }, 1, timeout)
  367. if response == nil {
  368. response = &ResponseData{
  369. State: config.NO_MATCH,
  370. Data: []byte(config.NO_MATCH_MSG),
  371. }
  372. }
  373. return
  374. }
  375. // 推送消息出去,不需要返回数据
  376. func (h *Hub) Push(channel *regexp.Regexp, cmd string, data []byte) {
  377. // 排除空频道
  378. if channel == nil {
  379. return
  380. }
  381. gd := &GetData{
  382. Channel: channel,
  383. Cmd: cmd,
  384. Data: data,
  385. Timeout: h.cf.ReadWait,
  386. backchan: nil,
  387. }
  388. h.sendRequest(gd)
  389. }
  390. // 推送最大对应数量的消息出去,不需要返回数据
  391. func (h *Hub) PushX(channel *regexp.Regexp, cmd string, data []byte, max int) {
  392. // 排除空频道
  393. if channel == nil {
  394. return
  395. }
  396. gd := &GetData{
  397. Channel: channel,
  398. Cmd: cmd,
  399. Data: data,
  400. Max: max,
  401. Timeout: h.cf.ReadWait,
  402. backchan: nil,
  403. }
  404. h.sendRequest(gd)
  405. }
  406. // 增加连接
  407. func (h *Hub) addLine(line *Line) {
  408. if _, ok := h.connects.Load(line); ok {
  409. log.Println("connect have exist")
  410. // 连接已经存在,直接返回
  411. return
  412. }
  413. // 检查是否有相同的channel,如果有的话将其关闭删除
  414. channel := line.channel
  415. h.connects.Range(func(key, _ any) bool {
  416. conn := key.(*Line)
  417. // 删除超时的连接
  418. if conn.state != Connected && conn.host == nil && time.Since(conn.lastRead) > time.Duration(h.cf.PingInterval*5)*time.Millisecond {
  419. h.connects.Delete(key)
  420. return true
  421. }
  422. if conn.channel == channel {
  423. conn.Close(true)
  424. h.connects.Delete(key)
  425. return false
  426. }
  427. return true
  428. })
  429. h.connects.Store(line, true)
  430. }
  431. // 删除连接
  432. func (h *Hub) removeLine(conn *Line) {
  433. conn.Close(true)
  434. h.connects.Delete(conn)
  435. }
  436. // 获取指定连接的连接持续时间
  437. func (h *Hub) ConnectDuration(conn *Line) time.Duration {
  438. t, ok := h.connects.Load(conn)
  439. if ok {
  440. return time.Since(t.(time.Time))
  441. }
  442. // 如果不存在直接返回0
  443. return time.Duration(0)
  444. }
  445. // 绑定端口,建立服务
  446. // 需要程序运行时调用
  447. func (h *Hub) BindForServer(info *HostInfo) (err error) {
  448. doConnectFunc := func(conn conn.Connect) {
  449. proto, version, channel, auth, err := conn.ReadAuthInfo()
  450. if err != nil {
  451. log.Println("[BindForServer ReadAuthInfo ERROR]", err)
  452. conn.Close()
  453. return
  454. }
  455. if version != info.Version || proto != info.Proto {
  456. log.Println("wrong version or protocol: ", version, proto)
  457. conn.Close()
  458. return
  459. }
  460. // 检查验证是否合法
  461. if !h.checkAuthFunc(proto, version, channel, auth) {
  462. conn.Close()
  463. return
  464. }
  465. // 发送频道信息
  466. if err := conn.WriteAuthInfo(h.channel, h.authFunc(proto, version, channel, auth)); err != nil {
  467. log.Println("[WriteAuthInfo ERROR]", err)
  468. conn.Close()
  469. return
  470. }
  471. // 将连接加入现有连接中
  472. done := false
  473. h.connects.Range(func(key, _ any) bool {
  474. line := key.(*Line)
  475. if line.state == Disconnected && line.channel == channel && line.host == nil {
  476. line.Start(conn, nil)
  477. done = true
  478. return false
  479. }
  480. return true
  481. })
  482. // 新建一个连接
  483. if !done {
  484. line := NewConnect(h.cf, h, channel, conn, nil)
  485. h.addLine(line)
  486. }
  487. }
  488. if info.Version == wsv2.VERSION && info.Proto == wsv2.PROTO {
  489. bind := ""
  490. if info.Bind != "" {
  491. bind = net.JoinHostPort(info.Bind, strconv.Itoa(int(info.Port)))
  492. }
  493. return wsv2.Server(h.cf, bind, info.Path, info.Hash, doConnectFunc)
  494. } else if info.Version == tpv2.VERSION && info.Proto == tpv2.PROTO {
  495. return tpv2.Server(h.cf, net.JoinHostPort(info.Bind, strconv.Itoa(int(info.Port))), info.Hash, doConnectFunc)
  496. }
  497. return errors.New("not connect protocol and version found")
  498. }
  499. // 新建一个连接,不同的连接协议由底层自己选择
  500. // channel: 要连接的频道信息,需要能表达频道关键信息的部分
  501. func (h *Hub) ConnectToServer(channel string, force bool) (err error) {
  502. // 检查当前channel是否已经存在
  503. if !force {
  504. line := h.ChannelToLine(channel)
  505. if line != nil && line.state == Connected {
  506. err = fmt.Errorf("[ConnectToServer ERROR] existed channel: %s", channel)
  507. return
  508. }
  509. }
  510. // 获取服务地址等信息
  511. host, err := h.connectHostFunc(channel)
  512. if err != nil {
  513. return err
  514. }
  515. var conn conn.Connect
  516. addr := net.JoinHostPort(host.Host, strconv.Itoa(int(host.Port)))
  517. if host.Version == wsv2.VERSION && host.Proto == wsv2.PROTO {
  518. conn, err = wsv2.Client(h.cf, addr, host.Path, host.Hash)
  519. } else if host.Version == tpv2.VERSION && host.Proto == tpv2.PROTO {
  520. conn, err = tpv2.Client(h.cf, addr, host.Hash)
  521. } else {
  522. return fmt.Errorf("not correct protocol and version found in: %+v", host)
  523. }
  524. if err != nil {
  525. log.Println("[Client ERROR]", host.Proto, err)
  526. host.Errors++
  527. host.Updated = time.Now()
  528. return err
  529. }
  530. // 发送验证信息
  531. if err := conn.WriteAuthInfo(h.channel, h.authFunc(host.Proto, host.Version, channel, nil)); err != nil {
  532. log.Println("[WriteAuthInfo ERROR]", err)
  533. conn.Close()
  534. host.Errors++
  535. host.Updated = time.Now()
  536. return err
  537. }
  538. // 接收频道信息
  539. proto, version, channel2, auth, err := conn.ReadAuthInfo()
  540. if err != nil {
  541. log.Println("[ConnectToServer ReadAuthInfo ERROR]", err)
  542. conn.Close()
  543. host.Errors++
  544. host.Updated = time.Now()
  545. return err
  546. }
  547. // 检查版本和协议是否一致
  548. if version != host.Version || proto != host.Proto {
  549. err = fmt.Errorf("[version or protocol wrong ERROR] %d, %s", version, proto)
  550. log.Println(err)
  551. conn.Close()
  552. host.Errors++
  553. host.Updated = time.Now()
  554. return err
  555. }
  556. // 检查频道名称是否匹配
  557. if !strings.Contains(channel2, channel) {
  558. err = fmt.Errorf("[channel ERROR] want %s, get %s", channel, channel2)
  559. log.Println(err)
  560. conn.Close()
  561. host.Errors++
  562. host.Updated = time.Now()
  563. return err
  564. }
  565. // 检查验证是否合法
  566. if !h.checkAuthFunc(proto, version, channel, auth) {
  567. err = fmt.Errorf("[checkAuthFunc ERROR] in proto: %s, version: %d, channel: %s, auth: %s", proto, version, channel, string(auth))
  568. log.Println(err)
  569. conn.Close()
  570. host.Errors++
  571. host.Updated = time.Now()
  572. return err
  573. }
  574. // 更新服务主机信息
  575. host.Errors = 0
  576. host.Updated = time.Now()
  577. // 将连接加入现有连接中
  578. done := false
  579. h.connects.Range(func(key, _ any) bool {
  580. line := key.(*Line)
  581. if line.channel == channel {
  582. if line.state == Connected {
  583. if !force {
  584. err = fmt.Errorf("[connectToServer ERROR] channel already connected: %s", channel)
  585. log.Println(err)
  586. return false
  587. }
  588. return true
  589. }
  590. line.Start(conn, host)
  591. done = true
  592. return false
  593. }
  594. return true
  595. })
  596. if err != nil {
  597. return err
  598. }
  599. // 新建一个连接
  600. if !done {
  601. line := NewConnect(h.cf, h, channel, conn, host)
  602. h.addLine(line)
  603. }
  604. return nil
  605. }
  606. // 重试方式连接服务
  607. // 将会一直阻塞直到连接成功
  608. func (h *Hub) ConnectToServerX(channel string, force bool) {
  609. for {
  610. err := h.ConnectToServer(channel, force)
  611. if err == nil {
  612. return
  613. }
  614. log.Println("[ConnectToServer ERROR, try it again]", err)
  615. // 产生一个随机数避免刹间重连过载
  616. r := rand.New(rand.NewSource(time.Now().UnixNano()))
  617. time.Sleep(time.Duration(r.Intn(h.cf.ConnectTimeout)+(h.cf.ConnectTimeout/2)) * time.Millisecond)
  618. }
  619. }
  620. // 建立一个集线器
  621. // connectFunc 用于监听连接状态的函数,可以为nil
  622. func NewHub(
  623. cf *config.Config,
  624. channel string,
  625. // 客户端需要用的函数
  626. connectHostFunc ConnectHostFunc,
  627. authFunc AuthFunc,
  628. // 服务端需要用的函数
  629. checkAuthFunc CheckAuthFunc,
  630. // 连接状态变化时调用的函数
  631. connectStatusFunc ConnectStatusFunc,
  632. ) (h *Hub) {
  633. h = &Hub{
  634. cf: cf,
  635. channel: channel,
  636. connectHostFunc: connectHostFunc,
  637. authFunc: authFunc,
  638. checkAuthFunc: checkAuthFunc,
  639. connectStatusFunc: connectStatusFunc,
  640. lastCleanDeadConnect: time.Now().UnixMilli(),
  641. }
  642. return h
  643. }