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
5c61fa37
Commit
5c61fa37
authored
May 20, 2019
by
Eric
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[Update] Heartbeat and upload replay gzip file
parent
b8d1f098
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
112 additions
and
0 deletions
+112
-0
init.go
pkg/proxy/init.go
+112
-0
No files found.
pkg/proxy/init.go
0 → 100644
View file @
5c61fa37
package
proxy
import
(
"os"
"path/filepath"
"strings"
"sync"
"time"
"cocogo/pkg/common"
"cocogo/pkg/config"
"cocogo/pkg/logger"
"cocogo/pkg/model"
"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
KeepHeartbeat
(
conf
.
HeartbeatDuration
)
}
func
uploadFailedReplay
(
rootPath
string
)
{
replayDir
:=
filepath
.
Join
(
rootPath
,
"data"
,
"replays"
)
err
:=
common
.
EnsureDirExist
(
replayDir
)
if
err
!=
nil
{
logger
.
Debugf
(
"upload failed replay err: %s"
,
err
.
Error
())
return
}
_
=
filepath
.
Walk
(
replayDir
,
func
(
path
string
,
info
os
.
FileInfo
,
err
error
)
error
{
if
err
!=
nil
||
info
.
IsDir
()
{
return
nil
}
filename
:=
info
.
Name
()
if
strings
.
HasSuffix
(
filename
,
".replay.gz"
)
{
sid
:=
strings
.
Split
(
filename
,
"."
)[
0
]
if
len
(
sid
)
==
36
{
relayRecord
:=
NewReplyRecord
(
sid
)
go
relayRecord
.
uploadGzipFile
(
3
)
}
}
return
nil
})
logger
.
Debug
(
"upload Failed Replay Done"
)
}
func
KeepHeartbeat
(
interval
int
)
{
tick
:=
time
.
Tick
(
time
.
Duration
(
interval
)
*
time
.
Second
)
for
{
select
{
case
<-
tick
:
data
:=
GetAliveSessions
()
tasks
:=
service
.
TerminalHeartBeat
(
data
)
if
len
(
tasks
)
!=
0
{
for
_
,
task
:=
range
tasks
{
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
}
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