mirror of
https://github.com/bingohuang/docker-labs.git
synced 2025-10-04 17:33:21 +08:00
Refactor sessions and add locking to avoid concurrent requests to create
more instances
This commit is contained in:
@@ -9,10 +9,10 @@ import (
|
||||
|
||||
func DeleteInstance(rw http.ResponseWriter, req *http.Request) {
|
||||
sessionId := bone.GetValue(req, "sessionId")
|
||||
instanceId := bone.GetValue(req, "instanceId")
|
||||
instanceName := bone.GetValue(req, "instanceName")
|
||||
|
||||
s := services.GetSession(sessionId)
|
||||
i := services.GetInstance(s, instanceId)
|
||||
i := services.GetInstance(s, instanceName)
|
||||
err := services.DeleteInstance(s, i)
|
||||
if err != nil {
|
||||
rw.WriteHeader(http.StatusInternalServerError)
|
||||
|
@@ -15,12 +15,12 @@ import (
|
||||
// Echo the data received on the WebSocket.
|
||||
func Exec(ws *websocket.Conn) {
|
||||
sessionId := bone.GetValue(ws.Request(), "sessionId")
|
||||
instanceId := bone.GetValue(ws.Request(), "instanceId")
|
||||
instanceName := bone.GetValue(ws.Request(), "instanceName")
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
session := services.GetSession(sessionId)
|
||||
instance := services.GetInstance(session, instanceId)
|
||||
instance := services.GetInstance(session, instanceName)
|
||||
|
||||
if instance.Stdout == nil {
|
||||
id, err := services.CreateExecConnection(instance.Name, ctx)
|
||||
|
@@ -14,12 +14,14 @@ func NewInstance(rw http.ResponseWriter, req *http.Request) {
|
||||
|
||||
s := services.GetSession(sessionId)
|
||||
|
||||
s.Lock()
|
||||
if len(s.Instances) >= 5 {
|
||||
rw.WriteHeader(http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
i, err := services.NewInstance(s)
|
||||
s.Unlock()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
//TODO: Set a status error
|
||||
|
Reference in New Issue
Block a user