Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
G
go-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ai3
go-common
Commits
964a40e5
Commit
964a40e5
authored
7 years ago
by
ale
Browse files
Options
Downloads
Patches
Plain Diff
Build the HTTP server in a separate function
Reduces cyclomatic complexity score and makes code more readable.
parent
a174aa61
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
serverutil/http.go
+37
-17
37 additions, 17 deletions
serverutil/http.go
with
37 additions
and
17 deletions
serverutil/http.go
+
37
−
17
View file @
964a40e5
...
...
@@ -20,43 +20,63 @@ var gracefulShutdownTimeout = 3 * time.Second
type
ServerConfig
struct
{
TLS
*
TLSServerConfig
`yaml:"tls"`
MaxInflightRequests
int
`yaml:"max_inflight_requests"`
TrustedForwarders
[]
string
`yaml:"trusted_forwarders"`
}
// Serve HTTP(S) content on the specified address. If serverConfig is
// not nil, enable HTTPS and TLS authentication.
//
// This function will return an error if there are problems creating
// the listener, otherwise it will handle graceful termination on
// SIGINT or SIGTERM and return nil.
func
Serve
(
h
http
.
Handler
,
serverConfig
*
ServerConfig
,
addr
string
)
(
err
error
)
{
func
(
config
*
ServerConfig
)
buildHTTPServer
(
h
http
.
Handler
,
addr
string
)
(
*
http
.
Server
,
error
)
{
var
tlsConfig
*
tls
.
Config
if
serverConfig
!=
nil
{
if
serverConfig
.
TLS
!=
nil
{
tlsConfig
,
err
=
serverConfig
.
TLS
.
TLSConfig
()
var
err
error
if
config
!=
nil
{
if
config
.
TLS
!=
nil
{
tlsConfig
,
err
=
config
.
TLS
.
TLSConfig
()
if
err
!=
nil
{
return
err
return
nil
,
err
}
h
,
err
=
serverConfig
.
TLS
.
TLSAuthWrapper
(
h
)
h
,
err
=
config
.
TLS
.
TLSAuthWrapper
(
h
)
if
err
!=
nil
{
return
nil
,
err
}
}
// If TrustedForwarders is defined, rewrite the request
// headers using X-Forwarded-Proto and X-Real-IP.
if
len
(
config
.
TrustedForwarders
)
>
0
{
h
,
err
=
newProxyHeaders
(
h
,
config
.
TrustedForwarders
)
if
err
!=
nil
{
return
err
return
nil
,
err
}
}
if
serverConfig
.
MaxInflightRequests
>
0
{
h
=
newLoadSheddingWrapper
(
serverConfig
.
MaxInflightRequests
,
h
)
// If MaxInflightRequests is set, enable the load
// shedding wrapper.
if
config
.
MaxInflightRequests
>
0
{
h
=
newLoadSheddingWrapper
(
config
.
MaxInflightRequests
,
h
)
}
}
// These are not meant to be external-facing servers, so we
// can be generous with the timeouts to keep the number of
// reconnections low.
srv
:=
&
http
.
Server
{
return
&
http
.
Server
{
Addr
:
addr
,
Handler
:
instrumentHandler
(
h
),
ReadTimeout
:
30
*
time
.
Second
,
WriteTimeout
:
30
*
time
.
Second
,
IdleTimeout
:
600
*
time
.
Second
,
TLSConfig
:
tlsConfig
,
},
nil
}
// Serve HTTP(S) content on the specified address. If config.TLS is
// not nil, enable HTTPS and TLS authentication.
//
// This function will return an error if there are problems creating
// the listener, otherwise it will handle graceful termination on
// SIGINT or SIGTERM and return nil.
func
Serve
(
h
http
.
Handler
,
config
*
ServerConfig
,
addr
string
)
error
{
srv
,
err
:=
config
.
buildHTTPServer
(
h
,
addr
)
if
err
!=
nil
{
return
err
}
// Install a signal handler for gentle process termination.
...
...
@@ -80,7 +100,7 @@ func Serve(h http.Handler, serverConfig *ServerConfig, addr string) (err error)
}()
signal
.
Notify
(
sigCh
,
syscall
.
SIGINT
,
syscall
.
SIGTERM
)
if
tls
Config
!=
nil
{
if
srv
.
TLS
Config
!=
nil
{
err
=
srv
.
ListenAndServeTLS
(
""
,
""
)
}
else
{
err
=
srv
.
ListenAndServe
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment