Skip to content

Commit

Permalink
perf: fix share session pause action
Browse files Browse the repository at this point in the history
  • Loading branch information
LeeEirc committed Jul 2, 2024
1 parent 914864f commit 9159181
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 3 deletions.
4 changes: 3 additions & 1 deletion pkg/tunnel/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,5 +114,7 @@ const (
ShareExit = "share_exit"
ShareUsers = "share_users"

ShareRemoveUser = "share_remove_user"
ShareRemoveUser = "share_remove_user"
ShareSessionPause = "share_session_pause"
ShareSessionResume = "share_session_resume"
)
20 changes: 19 additions & 1 deletion pkg/tunnel/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ func (t *Connection) HandleTask(task *model.TerminalTask) error {
p, _ := json.Marshal(data)
ins := NewJmsEventInstruction("session_resume", string(p))
_ = t.SendWsMessage(ins)
t.notifySessionAction(ShareSessionResume, task.Kwargs.CreatedByUser)
case model.TaskLockSession:
t.lockedStatus.Store(true)
t.operatorUser.Store(task.Kwargs.CreatedByUser)
Expand All @@ -368,6 +369,7 @@ func (t *Connection) HandleTask(task *model.TerminalTask) error {
p, _ := json.Marshal(data)
ins := NewJmsEventInstruction("session_pause", string(p))
_ = t.SendWsMessage(ins)
t.notifySessionAction(ShareSessionPause, task.Kwargs.CreatedByUser)
case model.TaskKillSession:
t.recordStatus.Store(true)
username := task.Kwargs.TerminatedBy
Expand Down Expand Up @@ -487,6 +489,10 @@ func (t *Connection) handleEvent(eventMsg *Event) {
logger.Info("Ignore self join event")
return
}
if t.lockedStatus.Load() {
user := t.operatorUser.Load().(string)
defer t.notifySessionAction(ShareSessionPause, user)
}
case ShareExit:
var meta MetaShareUserMessage
if err := json.Unmarshal(eventMsg.Data, &meta); err != nil {
Expand All @@ -499,12 +505,15 @@ func (t *Connection) handleEvent(eventMsg *Event) {
t.traceLock.Unlock()
defer t.notifyShareUsers()
case ShareUsers:
case ShareRemoveUser:
case ShareRemoveUser,
ShareSessionPause,
ShareSessionResume:
return
}
inst := NewJmsEventInstruction(eventMsg.Type, string(eventMsg.Data))
_ = t.SendWsMessage(inst)
}

func (t *Connection) notifyShareUsers() {
t.traceLock.Lock()
body, _ := json.Marshal(t.currentOnlineUsers)
Expand All @@ -514,3 +523,12 @@ func (t *Connection) notifyShareUsers() {
Data: body,
})
}

func (t *Connection) notifySessionAction(action string, user string) {
data := map[string]interface{}{
"user": user,
}
p, _ := json.Marshal(data)
t.Cache.BroadcastSessionEvent(t.Sess.ID,
&Event{Type: action, Data: p})
}
26 changes: 26 additions & 0 deletions pkg/tunnel/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"strings"
"sync"
"sync/atomic"

"github.com/gorilla/websocket"

Expand All @@ -25,6 +26,8 @@ type MonitorCon struct {
Service *GuacamoleTunnelServer
User *model.User
Meta *MetaShareUserMessage

lockedStatus atomic.Bool
}

func (m *MonitorCon) SendWsMessage(msg guacd.Instruction) error {
Expand Down Expand Up @@ -105,6 +108,25 @@ func (m *MonitorCon) Run(ctx context.Context) (err error) {
}
continue
}
if t.lockedStatus.Load() {
switch ret.Opcode {
case guacd.InstructionClientSync,
guacd.InstructionClientNop,
guacd.InstructionStreamingAck:
default:
logger.Infof("Session[%s] in locked status drop receive web client message opcode[%s]",
t.Id, ret.Opcode)
continue
}
_, err4 := t.writeTunnelMessage(message)
if err4 != nil {
logger.Errorf("Session[%s] guacamole server write err: %+v", t.Id, err2)
exit <- err4
break
}
logger.Debugf("Session[%s] send guacamole server message when locked status", t.Id)
continue
}
} else {
logger.Errorf("Monitor[%s] parse instruction err %s", t.Id, err2)
}
Expand Down Expand Up @@ -165,6 +187,10 @@ func (m *MonitorCon) handleEvent(eventMsg *Event) {
}
errInst := NewJMSGuacamoleError(1011, removeData.User)
inst = errInst.Instruction()
case ShareSessionPause, ShareSessionResume:
inst = NewJmsEventInstruction(eventMsg.Type, string(eventMsg.Data))
locked := eventMsg.Type == ShareSessionPause
m.lockedStatus.Store(locked)
default:
return
}
Expand Down
18 changes: 17 additions & 1 deletion ui/src/components/GuacamoleShare.vue
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export default {
sessionId: '',
onlineUsersMap: {},
share_id: '',
recordId: ''
recordId: '',
locked: false
}
},
computed: {
Expand Down Expand Up @@ -124,6 +125,21 @@ export default {
this.onlineUsersMap = dataObj
break
}
case 'share_session_pause': {
if (this.locked) {
break
}
const msg = `${dataObj.user} ${this.$t('PauseSession')}`
this.$message.info(msg)
this.locked = true
break
}
case 'share_session_resume': {
const msg = `${dataObj.user} ${this.$t('ResumeSession')}`
this.$message.info(msg)
this.locked = false
break
}
}
}
}
Expand Down

0 comments on commit 9159181

Please sign in to comment.