http://dave.cheney.net/2015/08/08/performance-without-the-event-loop
Goroutines

Rather than relying on the kernel to manage their time sharing, goroutines
are cooperatively scheduled. The switch between goroutines only happens at
well defined points, when an explicit call is made to the Go runtime
scheduler. The major points where a goroutine will yield to the scheduler
include:

   - Channel send and receive operations, if those operations would block.
   - The go statement, although there is no guarantee that new goroutine
   will be scheduled immediately.
   - Blocking syscalls like file and network operations.
   - After being stopped for a garbage collection cycle.


On Wed, Sep 21, 2016 at 2:48 PM Justin Israel <justinisr...@gmail.com>
wrote:

> I'm not an expert, but this looks to be the expected behaviour to me. If
> you have GOMAXPROCS=8 and you tie up 8 goroutines with infinite loops, then
> there is nothing left to service requests. They are spinning the cpus
> without any kind of context switching points. This kind of behaviour has
> been mentioned in the past. There have to be points in your code for the
> runtime to switch to another goroutine, such as function calls, chan
> send/receive, syscalls (?), etc.
>
> On Wed, Sep 21, 2016 at 2:42 PM Tony Chen <chengnuo...@gmail.com> wrote:
>
>> ### What version of Go are you using (`go version`)?
>> go 1.6.2 mac/amd64
>>
>> ### What operating system and processor architecture are you using (`go
>> env`)?
>> GOARCH="amd64"
>> GOBIN=""
>> GOEXE=""
>> GOHOSTARCH="amd64"
>> GOHOSTOS="darwin"
>> GOOS="darwin"
>> GOPATH="/data/apps/go"
>> GORACE=""
>> GOROOT="/usr/local/go"
>> GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
>> GO15VENDOREXPERIMENT="1"
>> CC="clang"
>> GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments
>> -fmessage-length=0 -fno-common"
>> CXX="clang++"
>> CGO_ENABLED="1"
>>
>> ### What did you do?
>>
>> package main
>>
>> import (
>> "net/http"
>> "strconv"
>> "fmt"
>> )
>>
>> func main() {
>> http.HandleFunc("/", httpHandler)
>> http.ListenAndServe(":8001", nil)
>> }
>>
>> func httpHandler(w http.ResponseWriter, r *http.Request) {
>> b, _ := strconv.ParseBool(r.URL.Query().Get("loop"))
>> if b {
>> loop()
>> }
>> fmt.Fprintf(w, "ok")
>> }
>>
>> func loop() {
>> i := 0
>> for {
>> if i == 10000 {
>> i = 0
>> }
>> i++
>> }
>> }
>> 1. curl http://127.0.0.1:8001/?loop=true
>> 2. curl http://127.0.0.1:8001/?loop=false
>> When the loop to the CPU processing and about 8 times the request,
>> refused to all the HTTP request.
>> ### What did you expect to see?
>>
>> export GODEBUG=schedtrace=1000
>> SCHED 1010ms: gomaxprocs=8 idleprocs=8 threads=6 spinningthreads=0
>> idlethreads=3 runqueue=0 [0 0 0 0 0 0 0 0]
>> SCHED 2016ms: gomaxprocs=8 idleprocs=8 threads=6 spinningthreads=0
>> idlethreads=3 runqueue=0 [0 0 0 0 0 0 0 0]
>> SCHED 3019ms: gomaxprocs=8 idleprocs=8 threads=6 spinningthreads=0
>> idlethreads=3 runqueue=0 [0 0 0 0 0 0 0 0]
>> SCHED 4028ms: gomaxprocs=8 idleprocs=7 threads=6 spinningthreads=0
>> idlethreads=2 runqueue=0 [0 0 0 0 0 0 0 0]
>> SCHED 5033ms: gomaxprocs=8 idleprocs=5 threads=6 spinningthreads=0
>> idlethreads=0 runqueue=0 [0 0 0 0 0 0 0 0]
>> SCHED 6033ms: gomaxprocs=8 idleprocs=3 threads=8 spinningthreads=0
>> idlethreads=0 runqueue=0 [0 0 0 0 0 0 0 0]
>> SCHED 7035ms: gomaxprocs=8 idleprocs=0 threads=10 spinningthreads=0
>> idlethreads=0 runqueue=0 [0 0 0 0 0 0 0 0]
>> SCHED 8037ms: gomaxprocs=8 idleprocs=0 threads=10 spinningthreads=0
>> idlethreads=0 runqueue=1 [0 0 0 0 0 0 0 0]
>> SCHED 9044ms: gomaxprocs=8 idleprocs=0 threads=10 spinningthreads=0
>> idlethreads=0 runqueue=1 [0 0 0 0 0 0 0 0]
>> SCHED 10050ms: gomaxprocs=8 idleprocs=0 threads=10 spinningthreads=0
>> idlethreads=0 runqueue=1 [0 0 0 0 0 0 0 0]
>> SCHED 11059ms: gomaxprocs=8 idleprocs=0 threads=10 spinningthreads=0
>> idlethreads=0 runqueue=1 [0 0 0 0 0 0 0 0]
>> SCHED 12060ms: gomaxprocs=8 idleprocs=0 threads=10 spinningthreads=0
>> idlethreads=0 runqueue=1 [0 0 0 0 0 0 0 0]
>>
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "golang-nuts" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to golang-nuts+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to golang-nuts+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to