Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
getty
Commits
5d4283aa
Commit
5d4283aa
authored
Feb 08, 2017
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add server.Shutdown in (Server)stop
parent
4cf247e1
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
43 additions
and
9 deletions
+43
-9
change_log.md
change_log.md
+10
-3
server.go
server.go
+30
-3
version.go
version.go
+3
-3
No files found.
change_log.md
View file @
5d4283aa
...
...
@@ -11,6 +11,13 @@
## develop history ##
---
-
2017/02/08
> improvement
>
> 1 server shutdown in server.go:(Server) stop()
>
> 2 version: 0.7.01
-
2017/02/03
> 1 Session struct -> session struct and add Session interface
>
...
...
@@ -23,12 +30,12 @@
-
2016/11/19
> 1 add conn.go:(*gettyWSConn) setCompressType to add zip compress feature for ws connection
>
> 2 version: 0.6.2
> 2 version: 0.6.
0
2
-
2016/11/16
> 1 add zip/snappy compress for tcp connection
>
> 2 version: 0.6.1
> 2 version: 0.6.
0
1
-
2016/11/02
> 1 add session.go:Session{ID(), LocalAddr(), RemoteAddr()}
...
...
@@ -217,7 +224,7 @@
>
> 5 use err instead of nerr in session.go:(Session)handlePackage:defer:OnError
>
> 6 version: 0.2.07
> 6 version: 0.2.07
-
2016/08/25
> 1 move close done to once clause in server.go:(Server)stop
...
...
server.go
View file @
5d4283aa
...
...
@@ -10,6 +10,7 @@
package
getty
import
(
"context"
"crypto/tls"
"errors"
"net"
...
...
@@ -21,18 +22,22 @@ import (
import
(
"github.com/AlexStocks/goext/net"
"github.com/AlexStocks/goext/sync"
"github.com/AlexStocks/goext/time"
log
"github.com/AlexStocks/log4go"
"github.com/gorilla/websocket"
)
var
(
errSelfConnect
=
errors
.
New
(
"connect self!"
)
errSelfConnect
=
errors
.
New
(
"connect self!"
)
serverFastFailTimeout
=
gxtime
.
TimeSecondDuration
(
2
)
)
type
Server
struct
{
// net
addr
string
listener
net
.
Listener
lock
sync
.
Mutex
// for server
server
*
http
.
Server
// for ws or wss server
sync
.
Once
done
chan
gxsync
.
Empty
...
...
@@ -44,12 +49,26 @@ func NewServer() *Server {
}
func
(
s
*
Server
)
stop
()
{
var
(
err
error
ctx
context
.
Context
)
select
{
case
<-
s
.
done
:
return
default
:
s
.
Once
.
Do
(
func
()
{
close
(
s
.
done
)
s
.
lock
.
Lock
()
if
s
.
server
!=
nil
{
ctx
,
_
=
context
.
WithTimeout
(
context
.
Background
(),
serverFastFailTimeout
)
if
err
=
s
.
server
.
Shutdown
(
ctx
);
err
!=
nil
{
// 如果下面内容输出为:server shutdown ctx: context deadline exceeded,
// 则说明有未处理完的active connections。
log
.
Error
(
"server shutdown ctx:%#v"
,
err
)
}
}
s
.
lock
.
Unlock
()
// 把listener.Close放在这里,既能防止多次关闭调用,
// 又能及时让Server因accept返回错误而从RunEventloop退出
s
.
listener
.
Close
()
...
...
@@ -196,15 +215,20 @@ func (s *Server) RunWSEventLoop(newSession NewSessionCallback, path string) {
var
(
err
error
handler
*
wsHandler
server
*
http
.
Server
)
handler
=
newWSHandler
(
s
,
newSession
)
handler
.
HandleFunc
(
path
,
handler
.
serveWSRequest
)
err
=
(
&
http
.
Server
{
server
=
&
http
.
Server
{
Addr
:
s
.
addr
,
Handler
:
handler
,
// ReadTimeout: server.HTTPTimeout,
// WriteTimeout: server.HTTPTimeout,
})
.
Serve
(
s
.
listener
)
}
s
.
lock
.
Lock
()
s
.
server
=
server
s
.
lock
.
Unlock
()
err
=
server
.
Serve
(
s
.
listener
)
if
err
!=
nil
{
log
.
Error
(
"http.Server.Serve(addr{%s}) = err{%#v}"
,
s
.
addr
,
err
)
// panic(err)
...
...
@@ -242,6 +266,9 @@ func (s *Server) RunWSEventLoopWithTLS(newSession NewSessionCallback, path strin
// WriteTimeout: server.HTTPTimeout,
}
server
.
SetKeepAlivesEnabled
(
true
)
s
.
lock
.
Lock
()
s
.
server
=
server
s
.
lock
.
Unlock
()
err
=
server
.
Serve
(
tls
.
NewListener
(
s
.
listener
,
config
))
if
err
!=
nil
{
log
.
Error
(
"http.Server.Serve(addr{%s}) = err{%#v}"
,
s
.
addr
,
err
)
...
...
version.go
View file @
5d4283aa
...
...
@@ -10,9 +10,9 @@
package
getty
const
(
Version
=
"0.7.0"
DATE
=
"2017/02/0
3
"
Version
=
"0.7.0
1
"
DATE
=
"2017/02/0
8
"
GETTY_MAJOR
=
0
GETTY_MINOR
=
7
GETTY_BUILD
=
0
GETTY_BUILD
=
1
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment