File tree Expand file tree Collapse file tree 3 files changed +60
-4
lines changed Expand file tree Collapse file tree 3 files changed +60
-4
lines changed Original file line number Diff line number Diff line change @@ -13,6 +13,7 @@ import (
13
13
"os"
14
14
"path/filepath"
15
15
"strings"
16
+ "sync"
16
17
)
17
18
18
19
type (
@@ -198,6 +199,7 @@ type (
198
199
handler HandlerFunc
199
200
store Map
200
201
echo * Echo
202
+ lock sync.RWMutex
201
203
}
202
204
)
203
205
@@ -360,10 +362,18 @@ func (c *context) Cookies() []*http.Cookie {
360
362
}
361
363
362
364
func (c * context ) Get (key string ) interface {} {
365
+ if ! c .echo .UnsafeContext {
366
+ c .lock .RLock ()
367
+ defer c .lock .RUnlock ()
368
+ }
363
369
return c .store [key ]
364
370
}
365
371
366
372
func (c * context ) Set (key string , val interface {}) {
373
+ if ! c .echo .UnsafeContext {
374
+ c .lock .Lock ()
375
+ defer c .lock .Unlock ()
376
+ }
367
377
if c .store == nil {
368
378
c .store = make (Map )
369
379
}
@@ -597,4 +607,3 @@ func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
597
607
// NOTE: Don't reset because it has to have length c.echo.maxParam at all times
598
608
// c.pvalues = nil
599
609
}
600
-
Original file line number Diff line number Diff line change @@ -523,12 +523,59 @@ func TestContextRedirect(t *testing.T) {
523
523
}
524
524
525
525
func TestContextStore (t * testing.T ) {
526
- var c Context
527
- c = new (context )
526
+ e := & Echo {}
527
+
528
+ c := & context {
529
+ echo : e ,
530
+ }
531
+
528
532
c .Set ("name" , "Jon Snow" )
529
533
testify .Equal (t , "Jon Snow" , c .Get ("name" ))
530
534
}
531
535
536
+ func BenchmarkContext_Store (b * testing.B ) {
537
+ e := & Echo {}
538
+
539
+ c := & context {
540
+ echo : e ,
541
+ }
542
+
543
+ for n := 0 ; n < b .N ; n ++ {
544
+ c .Set ("name" , "Jon Snow" )
545
+ if c .Get ("name" ) != "Jon Snow" {
546
+ b .Fail ()
547
+ }
548
+ }
549
+ }
550
+
551
+ func TestContextUnsafeStore (t * testing.T ) {
552
+ e := & Echo {}
553
+ e .UnsafeContext = true
554
+
555
+ c := & context {
556
+ echo : e ,
557
+ }
558
+
559
+ c .Set ("name" , "Jon Safe" )
560
+ testify .Equal (t , "Jon Safe" , c .Get ("name" ))
561
+ }
562
+
563
+ func BenchmarkContext_UnsafeStore (b * testing.B ) {
564
+ e := & Echo {}
565
+ e .UnsafeContext = true
566
+
567
+ c := & context {
568
+ echo : e ,
569
+ }
570
+
571
+ for n := 0 ; n < b .N ; n ++ {
572
+ c .Set ("name" , "Jon Snow" )
573
+ if c .Get ("name" ) != "Jon Snow" {
574
+ b .Fail ()
575
+ }
576
+ }
577
+ }
578
+
532
579
func TestContextHandler (t * testing.T ) {
533
580
e := New ()
534
581
r := e .Router ()
@@ -543,4 +590,3 @@ func TestContextHandler(t *testing.T) {
543
590
c .Handler ()(c )
544
591
testify .Equal (t , "handler" , b .String ())
545
592
}
546
-
Original file line number Diff line number Diff line change 84
84
Validator Validator
85
85
Renderer Renderer
86
86
Logger Logger
87
+ UnsafeContext bool
87
88
}
88
89
89
90
// Route contains a handler and information for matching against requests.
You can’t perform that action at this time.
0 commit comments