forked from bp-alex/ibconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
34 lines (27 loc) · 711 Bytes
/
server.go
File metadata and controls
34 lines (27 loc) · 711 Bytes
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
package server
import (
"net"
"net/http"
"time"
"github.com/etix/stoppableListener"
)
// Serve executes a HTTP server and blocks until the passed channel closes.
func Serve(terminated <-chan struct{}, address string, handler http.Handler) error {
listener, err := net.Listen("tcp", address)
stoppable := stoppableListener.Handle(listener)
go func() {
select {
case <-terminated:
stoppable.Stop <- true
}
}()
err = http.Serve(stoppable, handler)
if !stoppable.Stopped && err != nil {
return err
}
// Give up to 5 seconds for in-flight requests to complete
killAt := time.Now().Add(5 * time.Second)
for time.Now().Before(killAt) && stoppable.ConnCount.Get() > 0 {
}
return nil
}