From: qydysky Date: Wed, 20 Jan 2021 09:00:15 +0000 (+0800) Subject: copy map X-Git-Tag: v0.5.6~2^2~7 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=e4a60e4383b4f7e60825876b5e385afa8bff6cae;p=bili_danmu%2F.git copy map --- diff --git a/Reply/F.go b/Reply/F.go index a5be923..d74f013 100644 --- a/Reply/F.go +++ b/Reply/F.go @@ -506,6 +506,7 @@ func Danmuji_auto(sleep int) { type Autoskip struct { buf map[string]Autoskip_item + sync.Mutex now uint ticker *time.Ticker sync.Mutex @@ -527,6 +528,7 @@ func init(){ <-autoskip.ticker.C if len(autoskip.buf) == 0 {continue} autoskip.now += 1 + autoskip.Lock() for k,v := range autoskip.buf{ if v.Exprie <= autoskip.now { autoskip.Lock() @@ -541,6 +543,12 @@ func init(){ } } } + {//copy map + tmp := make(map[string]Autoskip_item) + for k,v := range autoskip.buf {tmp[k] = v} + autoskip.buf = tmp + } + autoskip.Unlock() } }() } @@ -558,6 +566,8 @@ func Autoskipf(s string) uint { return v.Num } } + autoskip.Lock() + defer autoskip.Unlock() {//设置 autoskip.buf[s] = Autoskip_item{ Exprie:autoskip.now + 8, diff --git a/Reply/Reply.go b/Reply/Reply.go index 5dec003..b91f6a0 100644 --- a/Reply/Reply.go +++ b/Reply/Reply.go @@ -458,15 +458,20 @@ func (replyF) live(s string) { } //Msg-超级留言处理 -var sc_buf = make(map[string]bool) +var sc_buf = make(map[string]struct{}) func (replyF) super_chat_message(s string){ id := p.Json().GetValFromS(s, "data.id"); if id != nil { if _,ok := sc_buf[id.(string)];ok{return} if len(sc_buf) >= 10 { for k,_ := range sc_buf {delete(sc_buf, k);break} + {//copy map + tmp := make(map[string]struct{}) + for k,v := range sc_buf {tmp[k] = v} + sc_buf = tmp + } } - sc_buf[id.(string)] = true + sc_buf[id.(string)] = struct{}{} } uname := p.Json().GetValFromS(s, "data.user_info.uname"); price := p.Json().GetValFromS(s, "data.price"); diff --git a/demo/main.go b/demo/main.go index 0d24d23..c7e8a47 100644 --- a/demo/main.go +++ b/demo/main.go @@ -1,42 +1,36 @@ package main import ( - // "time" + "time" + "runtime" // "runtime/pprof" - // "os" + "os" // "log" - // "net/http" - // "fmt" - // _ "net/http/pprof" + "net/http" + "fmt" + _ "net/http/pprof" + "runtime/debug" // "github.com/skratchdot/open-golang/open" q "github.com/qydysky/bili_danmu" ) func main() { - // go func() { - // http.ListenAndServe("0.0.0.0:8899", nil) - // }() + go func() { + http.ListenAndServe("0.0.0.0:8899", nil) + }() // defer func(){ // open.Run("http://127.0.0.1:8899/debug/pprof/goroutine?debug=2") // time.Sleep(time.Duration(3)*time.Second) // }() - // go func(){ - // // var memStats_old runtime.MemStats - // for{ - // time.Sleep(time.Duration(10)*time.Second) - // var memStats runtime.MemStats - // runtime.ReadMemStats(&memStats) - // fmt.Printf("=====\n") - // // fmt.Printf("总内存:%v MB\n",memStats.Sys/1024e2/8) - // fmt.Printf("GC次数:%v \n",memStats.NumGC) - - // fmt.Printf("堆 :%v %v MB\n",memStats.HeapInuse/1024e2/8,(memStats.HeapIdle - memStats.HeapReleased)/1024e2/8) - - // fmt.Printf("栈 :%v/%v MB\n",memStats.StackInuse/1024e2/8,memStats.StackSys/1024e2/8) - // fmt.Printf("=====\n") - // // memStats_old = memStats - // } - // }() + go func(){ + for{ + View() + time.Sleep(time.Duration(60)*time.Second) + { + debug.FreeOSMemory() + } + } + }() // f, err := os.OpenFile("cpu.pprof", os.O_RDWR|os.O_CREATE, 0644) // if err != nil { // log.Fatal(err) @@ -47,4 +41,15 @@ func main() { q.Demo() // pprof.StopCPUProfile() +} + +func View(){ + var memStats runtime.MemStats + runtime.ReadMemStats(&memStats) + fmt.Printf("=====\n") + fmt.Printf("PID:%d\n",os.Getpid()) + fmt.Printf("总内存:%v MB\n",memStats.Alloc/1024e2/8) + fmt.Printf("GC次数:%v \n",memStats.NumGC) + fmt.Printf("堆 :%v %v MB\n",memStats.HeapInuse/1024e2/8,(memStats.HeapIdle - memStats.HeapReleased)/1024e2/8) + fmt.Printf("=====\n") } \ No newline at end of file