doujiang1913 2018-09-17 07:30
浏览 39
已采纳

close(channel)用于实现观察者模式

I need to stop an HTTP server on demand besides calling other functions as well when receiving the "quit" signal in no specific order.

In my try to implement something like the observer pattern, I found "handy" to create a channel (quit := make(chan struct{}), let's say the "subject" and then on each of the goroutines "observers" listen on that channel <-quit waiting until a change for then to continue.

The way I trigger all the functions at once is by closing the channel close(quit) not by writing into it, I have tried this and so far working, but wondering if there are some cons with this approach or if there are better/idiomatic ways of implementing similar behavior/pattern.

package main

import (
    "log"
    "net/http"
    "sync"
    "time"
)

func main() {
    var wg sync.WaitGroup

    srv := &http.Server{Addr: ":8080"}

    wg.Add(1)
    go func() {
        log.Println(srv.ListenAndServe())
        wg.Done()
    }()

    quit := make(chan struct{})
    go func() {
        <-quit
        if err := srv.Close(); err != nil {
            log.Printf("HTTP server Shutdown: %v", err)
        }
    }()

    wg.Add(1)
    go func() {
        <-quit
        log.Println("just waiting 1")
        wg.Done()
    }()

    wg.Add(1)
    go func() {
        <-quit
        log.Println("just waiting 2")
        wg.Done()
    }()

    <-time.After(2 * time.Second)
    close(quit)
    wg.Wait()
}

https://play.golang.org/p/uIfMJfN6xQy

  • 写回答

1条回答 默认 最新

  • doulu8341 2018-09-17 07:37
    关注

    I would say your way is good enough but lacks some elegance.

    You could implement required behavior using sync.Cond:

    https://golang.org/pkg/sync/#Cond
    How to correctly use sync.Cond?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?