From: qydysky <32743305+qydysky@users.noreply.github.com> Date: Wed, 1 Mar 2023 19:01:01 +0000 (+0800) Subject: Improve 连接池 X-Git-Tag: v0.7.0~8 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=9b1f46d475678969774038473d7bf23fb006eaef;p=bili_danmu%2F.git Improve 连接池 --- diff --git a/CV/Var.go b/CV/Var.go index e6c8198..0745fcb 100644 --- a/CV/Var.go +++ b/CV/Var.go @@ -16,9 +16,9 @@ import ( "github.com/dustin/go-humanize" file "github.com/qydysky/part/file" - idpool "github.com/qydysky/part/idpool" log "github.com/qydysky/part/log" mq "github.com/qydysky/part/msgq" + pool "github.com/qydysky/part/pool" reqf "github.com/qydysky/part/reqf" syncmap "github.com/qydysky/part/sync" web "github.com/qydysky/part/web" @@ -59,7 +59,7 @@ type Common struct { K_v syncmap.Map //配置文件 Log *log.Log_interface //日志 Danmu_Main_mq *mq.Msgq //消息 - ReqPool *idpool.Idpool //请求池 + ReqPool *pool.Buf[reqf.Req] //请求池 SerF *web.WebPath //web服务处理 } @@ -180,9 +180,20 @@ func (t *Common) Init() Common { t.Danmu_Main_mq = mq.New() - t.ReqPool = idpool.New(func() interface{} { - return reqf.New() - }) + t.ReqPool = pool.New( + func() *reqf.Req { + return reqf.New() + }, + func(t *reqf.Req) bool { + return t.IsLive() + }, + func(t *reqf.Req) *reqf.Req { + return t + }, + func(t *reqf.Req) *reqf.Req { + return t + }, 100, + ) var ( ckv = flag.String("ckv", "", "自定义配置KV文件,将会覆盖config_K_v配置") @@ -305,8 +316,7 @@ func (t *Common) loadConf(customConf string) error { if strings.Contains(customConf, "http:") || strings.Contains(customConf, "https:") { //从网址读取 req := t.ReqPool.Get() - r := req.Item.(*reqf.Req) - if e := r.Reqf(reqf.Rval{ + if e := req.Reqf(reqf.Rval{ Url: customConf, Header: map[string]string{ `User-Agent`: `Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:103.0) Gecko/20100101 Firefox/103.0`, @@ -321,13 +331,13 @@ func (t *Common) loadConf(customConf string) error { }); e != nil { return errors.New("无法获取自定义配置文件 " + e.Error()) } - if r.Response == nil { + if req.Response == nil { return errors.New("无法获取自定义配置文件 响应为空") - } else if r.Response.StatusCode&200 != 200 { - return fmt.Errorf("无法获取自定义配置文件 %d", r.Response.StatusCode) + } else if req.Response.StatusCode&200 != 200 { + return fmt.Errorf("无法获取自定义配置文件 %d", req.Response.StatusCode) } else { var tmp map[string]interface{} - json.Unmarshal(r.Respon, &tmp) + json.Unmarshal(req.Respon, &tmp) for k, v := range tmp { data[k] = v } diff --git a/F/api.go b/F/api.go index c8c5b69..cf6fcb4 100644 --- a/F/api.go +++ b/F/api.go @@ -28,7 +28,7 @@ import ( ) var apilog = c.C.Log.Base(`api`) -var api_limit = limit.New(1, 2000, 30000) //频率限制1次/2s,最大等待时间30s +var api_limit = limit.New(2, 1000, 30000) //频率限制2次/s,最大等待时间30s type GetFunc struct { *c.Common @@ -473,9 +473,8 @@ func (c *GetFunc) getInfoByRoom() (missKey []string) { Roomid := strconv.Itoa(c.Roomid) { //使用其他api - reqi := c.Common.ReqPool.Get() - defer c.Common.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.Common.ReqPool.Get() + defer c.Common.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: "https://api.live.bilibili.com/xlive/web-room/v1/index/getInfoByRoom?room_id=" + Roomid, Header: map[string]string{ @@ -562,9 +561,8 @@ func (t *GetFunc) getRoomPlayInfo() (missKey []string) { return true }) - reqi := t.Common.ReqPool.Get() - defer t.Common.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := t.Common.ReqPool.Get() + defer t.Common.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: "https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?no_playurl=0&mask=1&qn=0&platform=web&protocol=0,1&format=0,2&codec=0,1&room_id=" + Roomid, Header: map[string]string{ @@ -656,9 +654,8 @@ func (t *GetFunc) getRoomPlayInfoByQn() (missKey []string) { return true }) - reqi := t.Common.ReqPool.Get() - defer t.Common.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := t.Common.ReqPool.Get() + defer t.Common.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: "https://api.live.bilibili.com/xlive/web-room/v2/index/getRoomPlayInfo?no_playurl=0&mask=1&qn=" + strconv.Itoa(t.Live_qn) + "&platform=web&protocol=0,1&format=0,2&codec=0,1&room_id=" + Roomid, Header: map[string]string{ @@ -731,9 +728,8 @@ func (c *GetFunc) getDanmuInfo() (missKey []string) { return true }) - reqi := c.Common.ReqPool.Get() - defer c.Common.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.Common.ReqPool.Get() + defer c.Common.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuInfo?type=0&id=" + Roomid, Header: map[string]string{ @@ -787,9 +783,8 @@ func Get_face_src(uid string) string { return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: "https://api.live.bilibili.com/xlive/web-room/v1/index/getDanmuMedalAnchorInfo?ruid=" + uid, Header: map[string]string{ @@ -843,9 +838,8 @@ func (c *GetFunc) getPopularAnchorRank() (missKey []string) { return true }) - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.ReqPool.Get() + defer c.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/general-interface/v1/rank/getPopularAnchorRank?uid=` + strconv.Itoa(c.Uid) + `&ruid=` + strconv.Itoa(c.UpUid) + `&clientType=2`, Header: map[string]string{ @@ -995,9 +989,8 @@ func (c *GetFunc) Get_guardNum() (missKey []string) { return true }) - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.ReqPool.Get() + defer c.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/app-room/v2/guardTab/topList?roomid=` + Roomid + `&page=1&ruid=` + strconv.Itoa(c.UpUid) + `&page_size=29`, Header: map[string]string{ @@ -1114,9 +1107,8 @@ func Info(UpUid int) (info J.Info) { Cookie[k.(string)] = v.(string) return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.bilibili.com/x/space/acc/info?mid=` + strconv.Itoa(UpUid) + `&token=&platform=web&jsonp=jsonp`, Proxy: c.C.Proxy, @@ -1170,9 +1162,8 @@ func (c *GetFunc) IsLogin() (isLogin bool) { return true }) - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.ReqPool.Get() + defer c.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.bilibili.com/x/web-interface/nav`, Header: map[string]string{ @@ -1242,9 +1233,8 @@ func (c *GetFunc) Get_cookie() (missKey []string) { var img_url string var oauth string { //获取二维码 - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - r := reqi.Item.(*reqf.Req) + r := c.ReqPool.Get() + defer c.ReqPool.Put(r) if e := r.Reqf(reqf.Rval{ Url: `https://passport.bilibili.com/qrcode/getLoginUrl`, Proxy: c.Proxy, @@ -1345,9 +1335,8 @@ func (c *GetFunc) Get_cookie() (missKey []string) { return } - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - r := reqi.Item.(*reqf.Req) + r := c.ReqPool.Get() + defer c.ReqPool.Put(r) if e := r.Reqf(reqf.Rval{ Url: `https://passport.bilibili.com/qrcode/getLoginInfo`, PostStr: `oauthKey=` + oauth, @@ -1463,9 +1452,8 @@ func Get_list_in_room() (array []J.GetMyMedals_Items) { { //获取牌子列表 var medalList []J.GetMyMedals_Items for pageNum := 1; true; pageNum += 1 { - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - r := reqi.Item.(*reqf.Req) + r := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(r) if e := r.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/app-ucenter/v1/user/GetMyMedals?page=` + strconv.Itoa(pageNum) + `&page_size=10`, Header: map[string]string{ @@ -1523,9 +1511,8 @@ func Get_weared_medal() (item J.GetWearedMedal_Data) { }) { //获取 - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - r := reqi.Item.(*reqf.Req) + r := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(r) if e := r.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/live_user/v1/UserInfo/get_weared_medal`, Header: map[string]string{ @@ -1639,9 +1626,8 @@ func (c *GetFunc) CheckSwitch_FansMedal() (missKey []string) { } } { //切换牌子 - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - r := reqi.Item.(*reqf.Req) + r := c.ReqPool.Get() + defer c.ReqPool.Put(r) if e := r.Reqf(reqf.Rval{ Url: post_url, PostStr: post_str, @@ -1701,9 +1687,8 @@ func Dosign() { return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/WebGetSignInfo`, Header: map[string]string{ @@ -1753,9 +1738,8 @@ func Dosign() { return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/web-ucenter/v1/sign/DoSign`, Header: map[string]string{ @@ -1815,9 +1799,8 @@ func (c *GetFunc) Get_LIVE_BUVID() (missKey []string) { } for _, roomid := range roomIdList { //获取 - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.ReqPool.Get() + defer c.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/live/getRoomKanBanModel?roomid=` + roomid, Header: map[string]string{ @@ -1915,9 +1898,8 @@ func Gift_list() (list []Gift_list_type_Data_List) { return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/web-room/v1/gift/bag_list?t=` + strconv.Itoa(int(sys.Sys().GetMTime())) + `&room_id=` + strconv.Itoa(c.C.Roomid), Header: map[string]string{ @@ -1986,9 +1968,8 @@ func (c *GetFunc) Silver_2_coin() (missKey []string) { return true }) - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.ReqPool.Get() + defer c.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/revenue/v1/wallet/getStatus`, Header: map[string]string{ @@ -2038,9 +2019,8 @@ func (c *GetFunc) Silver_2_coin() (missKey []string) { return true }) - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.ReqPool.Get() + defer c.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/revenue/v1/wallet/getRule`, Header: map[string]string{ @@ -2095,9 +2075,8 @@ func (c *GetFunc) Silver_2_coin() (missKey []string) { return true }) - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.ReqPool.Get() + defer c.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/revenue/v1/wallet/silver2coin`, PostStr: url.PathEscape(post_str), @@ -2190,9 +2169,8 @@ func GetHisStream() (Uplist []UpItem) { return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.bilibili.com/x/web-interface/history/cursor?type=live&ps=10`, Header: map[string]string{ @@ -2269,10 +2247,8 @@ func RoomEntryAction(roomId int) { return } - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/web-room/v1/index/roomEntryAction`, PostStr: fmt.Sprintf("room_id=%d&platform=pc&csrf_token=%s&csrf=%s&visit_id=", roomId, csrf, csrf), @@ -2327,9 +2303,8 @@ func (c *GetFunc) getOnlineGoldRank() (misskey []string) { return } //超额请求阻塞,超时将取消 - reqi := c.ReqPool.Get() - defer c.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.ReqPool.Get() + defer c.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: fmt.Sprintf("https://api.live.bilibili.com/xlive/general-interface/v1/rank/getOnlineGoldRank?ruid=%d&roomId=%d&page=1&pageSize=10", c.UpUid, c.Roomid), @@ -2393,9 +2368,8 @@ func Feed_list() (Uplist []J.FollowingDataList) { return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) for pageNum := 1; true; pageNum += 1 { if err := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/xlive/web-ucenter/user/following?page=` + strconv.Itoa(pageNum) + `&page_size=10`, @@ -2456,9 +2430,8 @@ func GetHistory(Roomid_int int) (j J.GetHistory) { Roomid := strconv.Itoa(Roomid_int) { //使用其他api - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: "https://api.live.bilibili.com/xlive/web-room/v1/dM/gethistory?roomid=" + Roomid, Header: map[string]string{ @@ -2500,9 +2473,8 @@ func SearchUP(s string) (list []searchresult) { } //超额请求阻塞,超时将取消 { //使用其他api - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) Cookie := make(map[string]string) c.C.Cookie.Range(func(k, v interface{}) bool { @@ -2570,9 +2542,8 @@ func IsConnected() bool { return true } - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: "https://www.bilibili.com", Proxy: c.C.Proxy, diff --git a/Reply/stream.go b/Reply/stream.go index 36c637c..8d74550 100644 --- a/Reply/stream.go +++ b/Reply/stream.go @@ -21,7 +21,6 @@ import ( p "github.com/qydysky/part" file "github.com/qydysky/part/file" funcCtrl "github.com/qydysky/part/funcCtrl" - idpool "github.com/qydysky/part/idpool" log "github.com/qydysky/part/log" msgq "github.com/qydysky/part/msgq" pool "github.com/qydysky/part/pool" @@ -53,7 +52,7 @@ type M4SStream struct { Callback_startRec func(*M4SStream) error //录制开始的回调 Callback_stopRec func(*M4SStream) //录制结束的回调 Callback_stop func(*M4SStream) //实例结束的回调 - reqPool *idpool.Idpool //请求池 + reqPool *pool.Buf[reqf.Req] //请求池 } type M4SStream_Config struct { @@ -212,8 +211,7 @@ func (t *M4SStream) fetchCheckStream() bool { }) for _, v := range t.common.Live { - req := t.reqPool.Get() - r := req.Item.(*reqf.Req) + r := t.reqPool.Get() if e := r.Reqf(reqf.Rval{ Url: v.Url, Retry: 10, @@ -244,7 +242,7 @@ func (t *M4SStream) fetchCheckStream() bool { t.log.L(`W: `, `live响应错误`, r.Response.Status, string(r.Respon)) t.common.Live = t.common.Live[1:] } - t.reqPool.Put(req) + t.reqPool.Put(r) } return len(t.common.Live) != 0 @@ -257,9 +255,8 @@ func (t *M4SStream) fetchParseM3U8() (m4s_links []*m4s_link_item, m3u8_addon []b } // 开始请求 - req := t.reqPool.Get() - defer t.reqPool.Put(req) - r := req.Item.(*reqf.Req) + r := t.reqPool.Get() + defer t.reqPool.Put(r) // 请求解析m3u8内容 for k, v := range t.common.Live { @@ -629,10 +626,8 @@ func (t *M4SStream) saveStreamFlv() (e error) { s := signal.Init() //开始获取 - req := t.reqPool.Get() + r := t.reqPool.Get() { - r := req.Item.(*reqf.Req) - go func() { select { //停止录制 @@ -780,7 +775,7 @@ func (t *M4SStream) saveStreamFlv() (e error) { } } } - t.reqPool.Put(req) + t.reqPool.Put(r) if s.Islive() { s.Done() @@ -870,10 +865,8 @@ func (t *M4SStream) saveStreamM4s() (e error) { link.status = 1 // 设置切片状态为正在下载 link.tryDownCount += 1 - req := t.reqPool.Get() - defer t.reqPool.Put(req) - - r := req.Item.(*reqf.Req) + r := t.reqPool.Get() + defer t.reqPool.Put(r) reqConfig := reqf.Rval{ Url: link.Url, Timeout: 3000, diff --git a/Reply/tts.go b/Reply/tts.go index 3cdf667..aeca87c 100644 --- a/Reply/tts.go +++ b/Reply/tts.go @@ -183,9 +183,8 @@ func play() { } func baidu(msg string) error { - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://fanyi.baidu.com/gettts?lan=zh&text=` + url.PathEscape(msg) + `&spd=5&source=web`, SaveToPath: sys.Sys().Cdir() + `/tts.mp3`, @@ -240,9 +239,8 @@ func youdao(msg string) error { postS += k + `=` + v } - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if err := req.Reqf(reqf.Rval{ Url: `https://openapi.youdao.com/ttsapi`, PostStr: url.PathEscape(postS), diff --git a/Send/Send.go b/Send/Send.go index e464db1..5b7ddd2 100644 --- a/Send/Send.go +++ b/Send/Send.go @@ -101,9 +101,8 @@ func Danmu_s2(data map[string]string) { postStr, contentType := reqf.ToForm(data) l.L(`I: `, "发送", data[`msg`], "至", data[`roomid`]) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - r := reqi.Item.(*reqf.Req) + r := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(r) err := r.Reqf(reqf.Rval{ Url: "https://api.live.bilibili.com/msg/send", PostStr: postStr, diff --git a/Send/Send_gift.go b/Send/Send_gift.go index 7d5165c..dcca635 100644 --- a/Send/Send_gift.go +++ b/Send/Send_gift.go @@ -58,9 +58,8 @@ func Send_gift(gift_id, bag_id, gift_num int) { return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if e := req.Reqf(reqf.Rval{ Url: `https://api.live.bilibili.com/gift/v2/live/bag_send`, PostStr: url.PathEscape(sendStr), diff --git a/Send/Send_pm.go b/Send/Send_pm.go index d54838b..aaee17e 100644 --- a/Send/Send_pm.go +++ b/Send/Send_pm.go @@ -66,9 +66,8 @@ func Send_pm(uid int, msg string) error { return true }) - reqi := c.C.ReqPool.Get() - defer c.C.ReqPool.Put(reqi) - req := reqi.Item.(*reqf.Req) + req := c.C.ReqPool.Get() + defer c.C.ReqPool.Put(req) if e := req.Reqf(reqf.Rval{ Url: `https://api.vc.bilibili.com/web_im/v1/web_im/send_msg`, PostStr: url.PathEscape(send_str),