Skip to content

Instantly share code, notes, and snippets.

@morentharia
Created January 21, 2018 15:08
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save morentharia/41079a5d9e0b80e842a04b80f068b891 to your computer and use it in GitHub Desktop.
reconnet and pubish
package main
import (
"fmt"
"time"
)
func connector() chan struct{} {
fmt.Println("zzzz")
connectCh := make(chan struct{})
publishCh := make(chan struct{})
sessionCh := make(chan int)
freeSessionCh := make(chan struct{})
go func() {
conn := 0
// notify bla bla bla
for {
select {
case <-connectCh:
fmt.Printf("connect %d\n", conn)
time.Sleep(3 * time.Second)
conn += 1
fmt.Printf("connect %d end\n", conn)
case sessionCh <- conn:
fmt.Println("someone want session")
<-freeSessionCh
}
}
}()
// lets go
go func() {
for {
fmt.Println("reconnect")
connectCh <- struct{}{}
time.Sleep(8 * time.Second)
}
}()
connectCh <- struct{}{}
for i := 0; i < 40; i++ {
go func(i int) {
conn := <-sessionCh
fmt.Printf("get conn for publish %d \n", conn)
<-publishCh
fmt.Printf("read from publish done %d\n", i)
time.Sleep(1 * time.Second)
freeSessionCh <- struct{}{}
}(i)
}
return publishCh
}
func main() {
pub := connector()
for i := 0; i < 5; i++ {
go func() {
for {
select {
case pub <- struct{}{}:
fmt.Println("ok send to publish")
// default:
// fmt.Println("fail publish")
// time.Sleep(1 * time.Second)
}
}
}()
}
time.Sleep(15 * time.Second)
// for {
// select {
// case testCh <- struct{}{}:
// fmt.Println("hhaha end")
// default:
// fmt.Println("can't send to channel")
// time.Sleep(1 * time.Second)
// }
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment