FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sourcegraph/internal/httpserver/server.go at main · Siddhant-K-code/sourcegraph · GitHub
Siddhant-K-code
/
sourcegraph
Public
forked from
sourcegraph/sourcegraph-public-snapshot
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
sourcegraph
/
internal
/
httpserver
/
server.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
80 lines (64 loc) · 2.03 KB
Breadcrumbs
sourcegraph
/
internal
/
httpserver
/
server.go
Copy path
File metadata and controls
80 lines (64 loc) · 2.03 KB
Raw
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
)
}
})
}
Back
|
FazBrowse Home
|
New Git URL