Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
80 lines (64 loc) · 2.03 KB

File metadata and controls

80 lines (64 loc) · 2.03 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package httpserver
import (
"context"
"net"
"net/http"
"os"
"sync"
"time"
"github.com/inconshreveable/log15"
"github.com/sourcegraph/sourcegraph/internal/goroutine"
)
type server struct {
server *http.Server
makeListener func() (net.Listener, error)
once sync.Once
preShutdownPause time.Duration
}
type ServerOptions func(s *server)
func WithPreShutdownPause(d time.Duration) ServerOptions {
return func(s *server) { s.preShutdownPause = d }
}
// New returns a BackgroundRoutine that serves the given server on the given listener.
func New(listener net.Listener, httpServer *http.Server, options ...ServerOptions) goroutine.BackgroundRoutine {
makeListener := func() (net.Listener, error) { return listener, nil }
return newServer(httpServer, makeListener, options...)
}
// NewFromAddr returns a BackgroundRoutine that serves the given handler on the given address.
func NewFromAddr(addr string, httpServer *http.Server, options ...ServerOptions) goroutine.BackgroundRoutine {
makeListener := func() (net.Listener, error) { return NewListener(addr) }
return newServer(httpServer, makeListener, options...)
}
func newServer(httpServer *http.Server, makeListener func() (net.Listener, error), options ...ServerOptions) goroutine.BackgroundRoutine {
s := &server{
server: httpServer,
makeListener: makeListener,
}
for _, option := range options {
option(s)
}
return s
}
func (s *server) Start() {
listener, err := s.makeListener()
if err != nil {
log15.Error("Failed to create listener", "error", err)
os.Exit(1)
}
if err := s.server.Serve(listener); err != http.ErrServerClosed {
log15.Error("Failed to start server", "error", err)
os.Exit(1)
}
}
func (s *server) Stop() {
s.once.Do(func() {
if s.preShutdownPause > 0 {
time.Sleep(s.preShutdownPause)
}
ctx, cancel := context.WithTimeout(context.Background(), goroutine.GracefulShutdownTimeout)
defer cancel()
if err := s.server.Shutdown(ctx); err != nil {
log15.Error("Failed to shutdown server", "error", err)
}
})
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.