Skip to content

Commit 38cb052

Browse files
committed
fix: Use skipmap as memory storage
1 parent da4a64a commit 38cb052

10 files changed

+79
-110
lines changed

context.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"net/http"
66
)
77

8-
// Define the key in the context
8+
// Define the keys in the context
99
type (
1010
ctxResKey struct{}
1111
ctxReqKey struct{}

context_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package session
33
import (
44
"context"
55
"fmt"
6-
"io/ioutil"
6+
"io"
77
"net/http"
88
"net/http/httptest"
99
"reflect"
@@ -48,7 +48,7 @@ func TestContext(t *testing.T) {
4848
return
4949
}
5050

51-
buf, _ := ioutil.ReadAll(res.Body)
51+
buf, _ := io.ReadAll(res.Body)
5252
res.Body.Close()
5353
if string(buf) != "ok" {
5454
t.Error("Not expected value:", string(buf))

default.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func manager(opt ...Option) *Manager {
1818
return internalManager
1919
}
2020

21-
// InitManager initialize the global session management instance
21+
// Initialize the global session management instance
2222
func InitManager(opt ...Option) {
2323
manager(opt...)
2424
}
@@ -33,7 +33,7 @@ func Destroy(ctx context.Context, w http.ResponseWriter, r *http.Request) error
3333
return manager().Destroy(ctx, w, r)
3434
}
3535

36-
// Refresh a session and return to session storage
36+
// Refresh and return session storage
3737
func Refresh(ctx context.Context, w http.ResponseWriter, r *http.Request) (Store, error) {
3838
return manager().Refresh(ctx, w, r)
3939
}

default_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package session
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88
"testing"
@@ -59,7 +59,7 @@ func TestDefaultStart(t *testing.T) {
5959
So(err, ShouldBeNil)
6060
So(res, ShouldNotBeNil)
6161

62-
buf, err := ioutil.ReadAll(res.Body)
62+
buf, err := io.ReadAll(res.Body)
6363
So(err, ShouldBeNil)
6464
res.Body.Close()
6565
So(string(buf), ShouldEqual, "bar:true")
@@ -124,7 +124,7 @@ func TestDefaultDestroy(t *testing.T) {
124124
So(err, ShouldBeNil)
125125
So(res, ShouldNotBeNil)
126126

127-
buf, err := ioutil.ReadAll(res.Body)
127+
buf, err := io.ReadAll(res.Body)
128128
So(err, ShouldBeNil)
129129
res.Body.Close()
130130
So(string(buf), ShouldEqual, "<nil>:false")
@@ -185,7 +185,7 @@ func TestDefaultRefresh(t *testing.T) {
185185
So(len(res.Cookies()), ShouldBeGreaterThan, 0)
186186
So(res.Cookies()[0].Value, ShouldNotEqual, cookie.Value)
187187

188-
buf, err := ioutil.ReadAll(res.Body)
188+
buf, err := io.ReadAll(res.Body)
189189
So(err, ShouldBeNil)
190190
res.Body.Close()
191191
So(string(buf), ShouldEqual, "bar:true")

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go 1.17
55
require github.com/smartystreets/goconvey v1.6.4
66

77
require (
8+
github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6 // indirect
89
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 // indirect
910
github.com/jtolds/gls v4.20.0+incompatible // indirect
1011
github.com/smartystreets/assertions v1.1.0 // indirect

go.sum

+10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1+
github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6 h1:FCLDGi1EmB7JzjVVYNZiqc/zAJj2BQ5M0lfkVOxbfs8=
2+
github.com/bytedance/gopkg v0.0.0-20221122125632-68358b8ecec6/go.mod h1:5FoAH5xUHHCMDvQPy1rnj8moqLkLHFaDVBjHhcFwEi0=
3+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
14
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
25
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00 h1:l5lAOZEym3oK3SQ2HBHWsJUfbNBiTXJDeW2QDxw9AQ0=
36
github.com/gopherjs/gopherjs v0.0.0-20200217142428-fce0ec30dd00/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
47
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
58
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
9+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
610
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
711
github.com/smartystreets/assertions v1.1.0 h1:MkTeG1DMwsrdH7QtLXy5W+fUxWq+vmb6cLmyJ7aRtF0=
812
github.com/smartystreets/assertions v1.1.0/go.mod h1:tcbTF8ujkAEcZ8TElKY+i30BzYlVhC/LOxJk7iOWnoo=
913
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
1014
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
15+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
16+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
1117
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
1218
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
19+
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
1320
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
21+
golang.org/x/sys v0.0.0-20221010170243-090e33056c14/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
1422
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
1523
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
24+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
25+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

session.go

+25-28
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@ import (
1818
const Version = "3.1.4"
1919

2020
var (
21-
// ErrInvalidSessionID invalid session id
22-
ErrInvalidSessionID = errors.New("invalid session id")
21+
ErrInvalidSessionID = errors.New("Invalid session id")
2322
)
2423

25-
// IDHandlerFunc Define the handler to get the session id
24+
// Define the handler to get the session id
2625
type IDHandlerFunc func(context.Context) string
2726

2827
// Define default options
@@ -55,103 +54,102 @@ type options struct {
5554
store ManagerStore
5655
}
5756

58-
// Option A session parameter options
5957
type Option func(*options)
6058

61-
// SetSign Set the session id signature value
59+
// Set the session id signature value
6260
func SetSign(sign []byte) Option {
6361
return func(o *options) {
6462
o.sign = sign
6563
}
6664
}
6765

68-
// SetCookieName Set the cookie name
66+
// Set the cookie name
6967
func SetCookieName(cookieName string) Option {
7068
return func(o *options) {
7169
o.cookieName = cookieName
7270
}
7371
}
7472

75-
// SetCookieLifeTime Set the cookie expiration time (in seconds)
73+
// Set the cookie expiration time (in seconds)
7674
func SetCookieLifeTime(cookieLifeTime int) Option {
7775
return func(o *options) {
7876
o.cookieLifeTime = cookieLifeTime
7977
}
8078
}
8179

82-
// SetDomain Set the domain name of the cookie
80+
// Set the domain name of the cookie
8381
func SetDomain(domain string) Option {
8482
return func(o *options) {
8583
o.domain = domain
8684
}
8785
}
8886

89-
// SetSecure Set cookie security
87+
// Set cookie security
9088
func SetSecure(secure bool) Option {
9189
return func(o *options) {
9290
o.secure = secure
9391
}
9492
}
9593

96-
// SetSameSite Set SameSite attribute of the cookie
94+
// Set SameSite attribute of the cookie
9795
func SetSameSite(sameSite http.SameSite) Option {
9896
return func(o *options) {
9997
o.sameSite = sameSite
10098
}
10199
}
102100

103-
// SetExpired Set session expiration time (in seconds)
101+
// Set session expiration time (in seconds)
104102
func SetExpired(expired int64) Option {
105103
return func(o *options) {
106104
o.expired = expired
107105
}
108106
}
109107

110-
// SetSessionID Set callback function to generate session id
108+
// Set callback function to generate session id
111109
func SetSessionID(handler IDHandlerFunc) Option {
112110
return func(o *options) {
113111
o.sessionID = handler
114112
}
115113
}
116114

117-
// SetEnableSetCookie Enable writing session id to cookie
115+
// Enable writing session id to cookie
118116
// (enabled by default, can be turned off if no cookie is written)
119117
func SetEnableSetCookie(enableSetCookie bool) Option {
120118
return func(o *options) {
121119
o.enableSetCookie = enableSetCookie
122120
}
123121
}
124122

125-
// SetEnableSIDInURLQuery Allow session id from URL query parameters (enabled by default)
123+
// Allow session id from URL query parameters (enabled by default)
126124
func SetEnableSIDInURLQuery(enableSIDInURLQuery bool) Option {
127125
return func(o *options) {
128126
o.enableSIDInURLQuery = enableSIDInURLQuery
129127
}
130128
}
131129

132-
// SetEnableSIDInHTTPHeader Allow session id to be obtained from the request header
130+
// Allow session id to be obtained from the request header
133131
func SetEnableSIDInHTTPHeader(enableSIDInHTTPHeader bool) Option {
134132
return func(o *options) {
135133
o.enableSIDInHTTPHeader = enableSIDInHTTPHeader
136134
}
137135
}
138136

139-
// SetSessionNameInHTTPHeader The key name in the request header where the session ID is stored
137+
// The key name in the request header where the session ID is stored
140138
// (if it is empty, the default is the cookie name)
141139
func SetSessionNameInHTTPHeader(sessionNameInHTTPHeader string) Option {
142140
return func(o *options) {
143141
o.sessionNameInHTTPHeader = sessionNameInHTTPHeader
144142
}
145143
}
146144

147-
// SetStore Set session management storage
145+
// Set session management storage
148146
func SetStore(store ManagerStore) Option {
149147
return func(o *options) {
150148
o.store = store
151149
}
152150
}
153151

154-
// NewManager Create a session management instance
152+
// Create a session management instance
155153
func NewManager(opt ...Option) *Manager {
156154
opts := defaultOptions
157155
for _, o := range opt {
@@ -168,7 +166,7 @@ func NewManager(opt ...Option) *Manager {
168166
return &Manager{opts: &opts}
169167
}
170168

171-
// Manager A session management instance, including start and destroy operations
169+
// A session management instance, including start and destroy operations
172170
type Manager struct {
173171
opts *options
174172
}
@@ -305,8 +303,8 @@ func (m *Manager) Start(ctx context.Context, w http.ResponseWriter, r *http.Requ
305303
}
306304

307305
if sid != "" {
308-
if exists, verr := m.opts.store.Check(ctx, sid); verr != nil {
309-
return nil, verr
306+
if exists, err := m.opts.store.Check(ctx, sid); err != nil {
307+
return nil, err
310308
} else if exists {
311309
return m.opts.store.Update(ctx, sid, m.opts.expired)
312310
}
@@ -322,19 +320,19 @@ func (m *Manager) Start(ctx context.Context, w http.ResponseWriter, r *http.Requ
322320
return store, nil
323321
}
324322

325-
// Refresh a session and return to session storage
323+
// Refresh and return session storage
326324
func (m *Manager) Refresh(ctx context.Context, w http.ResponseWriter, r *http.Request) (Store, error) {
327325
ctx = m.getContext(ctx, w, r)
328326

329-
oldsid, err := m.sessionID(r)
327+
oldSID, err := m.sessionID(r)
330328
if err != nil {
331329
return nil, err
332-
} else if oldsid == "" {
333-
oldsid = m.opts.sessionID(ctx)
330+
} else if oldSID == "" {
331+
oldSID = m.opts.sessionID(ctx)
334332
}
335333

336334
sid := m.opts.sessionID(ctx)
337-
store, err := m.opts.store.Refresh(ctx, oldsid, sid, m.opts.expired)
335+
store, err := m.opts.store.Refresh(ctx, oldSID, sid, m.opts.expired)
338336
if err != nil {
339337
return nil, err
340338
}
@@ -354,8 +352,7 @@ func (m *Manager) Destroy(ctx context.Context, w http.ResponseWriter, r *http.Re
354352
return nil
355353
}
356354

357-
exists, err := m.opts.store.Check(ctx, sid)
358-
if err != nil {
355+
if exists, err := m.opts.store.Check(ctx, sid); err != nil {
359356
return err
360357
} else if !exists {
361358
return nil

session_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package session
22

33
import (
44
"fmt"
5-
"io/ioutil"
5+
"io"
66
"net/http"
77
"net/http/httptest"
88
"testing"
@@ -56,7 +56,7 @@ func TestSessionStart(t *testing.T) {
5656
So(err, ShouldBeNil)
5757
So(res, ShouldNotBeNil)
5858

59-
buf, err := ioutil.ReadAll(res.Body)
59+
buf, err := io.ReadAll(res.Body)
6060
So(err, ShouldBeNil)
6161
res.Body.Close()
6262
So(string(buf), ShouldEqual, "bar:true")
@@ -127,7 +127,7 @@ func TestSessionDestroy(t *testing.T) {
127127
So(err, ShouldBeNil)
128128
So(res, ShouldNotBeNil)
129129

130-
buf, err := ioutil.ReadAll(res.Body)
130+
buf, err := io.ReadAll(res.Body)
131131
So(err, ShouldBeNil)
132132
res.Body.Close()
133133
So(string(buf), ShouldEqual, "<nil>:false")
@@ -194,7 +194,7 @@ func TestSessionRefresh(t *testing.T) {
194194
So(len(res.Cookies()), ShouldBeGreaterThan, 0)
195195
So(res.Cookies()[0].Value, ShouldNotEqual, cookie.Value)
196196

197-
buf, err := ioutil.ReadAll(res.Body)
197+
buf, err := io.ReadAll(res.Body)
198198
So(err, ShouldBeNil)
199199
res.Body.Close()
200200
So(string(buf), ShouldEqual, "bar:true")

0 commit comments

Comments
 (0)