go实现Event事件模型修正
妙音
posted @ 2021年6月03日 22:39
in golang
, 1322 阅读
描述
之前文章中实现了event事件模型,实际使用中发现了几个问题
1. 并发会出现情况send先执行, 此时还没有waiter。将wait拆分成addWaiter和wait()
2. 不能使用缓冲通道。会出现send完成,wait还未收到的情况。
用法修正
e.Reset() waiter := e.AddWaiter() waiter := e.Wait(waiter, xxx) e.Send(xxx)
事件实现
package common import ( "context" "errors" "go.uber.org/zap" "time" ) var NOT_USED = struct{}{} func NewEvent() *Event { e := &Event{ log: zap.S(), } e.Reset() return e } type Event struct{ //监听者 waiters []chan interface{} //结果 result interface{} //上下文控制 ctxBg context.Context ctxCancel context.CancelFunc //日志 log *zap.SugaredLogger } func (e *Event) AddWaiter() *chan interface{}{ //等待者 resultChan := make(chan interface{}, 0) e.waiters = append(e.waiters, resultChan) return &resultChan } //等待结果 func (e *Event) Wait(waiter *chan interface{}, timeout time.Duration) (interface{}, error){ if e.result ==NOT_USED{ ctx, cancel := context.WithTimeout(e.ctxBg, time.Second*timeout) defer cancel() //等待 select{ case result := <- *waiter: return result, nil case <- ctx.Done(): if ctx.Err() == context.Canceled{ return nil, nil } return nil, errors.New("event wait timeout") } return nil, nil }else{ return e.result, nil } } //发送结果 func (e *Event) Send(result interface{}) error{ //防止发送多次 if e.result !=NOT_USED{ return errors.New("Event is used") } ctx, cancel := context.WithTimeout(e.ctxBg, time.Second*3) defer cancel() for _, resultChan := range e.waiters{ select{ case resultChan <- result: case <- ctx.Done(): e.log.Warnf("Event.Send %p resultChan=%d, result=%v", e, len(resultChan), result) } } e.result = result return nil } //重置 func (e *Event) Reset(){ if e.ctxBg !=nil{ e.ctxCancel() } e.ctxBg, e.ctxCancel = context.WithCancel(context.Background()) e.waiters = nil e.result = NOT_USED }
2023年6月14日 12:58
Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. vay tien online
2023年6月17日 20:16
No doubt this is an excellent post I got a lot of knowledge after reading good luck. Theme of blog is excellent there is almost everything to read, Brilliant post. 수원출장마사지
2023年6月21日 11:49
I recently found many useful information in your website especially this blog page. Among the lots of comments on your articles. Thanks for sharing. andro supplement
2023年7月19日 05:21
I was looking for SEO services and came across this site. Another great site I recommend is Bus Web Design for SEO experts.
2023年7月19日 05:24
In regard to this article, we also have the perfect resource for Autoglass services.
2023年7月19日 05:29
Thank you for sharing this great post! I came across this site while searching for how to sell my car for cash.
2023年7月19日 05:34
Tier2Tek staffing is one of the hidden gems of the staffing world. Great rates, amazing quality service.
2023年8月18日 17:09
Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. Newsnow Nigeria
2023年8月19日 18:31
Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. Liga787
2023年8月20日 12:13
Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. large magnum rifle primers
2023年8月27日 13:35
Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. Mounjaro wholesale USA
2023年8月28日 16:31
Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. Avaliador Premiado Funciona
2023年8月28日 21:24
Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. เว็บแทงบอลดีที่สุดUFABET
2023年9月05日 21:20
Nice post! This is a very nice blog that I will definitively come back to more times this year! Thanks for informative post. free bubble games
2023年9月10日 19:08
I really appreciate the kind of topics you post here. Thanks for sharing us a great information that is actually helpful. Good day! trippy tips ice cream
2023年9月16日 14:11
Keep up the good work , I read few posts on this web site and I conceive that your blog is very interesting and has sets of fantastic information. leather lounge suite nz
2023年9月19日 00:32
I appreciate your presence here and your desire to contribute valuable and up-to-date content to my website. I believe it's important to continually improve and maintain a strong online presence for your own website as well.
<a href="https://acustomboxes.com/">A Custom Boxes</a>
2023年9月27日 19:27
For this reason it's much better that you could relevant evaluation in front of generating. You'll be able to publish higher post that way. California Time Zone
2023年9月28日 01:39
This really even a very good put up that i in fact really enjoyed perusing. It is not necessarily regular that i include the option to ascertain a specific thing. Chicago Time Zone
2023年9月28日 02:25
I love someone's submitting. The great to see you truly describe inside terms with all the heart and soul additionally decision with this important make a difference is frequently successfully looked at. Utah Time Zone
2023年10月01日 13:07
Thanks for sharing the post.. parents are worlds best person in each lives of individual..they need or must succeed to sustain needs of the family. Dinobet
2023年10月04日 21:14
i really like this article please keep it up. 丿 讷乇蹖賳丕趬丕 趨禺賴 丿 鬲乇讴蹖蹛 丌賳賱丕蹖賳 賵蹖夭賴
2023年10月05日 13:03
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. XM Indonesia
2023年10月06日 13:10
It's superior, however , check out material at the street address. <a href="https://www.cbdflex.com/product/30ct-cbd-gummy-bears-300mg/">Buy CBD Gummy Bears</a>
2023年10月06日 21:19
i really like this article please keep it up. ฝัน เห็น สามเณร
2023年10月07日 17:33
i really like this article please keep it up. AVALIADOR PREMIADO
2023年10月11日 20:12
i really like this article please keep it up. Fortune Tiger
2023年10月14日 20:04
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! CIRCULAXIL
2023年10月17日 19:10
This article gives the light in which we can observe the reality. This is very nice one and gives indepth information. Thanks for this nice article. juul 2
2023年10月18日 00:34
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! how to find product sku for dser
2023年10月23日 18:41
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! how do I recover my stolen crypto assets?
2023年10月24日 18:13
i love reading this article so beautiful!!great job! KETO ACV GUMMIES
2023年10月24日 19:15
The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. ต้นสาริกาลิ้นทอง ความหมาย
2023年10月24日 20:33
I am glad you take pride in what you write. This makes you stand way out from many other writers that push poorly written content. พันธุ์ มะพร้าว ใน ประเทศไทย
2023年10月24日 21:17
I exactly got what you mean, thanks for posting. And, I am too much happy to find this website on the world of Google. ลักษณะของสิงโต
2023年10月29日 01:22
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! opening company in Hong Kong
2023年11月01日 00:05
Took me time to read all the comments, but I really enjoyed the article. It proved to be Very helpful to me and I am sure to all the commenters here! It’s always nice when you can not only be informed, but also entertained! wallpaper singapore
2023年11月01日 13:46
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! Sajta de Pollo
2023年11月03日 12:44
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! Avaliador Premiado
2023年11月03日 22:29
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! AVALIADOR DE MARCAS
2023年11月05日 00:12
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! wallpaper singapore
2023年11月11日 17:48 Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! sidney applebaum
2023年11月12日 21:18
The pencils are exceptional, and the packaging is the silent applause in the background. pencil packing job
2023年11月15日 20:44
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! kuliah online kelas karyawan murah
2023年11月16日 13:07
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! temazepam kopen ideal
2023年11月17日 16:50
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! dentist greenslopes
2023年11月19日 13:55
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! best breakfast cafe canggu
2023年11月19日 21:31
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! شراء لايكات
2023年11月24日 13:48
Super-Duper site! I am Loving it!! Will come back again, Im taking your feed also, Thanks. Crimsafe northside
2023年12月20日 12:45
Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! Ladbrokes Casino Bonuses
2023年12月21日 14:26
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. roket288
2023年12月22日 23:53
This is very interesting, but it is necessary to click on this link: <a href="https://calculator-converter.com/millimeters-to-inches.htm">millimeters to inches</a>
2023年12月23日 18:20
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. eladó jogosítvány
2023年12月25日 02:47
I wrote about a similar issue, I give you the link to my site. <a href="https://metric-calculator.com/convert-cup-to-dl.htm">convertion cup to dl</a>
2024年1月06日 17:20
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. comprar erva online espanha
2024年1月10日 22:48
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. buy-high-school-diploma-online
2024年1月13日 20:59
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. UFABETพนันบอลออนไลน์ฟรีถูกกฏหมาย
2024年1月15日 00:01
Wow, this is fascinating reading. I am glad I found this and got to read it. Great job on this content. I liked it a lot. Thanks for the great and unique info. UFABETเว็บพนันบอลถูกกฏหมายรวดเร็วที่สุด
2024年1月15日 00:08
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. UFABETเข้าเว็บแทงบอลอย่างไร
2024年1月15日 00:36
Wow, this is fascinating reading. I am glad I found this and got to read it. Great job on this content. I liked it a lot. Thanks for the great and unique info. UFABETเข้าสู่ระบบไม่มีค่าธรรมเนียม
2024年1月15日 01:50
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. UFABETแทงบอลผ่านมือถือตรงเว็บแม่
2024年1月15日 19:24
Wow, this is fascinating reading. I am glad I found this and got to read it. Great job on this content. I liked it a lot. Thanks for the great and unique info. UFABETสมัครเว็บแทงบอลออนไลน์รับยูสเซอร์ฟรี
2024年1月15日 21:54
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. UFABETพนันบอลไม่ผ่านเอเย่นต์เว็บตรง
2024年1月16日 14:13
Wow, this is fascinating reading. I am glad I found this and got to read it. Great job on this content. I liked it a lot. Thanks for the great and unique info. UFABETแทงบอลบนมือถือที่ดีที่สุด
2024年1月16日 19:05
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. UFABETสมัครแทงบอลคาสิโนออนไลน์
2024年1月17日 19:45
These you will then see the most important thing, the application provides you a website a powerful important internet page: high-quality
2024年1月18日 19:16
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. plug play
2024年1月19日 17:16
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. cheap instagram likes $1
2024年1月20日 21:32
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. buy youtube shorts views
2024年1月31日 20:54
I think that thanks for the valuabe information and insights you have so provided here. delta77.shop
2024年3月20日 22:38
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. tempo gear
2024年3月22日 21:17
Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards, how to start merchant processing company
2024年3月23日 00:19 Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards, panen77
2024年3月25日 16:28
Thank you again for all the knowledge you distribute,Good post. I was very interested in the article, it's quite inspiring I should admit. I like visiting you site since I always come across interesting articles like this one.Great Job, I greatly appreciate that.Do Keep sharing! Regards, pay for someone to take my online classes
2024年5月02日 22:56
I am very happy to discover your post as it will become on top in my collection of favorite blogs to visit. 스포츠 토토
2024年5月03日 14:48
Is this a new post? i ask because i think i read the same article a month ago on hubpages.
Website: https://111.90.141.115/