Commit 53551f2b authored by Kevin Di Lallo's avatar Kevin Di Lallo
Browse files

max session count support

parent d8a6a55a
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -52,7 +52,7 @@ spec:
          env:
            {{- range $key, $value := .Values.image.env }}
            - name: {{ $key }}
              value: {{ $value }}
              value: {{ $value | quote }}
            {{- end }}
            {{- if .Values.user.frontend.enabled}}
            - name: USER_FRONTEND
+1 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ image:
  pullPolicy: Always
  env:
    MEEP_SESSION_KEY: "my-secret-key"
    MEEP_MAX_SESSIONS: "10"

service:
  type: ClusterIP
+9 −0
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import (
	"io/ioutil"
	"math/rand"
	"net/http"
	"os"
	"strconv"
	"time"

	"github.com/gorilla/mux"
@@ -51,6 +53,7 @@ type PlatformCtrl struct {
	sandboxStore  *ss.SandboxStore
	userStore     *users.Connector
	mqGlobal      *mq.MsgQueue
	maxSessions   int
}

const scenarioDBName = "scenarios"
@@ -82,6 +85,12 @@ func Init() (err error) {
	// Create new Platform Controller
	pfmCtrl = new(PlatformCtrl)

	// Retrieve maximum session count from environment variable
	if maxSessions, err := strconv.ParseInt(os.Getenv("MEEP_MAX_SESSIONS"), 10, 0); err == nil {
		pfmCtrl.maxSessions = int(maxSessions)
	}
	log.Info("MEEP_MAX_SESSIONS: ", pfmCtrl.maxSessions)

	// Create message queue
	pfmCtrl.mqGlobal, err = mq.NewMsgQueue(mq.GetGlobalName(), moduleName, moduleNamespace, redisDBAddr)
	if err != nil {
+9 −0
Original line number Diff line number Diff line
@@ -54,6 +54,15 @@ func uaLoginUser(w http.ResponseWriter, r *http.Request) {
	sessionStore := pfmCtrl.sessionMgr.GetSessionStore()
	session, err := sessionStore.GetByName(username)
	if err != nil {
		// Check if max session count is reached before creating a new one
		count := sessionStore.GetCount()
		if count >= pfmCtrl.maxSessions {
			err = errors.New("Maximum session count exceeded")
			log.Error(err.Error())
			http.Error(w, err.Error(), http.StatusInsufficientStorage)
			return
		}

		// Get requested sandbox name from user profile, if any
		user, err := pfmCtrl.userStore.GetUser(username)
		if err == nil {
+12 −0
Original line number Diff line number Diff line
@@ -137,6 +137,18 @@ func (ss *SessionStore) Get(r *http.Request) (s *Session, err error) {
	return s, nil
}

// GetCount - Retrieve session count
func (ss *SessionStore) GetCount() (count int) {
	_ = ss.rc.ForEachEntry(ss.baseKey+"*", getCountHandler, &count)
	return count
}

func getCountHandler(key string, fields map[string]string, userData interface{}) error {
	count := userData.(*int)
	*count += 1
	return nil
}

// GetAll - Retrieve session by name
func (ss *SessionStore) GetAll() (sessionList []*Session, err error) {
	// Get all sessions, if any