英文:
How to send message to multiple channels in go
问题
我的翻译如下:
所以我的问题是如何向通道发送消息,只有在通道未关闭且仅发送一次时,broadcast
函数才会接收到消息。
发送消息后,应该增加sentNumber
。
我只是提醒一下,发送消息到所有通道是有时间限制的!
package main
import (
"fmt"
"sync"
"time"
)
var (
sentNumber int
)
func broadcast(waitTime time.Duration, message string, ch ...chan string) (sentNumber int) {
start := time.Now()
for _, channel := range ch {
if time.Since(start) >= waitTime {
break
}
go send(channel, message)
}
return 0
}
func send(channel chan string, message string) {
for {
if _, open := <-channel; open {
break
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
channel <- message
}()
wg.Wait()
}
func main() {
a := make(chan string, 1)
b := make(chan string, 1)
broadcast(5, "秘密消息", a, b)
fmt.Println(<-a)
fmt.Println(<-b)
}
希望对你有帮助!
英文:
so my question is how to send message to channels that broadcast
function gets only if channel is not closed and just for once.
after sending message should increase sentNumber
.
I say just to remind, there is a time limit for sending message to all channels!
package main
import (
"fmt"
"sync"
"time"
)
var (
sentNumber int
)
func broadcast(waitTime time.Duration, message string, ch ...chan string) (sentNumber int) {
start := time.Now()
for _, channel := range ch {
if time.Since(start) >= waitTime {
break
}
go send(channel, message)
}
return 0
}
func send(channel chan string, message string) {
for {
if _,open := <-channel; open{
break
}
}
var wg sync.WaitGroup
wg.Add(1)
go func() {
wg.Done()
channel <- message
}()
wg.Wait()
}
func main() {
a := make(chan string, 1)
b := make(chan string, 1)
broadcast(5, "secret message", a, b)
fmt.Println(<-a)
fmt.Println(<-b)
}
答案1
得分: 1
time.Since(start) >= waitTime
无法中断send
函数。- 在这种情况下,
go send(channel, message)
不应该比单线程队列更高效。 broadcast
不负责检查通道是否已关闭,通道的创建/关闭不是由broadcast
完成。
package main
import (
"context"
"fmt"
"time"
)
func broadcast(waitTime time.Duration, message string, chs ...chan string) (sentNumber int) {
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
defer cancel()
jobQueue := make(chan chan string, len(chs))
for _, c := range chs {
jobQueue <- c
}
queue:
for c := range jobQueue {
select {
case c <- message:
// 发送成功
sentNumber += 1
if sentNumber == len(chs) {
cancel()
}
case <-ctx.Done():
// 超时,中断任务队列
break queue
default:
// 如果发送失败,稍后重试
jobQueue <- c
}
}
return
}
func main() {
a := make(chan string)
b := make(chan string)
go func() {
time.Sleep(time.Second)
fmt.Println("a:", <-a)
}()
go func() {
time.Sleep(3 * time.Second)
fmt.Println("b:", <-b)
}()
c := broadcast(2*time.Second, "secret message", a, b)
fmt.Printf("发送数量:%d\n", c)
time.Sleep(3 * time.Second)
}
英文:
time.Since(start) >= waitTime
can't break thesend
functiongo send(channel, message)
should not be more efficient than a single thread queue in this casebroadcast
has no responsibility to check if channel has been closed, channels were not created/closed bybroadcast
package main
import (
"context"
"fmt"
"time"
)
func broadcast(waitTime time.Duration, message string, chs ...chan string) (sentNumber int) {
ctx, cancel := context.WithTimeout(context.Background(), waitTime)
defer cancel()
jobQueue := make(chan chan string, len(chs))
for _, c := range chs {
jobQueue <- c
}
queue:
for c := range jobQueue {
select {
case c <- message:
// sent success
sentNumber += 1
if sentNumber == len(chs) {
cancel()
}
case <-ctx.Done():
// timeout, break job queue
break queue
default:
// if send failed, retry later
jobQueue <- c
}
}
return
}
func main() {
a := make(chan string)
b := make(chan string)
go func() {
time.Sleep(time.Second)
fmt.Println("a:", <-a)
}()
go func() {
time.Sleep(3 * time.Second)
fmt.Println("b:", <-b)
}()
c := broadcast(2*time.Second, "secret message", a, b)
fmt.Printf("sent count:%d\n", c)
time.Sleep(3 * time.Second)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论