TTL
The TTL determines how long a session can remain inactive before it expires.
By default, sessions use a three-hour TTL. Each session can consult or change its own duration by using GetTTL() and SetTTL().
func Login(ctx *gox.Context) gox.Action {
ctx.Session.SetTTL(30 * time.Minute)
return ctx.Response.DoneWith(204)
} When the TTL runs out of activity, GOX eliminates the session and the client receives a new session in his next request.
Structure
ctx.Session provides the following TTL-related methods:
Session
{
SetTTL(ttl time.Duration)
GetTTL() time.Duration
} Methods
| Method | Description |
|---|---|
| SetTTL() | Set the TTL for the current session. |
| GetTTL() | Returns the TTL from the current session. |
SetTTL()
Firma:func (*Session) SetTTL(ttl time.Duration)
SetTTL() sets out how long the current session can remain inactive.
ctx.Session.SetTTL(30 * time.Minute) The change only affects the current session and replaces the previously configured TTL.
When you run SetTTL()the expiry period begins again using the indicated duration.
func RememberMe(ctx *gox.Context) gox.Action {
ctx.Session.SetTTL(30 * 24 * time.Hour)
return ctx.Response.DoneWith(204)
} The duration must be greater than zero. Values equal to or less than zero are ignored.
SetTTL() no errors return. If the session is not available or was destroyed, the operation does not produce any change.
GetTTL()
Firma:func (*Session) GetTTL() time.Duration
GetTTL() returns the TTL configured for the current session.
ttl := ctx.Session.GetTTL() The result uses time.Duration.
func GET(ctx *gox.Context) gox.Page {
minutes := ctx.Session.GetTTL().Minutes()
return <p>TTL: {{ minutes }} minutos</p>
} If the session is not available or was destroyed, GetTTL() return 0.
GetTTL() returns the set duration, not the remaining time before the session expires.
Default configuration
The general TTL of the sessions is set up by server.Config.Session.TTL.
func SetGoxConfig() server.Config {
return server.Config{
Session: server.SessionConfig{
TTL: 24 * time.Hour,
},
}
} This value applies to new sessions. When no valid TTL is configured, GOX uses three hours.
A session can later replace this value by SetTTL().
See Configuration to know the complete default server configuration.
Renewal
The TTL is renewed when GOX correctly recovers a session during an HTTP request.
Última actividad + TTL = nueva expiración The activity of a WebSocket connection also keeps the session active. GOX renews its expiry when processing events associated with it.
Reading or modifying Session Storage does not start an independent period. The renewal belongs to the HTTP or WebSocket activity that contains the operation.
The own TTL established by SetTTL() is preserved and used in subsequent renovations.
Expiration
A session expires when your TTL passes without activity.
After expiry:
- The session is no longer available.
- Their values are eliminated.
- The associated WebSocket connections are closed.
- The client receives a new session in his next request.
The expiry by TTL is different from the explicit destruction by Destroy().
IsDestroyed() indicates whether a session was explicitly destroyed; does not indicate whether it expired by inactivity.
Yeah. OnExpire is configured, GOX runs it with a single-reading representation of the expired session. Events to know his behavior.
Full example
The following example assigns an extended TTL and then consults its duration:
func RememberMe(ctx *gox.Context) gox.Action {
ctx.Session.SetTTL(30 * 24 * time.Hour)
if err := ctx.Session.Set("rememberMe", true); err != nil {
return ctx.Response.DoneWith(500)
}
return ctx.Response.DoneWith(204)
}
func GET(ctx *gox.Context) gox.Page {
ttl := ctx.Session.GetTTL()
return <p>TTL: {{ ttl }}</p>
} The subsequent requests renew the expiry using the 30-day TTL.
Best practices
- Configure a default TTL appropriate for the application type.
- Use short TTL for sessions with sensitive information.
- Use
SetTTL()only when a session requires a different duration. - Do not use
GetTTL()to calculate the remaining time before expiry. - Do not use values equal to or less than zero.
- Use
Destroy()when you need to finish a session immediately. - It processes the expiry by
OnExpirewhen you need to perform cleaning tasks.