Small adjustments to doc and code
This commit is contained in:
parent
69d6fc8a08
commit
45123d248d
|
|
@ -108,6 +108,7 @@ func (s *storedSessionLoader) getValidatedSession(rw http.ResponseWriter, req *h
|
||||||
}
|
}
|
||||||
|
|
||||||
session, err = s.store.LoadWithLock(req)
|
session, err = s.store.LoadWithLock(req)
|
||||||
|
defer s.store.ReleaseLock(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -117,12 +118,10 @@ func (s *storedSessionLoader) getValidatedSession(rw http.ResponseWriter, req *h
|
||||||
}
|
}
|
||||||
|
|
||||||
if !s.isSessionRefreshNeeded(session) {
|
if !s.isSessionRefreshNeeded(session) {
|
||||||
_ = s.store.ReleaseLock(req)
|
|
||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
logger.Printf("Refreshing %s old session cookie for %s (refresh after %s)", session.Age(), session, s.refreshPeriod)
|
logger.Printf("Refreshing %s old session cookie for %s (refresh after %s)", session.Age(), session, s.refreshPeriod)
|
||||||
refreshed, err := s.refreshSession(rw, req, session)
|
refreshed, err := s.refreshSession(rw, req, session)
|
||||||
_ = s.store.ReleaseLock(req)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error refreshing access token for session (%s): %v", session, err)
|
return nil, fmt.Errorf("error refreshing access token for session (%s): %v", session, err)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ func (c *client) Get(ctx context.Context, key string) ([]byte, error) {
|
||||||
|
|
||||||
func (c *client) Lock(ctx context.Context, key string, expiration time.Duration) error {
|
func (c *client) Lock(ctx context.Context, key string, expiration time.Duration) error {
|
||||||
if c.locks[key] != nil {
|
if c.locks[key] != nil {
|
||||||
return fmt.Errorf("locks for key %s already exists", key)
|
return fmt.Errorf("lock for key %s already exists", key)
|
||||||
}
|
}
|
||||||
lock, err := c.locker.Obtain(ctx, key, expiration, nil)
|
lock, err := c.locker.Obtain(ctx, key, expiration, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -65,23 +65,13 @@ func (c *client) Unlock(ctx context.Context, key string) error {
|
||||||
if c.locks[key] == nil {
|
if c.locks[key] == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return c.locks[key].Release(ctx)
|
err := c.locks[key].Release(ctx)
|
||||||
|
delete(c.locks, key)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {
|
func (c *client) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {
|
||||||
err := c.Client.Set(ctx, key, value, expiration).Err()
|
return c.Client.Set(ctx, key, value, expiration).Err()
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if c.locks[key] == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
err = c.locks[key].Release(ctx)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
c.locks = nil
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *client) Del(ctx context.Context, key string) error {
|
func (c *client) Del(ctx context.Context, key string) error {
|
||||||
|
|
@ -105,12 +95,23 @@ func newClusterClient(c *redis.ClusterClient) Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *clusterClient) Get(ctx context.Context, key string) ([]byte, error) {
|
func (c *clusterClient) Get(ctx context.Context, key string) ([]byte, error) {
|
||||||
|
if c.locks[key] != nil {
|
||||||
|
for {
|
||||||
|
ttl, err := c.locks[key].TTL(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
if ttl <= 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
return c.ClusterClient.Get(ctx, key).Bytes()
|
return c.ClusterClient.Get(ctx, key).Bytes()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *clusterClient) Lock(ctx context.Context, key string, expiration time.Duration) error {
|
func (c *clusterClient) Lock(ctx context.Context, key string, expiration time.Duration) error {
|
||||||
if c.locks[key] != nil {
|
if c.locks[key] != nil {
|
||||||
return fmt.Errorf("locks for key %s already exists", key)
|
return fmt.Errorf("lock for key %s already exists", key)
|
||||||
}
|
}
|
||||||
lock, err := c.locker.Obtain(ctx, key, expiration, nil)
|
lock, err := c.locker.Obtain(ctx, key, expiration, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -124,7 +125,9 @@ func (c *clusterClient) Unlock(ctx context.Context, key string) error {
|
||||||
if c.locks[key] == nil {
|
if c.locks[key] == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return c.locks[key].Release(ctx)
|
err := c.locks[key].Release(ctx)
|
||||||
|
delete(c.locks, key)
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *clusterClient) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {
|
func (c *clusterClient) Set(ctx context.Context, key string, value []byte, expiration time.Duration) error {
|
||||||
|
|
|
||||||
|
|
@ -54,16 +54,17 @@ func (store *SessionStore) Load(ctx context.Context, key string) ([]byte, error)
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReleaseLock sessions.SessionState information from a persistence
|
// Load reads sessions.SessionState information from a persistence
|
||||||
|
// cookie within the HTTP request object and locks it on redis
|
||||||
func (store *SessionStore) LoadWithLock(ctx context.Context, key string) ([]byte, error) {
|
func (store *SessionStore) LoadWithLock(ctx context.Context, key string) ([]byte, error) {
|
||||||
value, err := store.Client.Get(ctx, key)
|
value, err := store.Client.Get(ctx, key)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error loading redis session: %v", err)
|
return nil, fmt.Errorf("error loading redis session: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = store.Client.Lock(ctx, key, 200*time.Millisecond)
|
err = store.Client.Lock(ctx, key, 300*time.Millisecond)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("error setting redis locks: %v", err)
|
return nil, fmt.Errorf("error setting redis lock: %v", err)
|
||||||
}
|
}
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue