Skip to content

Commit

Permalink
count docs instead of find
Browse files Browse the repository at this point in the history
  • Loading branch information
jr22 committed Sep 24, 2024
1 parent 6aa1e5a commit 1d96286
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions web/sessions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ type Store interface {
Get(ctx context.Context, id string) (*Session, error)
Save(ctx context.Context, s *Session) error
SetSessionManager(*SessionManager)
GetByToken(ctx context.Context, id string) (*Session, error)
HasSessionWithToken(ctx context.Context, token string) (bool, error)
}

// ----
Expand Down Expand Up @@ -138,16 +138,13 @@ func (sm *SessionManager) DeleteSession(ctx context.Context, r *http.Request, w

// HasSessionWithAccessToken returns true if there is an active session associated with that access token.
func (sm *SessionManager) HasSessionWithAccessToken(ctx context.Context, token string) bool {
session, err := sm.store.GetByToken(ctx, token)
if err != nil && !errors.Is(err, errNoSession) {
hasSessionWIthToken, err := sm.store.HasSessionWithToken(ctx, token)
if err != nil {
sm.logger.Errorw("error finding session with access token", "err", err)
return false
}

if session == nil {
return false
}
return true
return hasSessionWIthToken
}

func (sm *SessionManager) newID() (string, error) {
Expand Down Expand Up @@ -236,32 +233,19 @@ func (mss *mongoDBSessionStore) Get(ctx context.Context, id string) (*Session, e
return s, nil
}

func (mss *mongoDBSessionStore) GetByToken(ctx context.Context, token string) (*Session, error) {
func (mss *mongoDBSessionStore) HasSessionWithToken(ctx context.Context, token string) (bool, error) {
ctx, span := trace.StartSpan(ctx, "MongoDBSessionStore::Get")
defer span.End()

res := mss.collection.FindOne(ctx, bson.M{"data.access_token": token})
if res.Err() != nil {
if errors.Is(res.Err(), mongo.ErrNoDocuments) {
return nil, errNoSession
count, err := mss.collection.CountDocuments(ctx, bson.M{"data.access_token": token}, options.Count().SetLimit(1))
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
return false, errNoSession
}
return nil, fmt.Errorf("couldn't load session from db: %w", res.Err())
return false, fmt.Errorf("failed to load session from db: %w", err)
}

m := bson.M{}
if err := res.Decode(&m); err != nil {
return nil, err
}

s := &Session{
store: mss,
manager: mss.manager,
isNew: false,
id: m["_id"].(string),
Data: m["data"].(bson.M),
}

return s, nil
return count > 0, nil
}

func (mss *mongoDBSessionStore) Save(ctx context.Context, s *Session) error {
Expand Down Expand Up @@ -306,19 +290,19 @@ func (mss *memorySessionStore) Delete(ctx context.Context, id string) error {
return nil
}

func (mss *memorySessionStore) GetByToken(ctx context.Context, token string) (*Session, error) {
func (mss *memorySessionStore) HasSessionWithToken(ctx context.Context, token string) (bool, error) {
if mss.data != nil {
for _, session := range mss.data {
savedToken, ok := session.Data["access_token"]
if !ok {
continue
}
if token == savedToken {
return session, nil
return true, nil
}
}
}
return nil, errNoSession
return false, errNoSession
}

func (mss *memorySessionStore) Get(ctx context.Context, id string) (*Session, error) {
Expand Down

0 comments on commit 1d96286

Please sign in to comment.