goroutine

How to stop a goroutine that is listening for RethinkDB changefeeds?

时光毁灭记忆、已成空白 提交于 2019-12-10 22:22:08
问题 I am trying to figure out how to use RethinkDB changefeeds with golang. My specific question is how to stop a goroutine that listens for changes to the database. See, for example, the function getData() below. I run this from a handler function by calling go getData(c) . Whenever the database updates, the record is passed to the channel c , which is then passed to the handler function and sent to the client using SSE technology. My question is: when the client disconnects, I know how to stop

Close the goroutine reading from a TCP connection without closing connection

落爺英雄遲暮 提交于 2019-12-10 22:15:37
问题 I love the way Go handles I/O multiplexing internally which epoll and another mechanisms and schedules green threads (go-routine here) on its own giving the freedom to write synchronous code. I know TCP sockets are non-blocking and read will give EAGAIN when no data is available. Given that, conn.Read(buffer) will detect this and blocks the go routine doing a connection read with no data available in the socket buffer . Is there a way to stop such go routine without closing the underlying

Synchronization for several goroutines using channels

烈酒焚心 提交于 2019-12-10 21:08:25
问题 I need to start a number of workers with single task queue and single result queue. Each worker should be started in different goroutine. And I need to wait till all workers will be finished and task queue will be empty before exiting from program. I have prepare small example for goroutine synchronization. The main idea was that we count tasks in queue and waiting for all workers to finish jobs. But current implementation sometime miss values. Why this happends and how to solve the problem?

How does a caller function to recover from child goroutine's panics

你离开我真会死。 提交于 2019-12-10 21:04:13
问题 I used to think the panic in a goroutine will kill the program if its caller finishes before the panic (the deferred recovering gives no help since at that point there's no panic occurs yet), until I tried following code: func fun1() { fmt.Println("fun1 started") defer func() { if err := recover(); err != nil { fmt.Println("recover in func1") } }() go fun2() time.Sleep(10 * time.Second) // wait for the boom! fmt.Println("fun1 ended") } func fun2() { fmt.Println("fun2 started") time.Sleep(5 *

How to stop goroutine [duplicate]

China☆狼群 提交于 2019-12-10 17:35:33
问题 This question already has answers here : How to stop a goroutine (6 answers) Closed 2 years ago . I have a goroutine that calls a function and with a special parameter i want to start or stop this goroutine. My problem is that this code never stops my goroutine, it creates everytime a new job. quit := make(chan bool) run := make(chan bool) go func() { for { select { case <-quit: close(run) case <-run: myFunc(c) default: } } }() if x == true { quit <- true } else { run <- true } How do I stop

Are deferred functions called when SIGINT is received in Go?

五迷三道 提交于 2019-12-10 15:56:50
问题 For the snippet below, the deferred call is not made when ^C is received. Is it possible that the cleanup introduces a race condition? If yes, what could be a better pattern of cleanup on receiving an interrupt? func fn() { // some code defer cleanup() go func() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) // Block until a signal is received. _ = <-c cleanup() } for { // Infinite loop. Returns iff an error is encountered in the // body } } 回答1: Note that if you "install" your

Goroutines blocked connection pool

天涯浪子 提交于 2019-12-10 05:28:14
问题 package main import ( "database/sql" "fmt" _ "github.com/lib/pq" "sync" ) func main() { db, _ := sql.Open("postgres", fmt.Sprintf("host=%s dbname=%s user=%s sslmode=disable", "localhost", "dbname", "postgres")) defer db.Close() db.SetMaxOpenConns(15) var wg sync.WaitGroup for i := 0; i < 15; i++ { wg.Add(1) go func() { defer wg.Done() //#1 rows, _ := db.Query("SELECT * FROM reviews LIMIT 1") for rows.Next() { //#2 db.Exec("SELECT * FROM reviews LIMIT 1") } }() } wg.Wait() } Query #1 opens 15

如何优雅的控制goroutine的数量

放肆的年华 提交于 2019-12-09 22:16:56
1,为什么要控制goroutine的数量? goroutine固然好,但是数量太多了,往往会带来很多麻烦,比如耗尽系统资源导致程序崩溃,或者CPU使用率过高导致系统忙不过来。比如: for i:=0; i < 10000; i++ { go work() } 2,用什么方法控制goroutine的数量? 要在每一次执行go之前判断goroutine的数量,如果数量超了,就要阻塞go的执行。第一时间想到的就是使用通道。每次执行的go之前向通道写入值,直到通道满的时候就阻塞了,如下: var ch chan int func work() { //do something <-ch } func main() { ch = make(chan int, 10) for i:=0; i < 10000; i++ { ch <- 1 go work() } } 这样每次同时运行的goroutine就被限制为10个了。但是新的问题出现了,因为并不是所有的goroutine都执行完了,在main函数退出之后,还有一些goroutine没有执行完就被强制结束了。这个时候我们就需要用到sync.WaitGroup。使用WaitGroup等待所有的goroutine退出。如下: var wg *sync.WaitGroup func work() { defer wg.Done() //do

Forcing goroutines into the same thread

拟墨画扇 提交于 2019-12-09 16:24:11
问题 Is there a way to ensure that a goroutine will run only in a specific OS thread? For example, when GUI operations must run in the GUI thread, but there might be multiple goroutines running GUI code. GOMAXPROCS(1) does the job technically, but that defeats the purpose of multithreading. LockOSThread() works too, but that prevents any other goroutine from running in that thread as well. Is there a way to do this, or must everything that requires the same thread also run in the same goroutine?

Does Go have something like ThreadLocal from Java?

不想你离开。 提交于 2019-12-09 14:53:54
问题 I use Go and Gin to setup my website and want to know the database access time. I use goroutine so if don't use something like thread-local, I must change almost every function to do it. Does Go have a good way to do it? 回答1: The Go runtime and standard libraries do not provide goroutine local storage or a goroutine identifier that can be used to implement goroutine local storage. The third party gls package implements goroutine local storage in an interesting way. Some find this package