Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
haoqu.ma
/
gotty
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit 46e33887
authored
Oct 08, 2015
by
Iwasaki Yudai
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Ensure only one client can connect when `--once` is given
Using a mutex
1 parent
e1de07bc
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
19 additions
and
7 deletions
app/app.go
app/client_context.go
app/app.go
View file @
46e3388
...
...
@@ -25,6 +25,7 @@ import (
"github.com/gorilla/websocket"
"github.com/hashicorp/hcl"
"github.com/kr/pty"
"github.com/yudai/umutex"
)
type
InitMessage
struct
{
...
...
@@ -40,6 +41,8 @@ type App struct {
server
*
manners
.
GracefulServer
titleTemplate
*
template
.
Template
onceMutex
*
umutex
.
UnblockingMutex
}
type
Options
struct
{
...
...
@@ -104,6 +107,8 @@ func New(command []string, options *Options) (*App, error) {
},
titleTemplate
:
titleTemplate
,
onceMutex
:
umutex
.
New
(),
},
nil
}
...
...
@@ -314,6 +319,20 @@ func (app *App) handleWS(w http.ResponseWriter, r *http.Request) {
argv
=
append
(
argv
,
params
...
)
}
}
app
.
server
.
StartRoutine
()
if
app
.
options
.
Once
{
if
app
.
onceMutex
.
TryLock
()
{
// no unlock required, it will die soon
log
.
Printf
(
"Last client accepted, closing the listener."
)
app
.
server
.
Close
()
}
else
{
log
.
Printf
(
"Server is already closing."
)
conn
.
Close
()
return
}
}
cmd
:=
exec
.
Command
(
app
.
command
[
0
],
argv
...
)
ptyIo
,
err
:=
pty
.
Start
(
cmd
)
if
err
!=
nil
{
...
...
app/client_context.go
View file @
46e3388
...
...
@@ -65,13 +65,6 @@ func (context *clientContext) goHandleClient() {
context
.
processReceive
()
}()
context
.
app
.
server
.
StartRoutine
()
if
context
.
app
.
options
.
Once
{
log
.
Printf
(
"Last client accepted, closing the listener."
)
context
.
app
.
server
.
Close
()
}
go
func
()
{
defer
context
.
app
.
server
.
FinishRoutine
()
...
...
Write
Preview
Markdown
is supported
Attach a file
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 post a comment