For this use case, it seems like it might be valid: This is dumbed down code, but you should get the idea -
func Log(v interface{}){
1. get json - json marshal some large object v
2. write big buf to a file
}
.. the above ^^ could be pretty blocking, so maybe want to put in a goroutine:
func Log(v interface{}){
go func(){
1. get json - json marshal some large object v
2. write big buf to a file (not necessarily stdout)
}()
}
but the problem with that is we are creating a lot of goroutines here, which might defeat the purpose of putting it in a goroutine anyway.
So my question is - is there a way to somehow use a pool of goroutines, and use those. Even just like 10 in a pool would be fabulous.
I assume using channels to communicate with them, but not sure exactly how
Two stipulations:
- only need 1 job to be added to the queue at a time (but they are added asynchronously)
- if the queue is full, I want to just run on a new goroutine separately, not adding a new worker to the queue
This code basically works. If we were just writing to stdout, then this really would be unnecessary, but since we are writing to multiple files, then could benefit from some goroutine stuff.
here is a test:
and here is the real world use case:
in my case I don't need the waitgroup, since it's all synced out code, but having the wg makes it more generic.