Comments (5)
By default the idle timeout is updated whenever the session is modified (not when it's only read). So a page refresh alone wouldn't trigger the idle timeout to be updated.
I've added a new *Manager.Use()
middleware, which checks whether there an idle timeout is set on the session manager, and if there is, will trigger the idle timeout update each time the session is read. If you wrap your handlers with that it should do what you need it too.
var sessionManager *scs.Manager
func main() {
sessionManager = scs.NewManager(redisstore.New(&redis.Pool{
MaxIdle: 10,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(0))
},
IdleTimeout: time.Duration(20 * time.Minute),
}))
sessionManager.IdleTimeout(20 * time.Minute)
sessionManager.Lifetime(time.Duration(2 * time.Hour))
mux := http.NewServeMux()
mux.HandleFunc("/put", putHandler)
mux.HandleFunc("/get", getHandler)
http.ListenAndServe(":4000", sessionManager.Use(mux))
}
Thanks
from scs.
But I'm using the echo framework. How can I solve this problem?
from scs.
I'm not hugely familiar with Echo but the code below seems to work for me:
package main
import (
"net/http"
"time"
"github.com/alexedwards/scs"
"github.com/alexedwards/scs/stores/redisstore"
"github.com/garyburd/redigo/redis"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
var sessionManager *scs.Manager
func main() {
sessionManager = scs.NewManager(redisstore.New(&redis.Pool{
MaxIdle: 10,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", "127.0.0.1:6379", redis.DialDatabase(0))
},
IdleTimeout: time.Duration(20 * time.Minute),
}))
sessionManager.IdleTimeout(20 * time.Minute)
sessionManager.Lifetime(time.Duration(2 * time.Hour))
e := echo.New()
e.Use(echo.WrapMiddleware(sessionManager.Use))
e.GET("/put", func(c echo.Context) error {
session := sessionManager.Load(c.Request())
err := session.PutString(c.Response().Writer, "message", "Hello world!")
if err != nil {
http.Error(c.Response().Writer, err.Error(), 500)
}
return c.String(http.StatusOK, "put")
})
e.GET("/get", func(c echo.Context) error {
session := sessionManager.Load(c.Request())
msg, err := session.GetString("message")
if err != nil {
http.Error(c.Response().Writer, err.Error(), 500)
}
return c.String(http.StatusOK, "result "+msg)
})
e.Start(":4000")
}
from scs.
Thank you!
from scs.
I think it's worth mentioning that redis.Pool.IdleTimeout has a different meaning than session.IdleTimeout. The redis client docs say that IdleTimeout has to do with stale client connections from redis.Pool to the redis.Server. It has nothing to do with invalidating the record within redis. The redis EXPIRE directive sets the timeout on the key.
The following is sufficient for controlling stale sessions within a web request:
SessionManager.IdleTimeout(20 * time.Minute)
SessionManager.Lifetime(time.Duration(2 * time.Hour)
Then use it in the middleware so that at least Touch()
is called which updates the deadline property.
from scs.
Related Issues (20)
- Fails using ListenAndServeTLS HOT 1
- Sqlite3store error constraint failed: NOT NULL constraint failed: sessions.expiry (1299) HOT 4
- manage all session HOT 1
- Redis example is using a different Redis library HOT 8
- Custom names for sessions tables HOT 3
- support partitioned CHIPS cookies HOT 2
- pgxstore expects a *pgxpool.Pool instead of a *sql.DB HOT 5
- Flush / streaming HOT 2
- *scs.sessionResponseWriter does not implement http.Hijacker HOT 3
- Support Turso and libsql HOT 2
- Session getting added to DB, but not to cookie, when set to SameSiteNone, but works in SameSiteLax HOT 2
- Sign session ids/tokens HOT 5
- 2.7.0 breaks websocket compatibility HOT 2
- NATS Jetstream and session replication might be of interest HOT 3
- Submodules not being released with tags HOT 1
- Usage with TursoDB HOT 1
- sqlite3 store with custom table name
- Gob register custom struct HOT 2
- [interest check for new store] sqlite3 store using CGO-less modernc.org/sqlite
- [bug] Pop doesn't remove value if w.Write happened before sm.Pop HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from scs.