fix session leak
This commit is contained in:
@@ -39,13 +39,13 @@ type SESClient struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
defaultExpiration = 5 * time.Minute
|
emailRetryExpiration = 5 * time.Minute
|
||||||
cleanupInterval = 10 * time.Minute
|
sesCleanupInterval = 10 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewSESClient() SESClient {
|
func NewSESClient() SESClient {
|
||||||
return SESClient{
|
return SESClient{
|
||||||
ResetCache: cache.New(defaultExpiration, cleanupInterval),
|
ResetCache: cache.New(emailRetryExpiration, sesCleanupInterval),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -84,7 +84,7 @@ func (s *SESClient) SendEmail(email Email) error {
|
|||||||
Source: aws.String(email.Sender),
|
Source: aws.String(email.Sender),
|
||||||
}
|
}
|
||||||
|
|
||||||
if err = s.ResetCache.Add(email.Recipient, nil, defaultExpiration); err != nil {
|
if err = s.ResetCache.Add(email.Recipient, nil, emailRetryExpiration); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,21 +3,28 @@ package core
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/patrickmn/go-cache"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
sessionExpiration = 5 * time.Minute
|
||||||
|
sessionCleanupInterval = 10 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
type NKodeAPI struct {
|
type NKodeAPI struct {
|
||||||
Db DbAccessor
|
Db DbAccessor
|
||||||
SignupSessions map[SessionId]UserSignSession
|
SignupSessionCache *cache.Cache
|
||||||
EmailQueue *EmailQueue
|
EmailQueue *EmailQueue
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewNKodeAPI(db DbAccessor, queue *EmailQueue) NKodeAPI {
|
func NewNKodeAPI(db DbAccessor, queue *EmailQueue) NKodeAPI {
|
||||||
return NKodeAPI{
|
return NKodeAPI{
|
||||||
Db: db,
|
Db: db,
|
||||||
SignupSessions: make(map[SessionId]UserSignSession),
|
|
||||||
EmailQueue: queue,
|
EmailQueue: queue,
|
||||||
|
SignupSessionCache: cache.New(sessionExpiration, sessionCleanupInterval),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,8 +61,12 @@ func (n *NKodeAPI) GenerateSignupResetInterface(userEmail UserEmail, customerId
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
n.SignupSessions[signupSession.Id] = *signupSession
|
//n.SignupSessions[signupSession.Id] = *signupSession
|
||||||
|
if err := n.SignupSessionCache.Add(signupSession.Id.String(), *signupSession, sessionExpiration); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
svgInterface, err := n.Db.GetSvgStringInterface(signupSession.LoginUserInterface.SvgId)
|
svgInterface, err := n.Db.GetSvgStringInterface(signupSession.LoginUserInterface.SvgId)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -74,47 +85,57 @@ func (n *NKodeAPI) SetNKode(customerId CustomerId, sessionId SessionId, keySelec
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
session, exists := n.SignupSessions[sessionId]
|
session, exists := n.SignupSessionCache.Get(sessionId.String())
|
||||||
if !exists {
|
if !exists {
|
||||||
log.Printf("session id does not exist %s", sessionId)
|
log.Printf("session id does not exist %s", sessionId)
|
||||||
return nil, ErrSignupSessionDNE
|
return nil, ErrSignupSessionDNE
|
||||||
}
|
}
|
||||||
confirmInterface, err := session.SetUserNKode(keySelection)
|
userSession, ok := session.(UserSignSession)
|
||||||
|
if !ok {
|
||||||
|
// handle the case where the type assertion fails
|
||||||
|
return nil, ErrSignupSessionDNE
|
||||||
|
}
|
||||||
|
confirmInterface, err := userSession.SetUserNKode(keySelection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
n.SignupSessions[sessionId] = session
|
n.SignupSessionCache.Set(sessionId.String(), userSession, sessionExpiration)
|
||||||
return confirmInterface, nil
|
return confirmInterface, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *NKodeAPI) ConfirmNKode(customerId CustomerId, sessionId SessionId, keySelection KeySelection) error {
|
func (n *NKodeAPI) ConfirmNKode(customerId CustomerId, sessionId SessionId, keySelection KeySelection) error {
|
||||||
session, exists := n.SignupSessions[sessionId]
|
session, exists := n.SignupSessionCache.Get(sessionId.String())
|
||||||
if !exists {
|
if !exists {
|
||||||
log.Printf("session id does not exist %s", sessionId)
|
log.Printf("session id does not exist %s", sessionId)
|
||||||
return ErrSignupSessionDNE
|
return ErrSignupSessionDNE
|
||||||
}
|
}
|
||||||
|
userSession, ok := session.(UserSignSession)
|
||||||
|
if !ok {
|
||||||
|
// handle the case where the type assertion fails
|
||||||
|
return ErrSignupSessionDNE
|
||||||
|
}
|
||||||
customer, err := n.Db.GetCustomer(customerId)
|
customer, err := n.Db.GetCustomer(customerId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
passcode, err := session.DeducePasscode(keySelection)
|
passcode, err := userSession.DeducePasscode(keySelection)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
err = customer.IsValidNKode(session.Kp, passcode)
|
err = customer.IsValidNKode(userSession.Kp, passcode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
user, err := NewUser(*customer, string(session.UserEmail), passcode, session.LoginUserInterface, session.Kp)
|
user, err := NewUser(*customer, string(userSession.UserEmail), passcode, userSession.LoginUserInterface, userSession.Kp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if session.Reset {
|
if userSession.Reset {
|
||||||
err = n.Db.UpdateUserNKode(*user)
|
err = n.Db.UpdateUserNKode(*user)
|
||||||
} else {
|
} else {
|
||||||
err = n.Db.WriteNewUser(*user)
|
err = n.Db.WriteNewUser(*user)
|
||||||
}
|
}
|
||||||
delete(n.SignupSessions, session.Id)
|
n.SignupSessionCache.Delete(userSession.Id.String())
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,6 +241,7 @@ func (n *NKodeAPI) ResetNKode(userEmail UserEmail, customerId CustomerId) error
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error getting user in rest nkode %v", err)
|
return fmt.Errorf("error getting user in rest nkode %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if user == nil {
|
if user == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -242,11 +264,3 @@ func (n *NKodeAPI) ResetNKode(userEmail UserEmail, customerId CustomerId) error
|
|||||||
n.EmailQueue.AddEmail(email)
|
n.EmailQueue.AddEmail(email)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getColors(colorIds []int) []RGBColor {
|
|
||||||
colors := make([]RGBColor, len(colorIds))
|
|
||||||
for idx, colorIdx := range colorIds {
|
|
||||||
colors[idx] = SetColors[colorIdx]
|
|
||||||
}
|
|
||||||
return colors
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -98,6 +98,12 @@ func CustomerIdToString(customerId CustomerId) string {
|
|||||||
type SessionId uuid.UUID
|
type SessionId uuid.UUID
|
||||||
type UserId uuid.UUID
|
type UserId uuid.UUID
|
||||||
|
|
||||||
|
func (s *SessionId) String() string {
|
||||||
|
id := uuid.UUID(*s)
|
||||||
|
return id.String()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
type UserEmail string
|
type UserEmail string
|
||||||
|
|
||||||
func ParseEmail(email string) (UserEmail, error) {
|
func ParseEmail(email string) (UserEmail, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user