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
1e0ac263
Commit
1e0ac263
authored
Jun 09, 2019
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Imp: just treat task pool as a independent role
parent
ac39a3ba
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
69 additions
and
114 deletions
+69
-114
client.go
client.go
+0
-12
options.go
options.go
+49
-42
server.go
server.go
+0
-14
session.go
session.go
+6
-7
task_pool.go
task_pool.go
+14
-39
No files found.
client.go
View file @
1e0ac263
...
...
@@ -51,9 +51,6 @@ type client struct {
newSession
NewSessionCallback
ssMap
map
[
Session
]
struct
{}
// task queue pool
tQPool
*
taskPool
sync
.
Once
done
chan
struct
{}
wg
sync
.
WaitGroup
...
...
@@ -79,10 +76,6 @@ func newClient(t EndPointType, opts ...ClientOption) *client {
c
.
ssMap
=
make
(
map
[
Session
]
struct
{},
c
.
number
)
if
c
.
tQPoolSize
>
0
{
c
.
tQPool
=
newTaskPool
(
c
.
taskPoolOptions
)
}
return
c
}
...
...
@@ -357,7 +350,6 @@ func (c *client) connect() {
}
err
=
c
.
newSession
(
ss
)
if
err
==
nil
{
ss
.
(
*
session
)
.
SetTaskPool
(
c
.
tQPool
)
ss
.
(
*
session
)
.
run
()
c
.
Lock
()
if
c
.
ssMap
==
nil
{
...
...
@@ -429,10 +421,6 @@ func (c *client) stop() {
}
c
.
ssMap
=
nil
if
c
.
tQPool
!=
nil
{
c
.
tQPool
.
close
()
c
.
tQPool
=
nil
}
c
.
Unlock
()
})
}
...
...
options.go
View file @
1e0ac263
...
...
@@ -10,6 +10,10 @@
package
getty
import
(
"fmt"
)
/////////////////////////////////////////
// Server Options
/////////////////////////////////////////
...
...
@@ -19,9 +23,6 @@ type ServerOption func(*ServerOptions)
type
ServerOptions
struct
{
addr
string
// task pool
taskPoolOptions
// websocket
path
string
cert
string
...
...
@@ -36,27 +37,6 @@ func WithLocalAddress(addr string) ServerOption {
}
}
// @size is the task queue pool size
func
WithServerTaskPoolSize
(
size
int32
)
ServerOption
{
return
func
(
o
*
ServerOptions
)
{
o
.
taskPoolOptions
.
tQPoolSize
=
size
}
}
// @length is the task queue length
func
WithServerTaskQueueLength
(
length
int32
)
ServerOption
{
return
func
(
o
*
ServerOptions
)
{
o
.
taskPoolOptions
.
tQLen
=
length
}
}
// @number is the task queue number
func
WithServerTaskQueueNumber
(
number
int32
)
ServerOption
{
return
func
(
o
*
ServerOptions
)
{
o
.
taskPoolOptions
.
tQNumber
=
number
}
}
// @path: websocket request url path
func
WithWebsocketServerPath
(
path
string
)
ServerOption
{
return
func
(
o
*
ServerOptions
)
{
...
...
@@ -96,9 +76,6 @@ type ClientOptions struct {
number
int
reconnectInterval
int
// reConnect Interval
// task pool
taskPoolOptions
// the cert file of wss server which may contain server domain, server ip, the starting effective date, effective
// duration, the hash alg, the len of the private key.
// wss client will use it.
...
...
@@ -126,30 +103,60 @@ func WithConnectionNumber(num int) ClientOption {
}
}
// @
size is the task queue pool size
func
With
ClientTaskPoolSize
(
size
int32
)
ClientOption
{
// @
cert is client certificate file. it can be empty.
func
With
RootCertificateFile
(
cert
string
)
ClientOption
{
return
func
(
o
*
ClientOptions
)
{
o
.
taskPoolOptions
.
tQPoolSize
=
size
o
.
cert
=
cert
}
}
// @length is the task queue length
func
WithClientTaskQueueLength
(
length
int32
)
ClientOption
{
return
func
(
o
*
ClientOptions
)
{
o
.
taskPoolOptions
.
tQLen
=
length
/////////////////////////////////////////
// Task Pool Options
/////////////////////////////////////////
type
TaskPoolOptions
struct
{
tQLen
int
// task queue length
tQNumber
int
// task queue number
tQPoolSize
int
// task pool size
}
func
(
o
*
TaskPoolOptions
)
validate
()
{
if
o
.
tQPoolSize
==
0
{
panic
(
fmt
.
Sprintf
(
"[getty][task_pool] illegal pool size %d"
,
o
.
tQPoolSize
))
}
if
o
.
tQLen
==
0
{
o
.
tQLen
=
defaultTaskQLen
}
if
o
.
tQNumber
<
1
{
o
.
tQNumber
=
defaultTaskQNumber
}
if
o
.
tQNumber
>
o
.
tQPoolSize
{
o
.
tQNumber
=
o
.
tQPoolSize
}
}
// @number is the task queue number
func
WithClientTaskQueueNumber
(
number
int32
)
ClientOption
{
return
func
(
o
*
ClientOptions
)
{
o
.
taskPoolOptions
.
tQNumber
=
number
type
TaskPoolOption
func
(
*
TaskPoolOptions
)
// @size is the task queue pool size
func
WithTaskPoolTaskPoolSize
(
size
int
)
TaskPoolOption
{
return
func
(
o
*
TaskPoolOptions
)
{
o
.
tQPoolSize
=
size
}
}
// @cert is client certificate file. it can be empty.
func
WithRootCertificateFile
(
cert
string
)
ClientOption
{
return
func
(
o
*
ClientOptions
)
{
o
.
cert
=
cert
// @length is the task queue length
func
WithTaskPoolTaskQueueLength
(
length
int
)
TaskPoolOption
{
return
func
(
o
*
TaskPoolOptions
)
{
o
.
tQLen
=
length
}
}
// @number is the task queue number
func
WithTaskPoolTaskQueueNumber
(
number
int
)
TaskPoolOption
{
return
func
(
o
*
TaskPoolOptions
)
{
o
.
tQNumber
=
number
}
}
server.go
View file @
1e0ac263
...
...
@@ -41,9 +41,6 @@ type server struct {
endPointType
EndPointType
server
*
http
.
Server
// for ws or wss server
// task queue pool
tQPool
*
taskPool
sync
.
Once
done
chan
struct
{}
wg
sync
.
WaitGroup
...
...
@@ -67,10 +64,6 @@ func newServer(t EndPointType, opts ...ServerOption) *server {
panic
(
fmt
.
Sprintf
(
"@addr:%s"
,
s
.
addr
))
}
if
s
.
tQPoolSize
>
0
{
s
.
tQPool
=
newTaskPool
(
s
.
taskPoolOptions
)
}
return
s
}
...
...
@@ -137,10 +130,6 @@ func (s *server) stop() {
s
.
pktListener
.
Close
()
s
.
pktListener
=
nil
}
if
s
.
tQPool
!=
nil
{
s
.
tQPool
.
close
()
s
.
tQPool
=
nil
}
})
}
}
...
...
@@ -263,7 +252,6 @@ func (s *server) runTcpEventLoop(newSession NewSessionCallback) {
continue
}
delay
=
0
client
.
(
*
session
)
.
SetTaskPool
(
s
.
tQPool
)
client
.
(
*
session
)
.
run
()
}
}()
...
...
@@ -278,7 +266,6 @@ func (s *server) runUDPEventLoop(newSession NewSessionCallback) {
if
err
:=
newSession
(
ss
);
err
!=
nil
{
panic
(
err
.
Error
())
}
ss
.
(
*
session
)
.
SetTaskPool
(
s
.
tQPool
)
ss
.
(
*
session
)
.
run
()
}
...
...
@@ -335,7 +322,6 @@ func (s *wsHandler) serveWSRequest(w http.ResponseWriter, r *http.Request) {
if
ss
.
(
*
session
)
.
maxMsgLen
>
0
{
conn
.
SetReadLimit
(
int64
(
ss
.
(
*
session
)
.
maxMsgLen
))
}
ss
.
(
*
session
)
.
SetTaskPool
(
s
.
server
.
tQPool
)
ss
.
(
*
session
)
.
run
()
}
...
...
session.go
View file @
1e0ac263
...
...
@@ -63,7 +63,7 @@ type session struct {
// handle logic
maxMsgLen
int32
// task queue
t
QPool
*
t
askPool
t
Pool
*
T
askPool
// heartbeat
period
time
.
Duration
...
...
@@ -89,7 +89,6 @@ func newSession(endPoint EndPoint, conn Connection) *session {
Connection
:
conn
,
maxMsgLen
:
maxReadBufLen
,
tQLen
:
defaultTaskQLen
,
period
:
period
,
...
...
@@ -302,11 +301,11 @@ func (s *session) SetWaitTime(waitTime time.Duration) {
}
// set task pool
func
(
s
*
session
)
SetTaskPool
(
p
*
t
askPool
)
{
func
(
s
*
session
)
SetTaskPool
(
p
*
T
askPool
)
{
s
.
lock
.
Lock
()
defer
s
.
lock
.
Unlock
()
s
.
t
Q
Pool
=
p
s
.
tPool
=
p
}
// set attribute of key @session:key
...
...
@@ -457,7 +456,7 @@ func (s *session) run() {
s
.
wQ
=
make
(
chan
interface
{},
defaultQLen
)
}
if
s
.
rQ
==
nil
&&
s
.
t
Q
Pool
==
nil
{
if
s
.
rQ
==
nil
&&
s
.
tPool
==
nil
{
s
.
rQ
=
make
(
chan
interface
{},
defaultQLen
)
}
...
...
@@ -564,8 +563,8 @@ LOOP:
}
func
(
s
*
session
)
addTask
(
pkg
interface
{})
{
if
s
.
t
Q
Pool
!=
nil
{
s
.
t
Q
Pool
.
AddTask
(
task
{
session
:
s
,
pkg
:
pkg
})
if
s
.
tPool
!=
nil
{
s
.
tPool
.
AddTask
(
task
{
session
:
s
,
pkg
:
pkg
})
}
else
{
s
.
rQ
<-
pkg
}
...
...
task_pool.go
View file @
1e0ac263
package
getty
import
(
"fmt"
"sync"
"sync/atomic"
)
...
...
@@ -17,33 +16,9 @@ type task struct {
pkg
interface
{}
}
type
taskPoolOptions
struct
{
tQLen
int32
// task queue length
tQNumber
int32
// task queue number
tQPoolSize
int32
// task pool size
}
func
(
o
*
taskPoolOptions
)
Validate
()
{
if
o
.
tQPoolSize
==
0
{
panic
(
fmt
.
Sprintf
(
"[getty][task_pool] illegal pool size %d"
,
o
.
tQPoolSize
))
}
if
o
.
tQLen
==
0
{
o
.
tQLen
=
defaultTaskQLen
}
if
o
.
tQNumber
<
1
{
o
.
tQNumber
=
defaultTaskQNumber
}
if
o
.
tQNumber
>
o
.
tQPoolSize
{
o
.
tQNumber
=
o
.
tQPoolSize
}
}
// task pool: manage task ts
type
t
askPool
struct
{
t
askPoolOptions
type
T
askPool
struct
{
T
askPoolOptions
idx
uint32
// round robin index
qArray
[]
chan
task
...
...
@@ -54,16 +29,16 @@ type taskPool struct {
}
// build a task pool
func
newTaskPool
(
opts
taskPoolOptions
)
*
t
askPool
{
opts
.
V
alidate
()
func
newTaskPool
(
opts
TaskPoolOptions
)
*
T
askPool
{
opts
.
v
alidate
()
p
:=
&
t
askPool
{
t
askPoolOptions
:
opts
,
p
:=
&
T
askPool
{
T
askPoolOptions
:
opts
,
qArray
:
make
([]
chan
task
,
opts
.
tQNumber
),
done
:
make
(
chan
struct
{}),
}
for
i
:=
int32
(
0
)
;
i
<
p
.
tQNumber
;
i
++
{
for
i
:=
0
;
i
<
p
.
tQNumber
;
i
++
{
p
.
qArray
[
i
]
=
make
(
chan
task
,
p
.
tQLen
)
}
...
...
@@ -71,8 +46,8 @@ func newTaskPool(opts taskPoolOptions) *taskPool {
}
// start task pool
func
(
p
*
t
askPool
)
start
()
{
for
i
:=
int32
(
0
)
;
i
<
p
.
tQPoolSize
;
i
++
{
func
(
p
*
T
askPool
)
start
()
{
for
i
:=
0
;
i
<
p
.
tQPoolSize
;
i
++
{
p
.
wg
.
Add
(
1
)
workerID
:=
i
q
:=
p
.
qArray
[
workerID
%
p
.
tQNumber
]
...
...
@@ -81,7 +56,7 @@ func (p *taskPool) start() {
}
// worker
func
(
p
*
t
askPool
)
run
(
id
int
,
q
chan
task
)
{
func
(
p
*
T
askPool
)
run
(
id
int
,
q
chan
task
)
{
defer
p
.
wg
.
Done
()
var
(
...
...
@@ -109,7 +84,7 @@ func (p *taskPool) run(id int, q chan task) {
}
// add task
func
(
p
*
t
askPool
)
AddTask
(
t
task
)
{
func
(
p
*
T
askPool
)
AddTask
(
t
task
)
{
id
:=
atomic
.
AddUint32
(
&
p
.
idx
,
1
)
%
defaultTaskQNumber
select
{
...
...
@@ -120,7 +95,7 @@ func (p *taskPool) AddTask(t task) {
}
// stop all tasks
func
(
p
*
t
askPool
)
stop
()
{
func
(
p
*
T
askPool
)
stop
()
{
select
{
case
<-
p
.
done
:
return
...
...
@@ -132,7 +107,7 @@ func (p *taskPool) stop() {
}
// check whether the session has been closed.
func
(
p
*
t
askPool
)
isClosed
()
bool
{
func
(
p
*
T
askPool
)
isClosed
()
bool
{
select
{
case
<-
p
.
done
:
return
true
...
...
@@ -142,7 +117,7 @@ func (p *taskPool) isClosed() bool {
}
}
func
(
p
*
t
askPool
)
close
()
{
func
(
p
*
T
askPool
)
close
()
{
p
.
stop
()
p
.
wg
.
Wait
()
for
i
:=
range
p
.
qArray
{
...
...
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