Skip to content

Commit

Permalink
Merge branch 'master' into documentation/improve-markdown-formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Sogbey committed Nov 13, 2023
2 parents f6f87ff + 4deadb9 commit 889fbb6
Show file tree
Hide file tree
Showing 7 changed files with 1,034 additions and 67 deletions.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
- [Using Custom Session Stores (with context.Context)](#using-custom-session-stores-with-contextcontext)
- [Multiple Sessions per Request](#multiple-sessions-per-request)
- [Enumerate All Sessions](#enumerate-all-sessions)
- [Flushing and Streaming Responses](#flushing-and-streaming-responses)
- [Compatibility](#compatibility)
- [Contributing](#contributing)

### Installation

Expand Down Expand Up @@ -254,7 +256,9 @@ It is possible for an application to support multiple sessions per request, with

### Enumerate All Sessions


To iterate throught all sessions, SCS offers to all data stores an `All()` function where they can return their own sessions.

Essentially, in your code, you pass the `Iterate()` method a closure with the signature `func(ctx context.Context) error` which contains the logic that you want to execute against each session. For example, if you want to revoke all sessions with contain a `userID` value equal to `4` you can do the following:

```go
Expand All @@ -272,6 +276,38 @@ if err != nil {
}
```

### Flushing and Streaming Responses

Flushing responses is supported via the `http.NewResponseController` type (available in Go >= 1.20).

```go
func flushingHandler(w http.ResponseWriter, r *http.Request) {
sessionManager.Put(r.Context(), "message", "Hello from a flushing handler!")

rc := http.NewResponseController(w)

for i := 0; i < 5; i++ {
fmt.Fprintf(w, "Write %d\n", i)

err := rc.Flush()
if err != nil {
log.Println(err)
return
}

time.Sleep(time.Second)
}
}
```

For a complete working example, please see [this comment](https://github.com/alexedwards/scs/issues/141#issuecomment-1774050802).

Note that the `http.ResponseWriter` passed on by the [`LoadAndSave()`](https://pkg.go.dev/github.com/alexedwards/scs/v2#SessionManager.LoadAndSave) middleware does not support the `http.Flusher` interface directly. This effectively means that flushing/streaming is only supported by SCS if you are using Go >= 1.20.

### Compatibility

You may have some problems using this package with Go frameworks that do not propagate the request context from standard-library compatible middleware, like [Echo](https://github.com/alexedwards/scs/issues/57) and [Fiber](https://github.com/alexedwards/scs/issues/106). If you are using Echo, please use the [echo-scs-session](https://github.com/spazzymoto/echo-scs-session) fork of this package instead.

### Contributing

Bug fixes and documentation improvements are very welcome! For feature additions or behavioral changes, please open an issue to discuss the change before submitting a PR. For new stores, please also open an issue to establish whether there is wider demand for the store before submitting a PR.
13 changes: 13 additions & 0 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,19 @@ func (s *SessionManager) Deadline(ctx context.Context) time.Time {
return sd.deadline
}

// SetDeadline updates the 'absolute' expiry time for the session. Please note
// that if you are using an idle timeout, it is possible that a session will
// expire due to non-use before the set deadline.
func (s *SessionManager) SetDeadline(ctx context.Context, expire time.Time) {
sd := s.getSessionDataFromContext(ctx)

sd.mu.Lock()
defer sd.mu.Unlock()

sd.deadline = expire
sd.status = Modified
}

// Token returns the session token. Please note that this will return the
// empty string "" if it is called before the session has been committed to
// the store.
Expand Down
14 changes: 3 additions & 11 deletions firestore/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,8 @@ module github.com/alexedwards/scs/firestore
go 1.12

require (
cloud.google.com/go/firestore v1.6.1
github.com/census-instrumentation/opencensus-proto v0.3.0 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/envoyproxy/go-control-plane v0.10.1 // indirect
github.com/envoyproxy/protoc-gen-validate v0.6.2 // indirect
cloud.google.com/go/firestore v1.9.0
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
golang.org/x/text v0.3.8 // indirect
google.golang.org/api v0.60.0
google.golang.org/genproto v0.0.0-20211118181313-81c1377c94b1 // indirect
google.golang.org/grpc v1.42.0
google.golang.org/api v0.114.0
google.golang.org/grpc v1.56.3
)
Loading

0 comments on commit 889fbb6

Please sign in to comment.