即使经过很短的时间,Go程序也会永远休眠。

I'm trying to build some short of semaphore in Go. Although when when the channel receives the signal it just sleeps forever.

I've tried changing the way to sleep and the duration to sleep, but it stills just stopping forever.

Here a representation of what I've tried:

func main() {
    backOffChan := make(chan struct{})
    go func() {
        time.Sleep(2)
        backOffChan <- struct{}{}
    }()
    for {
        select {
        case <-backOffChan:
            d := time.Duration(5 * time.Second)
            log.Println("reconnecting in %s", d)
            select {
            case <-time.After(d):
                log.Println("reconnected after %s", d)
                return
            }
        default:
        }
    }
}

I expect that it just returns after printing the log message and returning.

Thanks!

This code has a number of problems, mainly a tight loop using for/select that may not allow the other goroutine to ever get to send on the channel. Since the default case is empty and the select has only one case, the whole select is unnecessary. The following code works correctly:

backOffChan := make(chan struct{})
go func() {
    time.Sleep(1 * time.Millisecond)
    backOffChan <- struct{}{}
}()
for range backOffChan {
    d := time.Duration(10 * time.Millisecond)
    log.Printf("reconnecting in %s", d)
    select {
    case <-time.After(d):
        log.Printf("reconnected after %s", d)
        return
    }
}

This will wait until the backOffChan gets a message without burning a tight loop.

(Note that this code also addresses issues using log.Println with formatting directives - these were corrected to log.Printf).

See it in action here: https://play.golang.org/p/ksAzOq5ekrm