Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
K
koko
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
ops
koko
Commits
88f5215a
Commit
88f5215a
authored
May 20, 2019
by
Eric
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[update] 调整结构
parent
1461c7d1
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
70 deletions
+74
-70
coco.go
pkg/coco/coco.go
+1
-2
task.go
pkg/coco/task.go
+10
-56
recorder.go
pkg/proxy/recorder.go
+12
-12
sessiontask.go
pkg/proxy/sessiontask.go
+51
-0
No files found.
pkg/coco/coco.go
View file @
88f5215a
...
...
@@ -9,7 +9,6 @@ import (
"cocogo/pkg/config"
"cocogo/pkg/logger"
"cocogo/pkg/proxy"
"cocogo/pkg/service"
"cocogo/pkg/sshd"
)
...
...
@@ -45,5 +44,5 @@ func loadingBoot() {
config
.
Initial
()
logger
.
Initial
()
service
.
Initial
()
proxy
.
Initial
()
Initial
()
}
pkg/
proxy/init
.go
→
pkg/
coco/task
.go
View file @
88f5215a
package
proxy
package
coco
import
(
"os"
"path/filepath"
"strings"
"sync"
"time"
"cocogo/pkg/common"
"cocogo/pkg/config"
"cocogo/pkg/logger"
"cocogo/pkg/
model
"
"cocogo/pkg/
proxy
"
"cocogo/pkg/service"
)
var
sessionMap
=
make
(
map
[
string
]
*
SwitchSession
)
var
lock
=
new
(
sync
.
RWMutex
)
func
Initial
()
{
conf
:=
config
.
GetConf
()
if
conf
.
UploadFailedReplay
{
go
uploadFailedReplay
(
conf
.
RootPath
)
}
go
K
eepHeartbeat
(
conf
.
HeartbeatDuration
)
go
k
eepHeartbeat
(
conf
.
HeartbeatDuration
)
}
func
uploadFailedReplay
(
rootPath
string
)
{
...
...
@@ -43,10 +39,10 @@ func uploadFailedReplay(rootPath string) {
if
strings
.
HasSuffix
(
filename
,
".replay.gz"
)
{
sid
:=
strings
.
Split
(
filename
,
"."
)[
0
]
if
len
(
sid
)
==
36
{
relayRecord
:=
NewReplyRecord
(
sid
)
relayRecord
.
a
bsGzFilePath
=
path
relayRecord
.
t
arget
,
_
=
filepath
.
Rel
(
path
,
rootPath
)
go
relayRecord
.
u
ploadGzipFile
(
3
)
relayRecord
:=
proxy
.
NewReplyRecord
(
sid
)
relayRecord
.
A
bsGzFilePath
=
path
relayRecord
.
T
arget
,
_
=
filepath
.
Rel
(
path
,
rootPath
)
go
relayRecord
.
U
ploadGzipFile
(
3
)
}
}
return
nil
...
...
@@ -54,61 +50,19 @@ func uploadFailedReplay(rootPath string) {
logger
.
Debug
(
"upload Replay Done"
)
}
func
K
eepHeartbeat
(
interval
int
)
{
func
k
eepHeartbeat
(
interval
int
)
{
tick
:=
time
.
Tick
(
time
.
Duration
(
interval
)
*
time
.
Second
)
for
{
select
{
case
<-
tick
:
data
:=
GetAliveSessions
()
data
:=
proxy
.
GetAliveSessions
()
tasks
:=
service
.
TerminalHeartBeat
(
data
)
if
len
(
tasks
)
!=
0
{
for
_
,
task
:=
range
tasks
{
HandlerSessionTask
(
task
)
proxy
.
HandlerSessionTask
(
task
)
}
}
}
}
}
func
HandlerSessionTask
(
task
model
.
TerminalTask
)
{
switch
task
.
Name
{
case
"kill_session"
:
KillSession
(
task
.
Args
)
service
.
FinishTask
(
task
.
Id
)
default
:
}
}
func
KillSession
(
sessionID
string
)
{
lock
.
RLock
()
defer
lock
.
RUnlock
()
if
sw
,
ok
:=
sessionMap
[
sessionID
];
ok
{
sw
.
Terminate
()
}
}
func
GetAliveSessions
()
[]
string
{
lock
.
RLock
()
defer
lock
.
RUnlock
()
sids
:=
make
([]
string
,
0
,
len
(
sessionMap
))
for
sid
:=
range
sessionMap
{
sids
=
append
(
sids
,
sid
)
}
return
sids
}
func
RemoveSession
(
sw
*
SwitchSession
)
{
lock
.
Lock
()
defer
lock
.
Unlock
()
delete
(
sessionMap
,
sw
.
Id
)
}
func
AddSession
(
sw
*
SwitchSession
)
{
lock
.
Lock
()
defer
lock
.
Unlock
()
sessionMap
[
sw
.
Id
]
=
sw
}
pkg/proxy/recorder.go
View file @
88f5215a
...
...
@@ -95,8 +95,8 @@ type ReplyRecorder struct {
sessionID
string
absFilePath
string
a
bsGzFilePath
string
t
arget
string
A
bsGzFilePath
string
T
arget
string
file
*
os
.
File
timeStartNano
int64
...
...
@@ -126,8 +126,8 @@ func (r *ReplyRecorder) prepare() {
replayDir
:=
filepath
.
Join
(
rootPath
,
"data"
,
"replays"
,
today
)
r
.
absFilePath
=
filepath
.
Join
(
replayDir
,
sessionId
)
r
.
a
bsGzFilePath
=
filepath
.
Join
(
replayDir
,
gzFileName
)
r
.
t
arget
=
strings
.
Join
([]
string
{
today
,
gzFileName
},
"/"
)
r
.
A
bsGzFilePath
=
filepath
.
Join
(
replayDir
,
gzFileName
)
r
.
T
arget
=
strings
.
Join
([]
string
{
today
,
gzFileName
},
"/"
)
r
.
timeStartNano
=
time
.
Now
()
.
UnixNano
()
err
:=
common
.
EnsureDirExist
(
replayDir
)
...
...
@@ -160,21 +160,21 @@ func (r *ReplyRecorder) uploadReplay() {
_
=
os
.
Remove
(
r
.
absFilePath
)
return
}
if
!
common
.
FileExists
(
r
.
a
bsGzFilePath
)
{
if
!
common
.
FileExists
(
r
.
A
bsGzFilePath
)
{
logger
.
Debug
(
"Compress replay file: "
,
r
.
absFilePath
)
_
=
common
.
GzipCompressFile
(
r
.
absFilePath
,
r
.
a
bsGzFilePath
)
_
=
common
.
GzipCompressFile
(
r
.
absFilePath
,
r
.
A
bsGzFilePath
)
_
=
os
.
Remove
(
r
.
absFilePath
)
}
r
.
u
ploadGzipFile
(
3
)
r
.
U
ploadGzipFile
(
3
)
}
func
(
r
*
ReplyRecorder
)
u
ploadGzipFile
(
maxRetry
int
)
{
func
(
r
*
ReplyRecorder
)
U
ploadGzipFile
(
maxRetry
int
)
{
for
i
:=
0
;
i
<=
maxRetry
;
i
++
{
logger
.
Debug
(
"Upload replay file: "
,
r
.
a
bsGzFilePath
)
err
:=
r
.
storage
.
Upload
(
r
.
absGzFilePath
,
r
.
t
arget
)
logger
.
Debug
(
"Upload replay file: "
,
r
.
A
bsGzFilePath
)
err
:=
r
.
storage
.
Upload
(
r
.
AbsGzFilePath
,
r
.
T
arget
)
if
err
==
nil
{
_
=
os
.
Remove
(
r
.
a
bsGzFilePath
)
_
=
os
.
Remove
(
r
.
A
bsGzFilePath
)
break
}
// 如果还是失败,使用备用storage再传一次
...
...
@@ -184,7 +184,7 @@ func (r *ReplyRecorder) uploadGzipFile(maxRetry int) {
}
logger
.
Errorf
(
"Using back off storage retry upload"
)
r
.
storage
=
r
.
backOffStorage
r
.
u
ploadGzipFile
(
3
)
r
.
U
ploadGzipFile
(
3
)
break
}
}
...
...
pkg/proxy/sessiontask.go
0 → 100644
View file @
88f5215a
package
proxy
import
(
"cocogo/pkg/model"
"cocogo/pkg/service"
"sync"
)
var
sessionMap
=
make
(
map
[
string
]
*
SwitchSession
)
var
lock
=
new
(
sync
.
RWMutex
)
func
HandlerSessionTask
(
task
model
.
TerminalTask
)
{
switch
task
.
Name
{
case
"kill_session"
:
KillSession
(
task
.
Args
)
service
.
FinishTask
(
task
.
Id
)
default
:
}
}
func
KillSession
(
sessionID
string
)
{
lock
.
RLock
()
defer
lock
.
RUnlock
()
if
sw
,
ok
:=
sessionMap
[
sessionID
];
ok
{
sw
.
Terminate
()
}
}
func
GetAliveSessions
()
[]
string
{
lock
.
RLock
()
defer
lock
.
RUnlock
()
sids
:=
make
([]
string
,
0
,
len
(
sessionMap
))
for
sid
:=
range
sessionMap
{
sids
=
append
(
sids
,
sid
)
}
return
sids
}
func
RemoveSession
(
sw
*
SwitchSession
)
{
lock
.
Lock
()
defer
lock
.
Unlock
()
delete
(
sessionMap
,
sw
.
Id
)
}
func
AddSession
(
sw
*
SwitchSession
)
{
lock
.
Lock
()
defer
lock
.
Unlock
()
sessionMap
[
sw
.
Id
]
=
sw
}
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