blob: 973775e14ea0374073c0c59af15a908bdf1254e0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
package worker
import (
"context"
"net/http"
)
type ServerTask struct {
server *http.Server
}
func NewServerTask(server *http.Server) *ServerTask {
return &ServerTask{
server: server,
}
}
func (self *ServerTask) Start(ctx context.Context) error {
done := make(chan error)
go func() {
done <- self.server.ListenAndServe()
}()
select {
// if ListenAndServe error for something other than context.Canceled
//(e.g.: address already in use) it trigger done to return sonner with
// the return error
case err := <-done:
return err
// in case of context canceled it will manually trigger the server to
// shutdown, and return its error, which is most cases, but not limited, is
// context.Canceled.
case <-ctx.Done():
return self.server.Shutdown(ctx)
}
}
|