0

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

nbari
  • 25,603
  • 10
  • 76
  • 131
  • 3
    Closing a channel to broadcast a singular event to an arbitrary number of parties is a common thing. (IMHO the use of struct{} is overly clever here.) – Volker Sep 17 '18 at 07:34
  • 1
    It's a sound approach and precisely how [context.Context](https://golang.org/pkg/context/#Context) works. – Peter Sep 17 '18 at 09:44

1 Answers1

1

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?

zdebra
  • 948
  • 8
  • 22