Skip to content

Commit e70766a

Browse files
Use concurrency safe context by default.
Allow use of unsafe context.
1 parent 8896575 commit e70766a

File tree

3 files changed

+60
-4
lines changed

3 files changed

+60
-4
lines changed

context.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"os"
1414
"path/filepath"
1515
"strings"
16+
"sync"
1617
)
1718

1819
type (
@@ -198,6 +199,7 @@ type (
198199
handler HandlerFunc
199200
store Map
200201
echo *Echo
202+
lock sync.RWMutex
201203
}
202204
)
203205

@@ -360,10 +362,18 @@ func (c *context) Cookies() []*http.Cookie {
360362
}
361363

362364
func (c *context) Get(key string) interface{} {
365+
if !c.echo.UnsafeContext {
366+
c.lock.RLock()
367+
defer c.lock.RUnlock()
368+
}
363369
return c.store[key]
364370
}
365371

366372
func (c *context) Set(key string, val interface{}) {
373+
if !c.echo.UnsafeContext {
374+
c.lock.Lock()
375+
defer c.lock.Unlock()
376+
}
367377
if c.store == nil {
368378
c.store = make(Map)
369379
}
@@ -597,4 +607,3 @@ func (c *context) Reset(r *http.Request, w http.ResponseWriter) {
597607
// NOTE: Don't reset because it has to have length c.echo.maxParam at all times
598608
// c.pvalues = nil
599609
}
600-

context_test.go

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -523,12 +523,59 @@ func TestContextRedirect(t *testing.T) {
523523
}
524524

525525
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+
528532
c.Set("name", "Jon Snow")
529533
testify.Equal(t, "Jon Snow", c.Get("name"))
530534
}
531535

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+
532579
func TestContextHandler(t *testing.T) {
533580
e := New()
534581
r := e.Router()
@@ -543,4 +590,3 @@ func TestContextHandler(t *testing.T) {
543590
c.Handler()(c)
544591
testify.Equal(t, "handler", b.String())
545592
}
546-

echo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ type (
8484
Validator Validator
8585
Renderer Renderer
8686
Logger Logger
87+
UnsafeContext bool
8788
}
8889

8990
// Route contains a handler and information for matching against requests.

0 commit comments

Comments
 (0)