Skip to content

Commit 4dc376e

Browse files
author
Dean Karn
committed
Update docs, benchmarks
1 parent e8bf16b commit 4dc376e

File tree

4 files changed

+41
-96
lines changed

4 files changed

+41
-96
lines changed

README.md

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
## LARS
2-
<img align="right" src="https://raw.githubusercontent.com/go-playground/lars/master/_examples/README/test.gif">![Project status](https://img.shields.io/badge/version-4.0.0-green.svg)
2+
<img align="right" src="https://raw.githubusercontent.com/go-playground/lars/master/_examples/README/test.gif">![Project status](https://img.shields.io/badge/version-4.0.1-green.svg)
33
[![Build Status](https://semaphoreci.com/api/v1/projects/4351aa2d-2f94-40be-a6ef-85c248490378/679708/badge.svg)](https://semaphoreci.com/joeybloggs/lars)
44
[![Coverage Status](https://coveralls.io/repos/github/go-playground/lars/badge.svg?branch=master)](https://coveralls.io/github/go-playground/lars?branch=master)
55
[![Go Report Card](https://goreportcard.com/badge/go-playground/lars)](https://goreportcard.com/report/go-playground/lars)
@@ -117,20 +117,6 @@ type MyContext struct {
117117
*lars.Ctx // a little dash of Duck Typing....
118118
}
119119

120-
// RequestStart overriding
121-
func (mc *MyContext) RequestStart(w http.ResponseWriter, r *http.Request) {
122-
mc.Ctx.RequestStart(w, r) // MUST be called!
123-
124-
// do whatever you need to on request start, db connections, variable init...
125-
}
126-
127-
// RequestEnd overriding
128-
func (mc *MyContext) RequestEnd() {
129-
// do whatever you need on request finish, reset variables, db connections...
130-
131-
mc.Ctx.RequestEnd() // MUST be called!
132-
}
133-
134120
// CustomContextFunction is a function that is specific to your applications needs that you added
135121
func (mc *MyContext) CustomContextFunction() {
136122
// do something
@@ -258,7 +244,7 @@ recovery middleware are very application dependent and therefore will be listed
258244

259245
Benchmarks
260246
-----------
261-
Run on MacBook Pro (Retina, 15-inch, Late 2013) 2.6 GHz Intel Core i7 16 GB 1600 MHz DDR3 using Go version go1.7 darwin/amd64
247+
Run on MacBook Pro (15-inch, 2017) 3.1 GHz Intel Core i7 16GB DDR3 using Go version go1.9.2 darwin/amd64
262248

263249
NOTICE: lars uses a custom version of [httprouter](https://github.com/julienschmidt/httprouter), benchmarks can be found [here](https://github.com/joeybloggs/go-http-routing-benchmark/tree/lars-only)
264250

@@ -276,22 +262,25 @@ go test -bench=. -benchmem=true
276262
#Static Routes: 157
277263
LARS: 30120 Bytes
278264

279-
BenchmarkLARS_Param 20000000 70.2 ns/op 0 B/op 0 allocs/op
280-
BenchmarkLARS_Param5 20000000 104 ns/op 0 B/op 0 allocs/op
281-
BenchmarkLARS_Param20 5000000 248 ns/op 0 B/op 0 allocs/op
282-
BenchmarkLARS_ParamWrite 10000000 134 ns/op 0 B/op 0 allocs/op
283-
BenchmarkLARS_GithubStatic 20000000 84.2 ns/op 0 B/op 0 allocs/op
284-
BenchmarkLARS_GithubParam 10000000 129 ns/op 0 B/op 0 allocs/op
285-
BenchmarkLARS_GithubAll 50000 25334 ns/op 0 B/op 0 allocs/op
286-
BenchmarkLARS_GPlusStatic 20000000 67.0 ns/op 0 B/op 0 allocs/op
287-
BenchmarkLARS_GPlusParam 20000000 84.5 ns/op 0 B/op 0 allocs/op
288-
BenchmarkLARS_GPlus2Params 20000000 103 ns/op 0 B/op 0 allocs/op
289-
BenchmarkLARS_GPlusAll 1000000 1135 ns/op 0 B/op 0 allocs/op
290-
BenchmarkLARS_ParseStatic 20000000 67.5 ns/op 0 B/op 0 allocs/op
291-
BenchmarkLARS_ParseParam 20000000 74.0 ns/op 0 B/op 0 allocs/op
292-
BenchmarkLARS_Parse2Params 20000000 86.9 ns/op 0 B/op 0 allocs/op
293-
BenchmarkLARS_ParseAll 1000000 2029 ns/op 0 B/op 0 allocs/op
294-
BenchmarkLARS_StaticAll 100000 18157 ns/op 0 B/op 0 allocs/op
265+
goos: darwin
266+
goarch: amd64
267+
pkg: github.com/joeybloggs/go-http-routing-benchmark
268+
BenchmarkLARS_Param 20000000 51.6 ns/op 0 B/op 0 allocs/op
269+
BenchmarkLARS_Param5 20000000 85.7 ns/op 0 B/op 0 allocs/op
270+
BenchmarkLARS_Param20 10000000 215 ns/op 0 B/op 0 allocs/op
271+
BenchmarkLARS_ParamWrite 20000000 94.3 ns/op 0 B/op 0 allocs/op
272+
BenchmarkLARS_GithubStatic 20000000 68.7 ns/op 0 B/op 0 allocs/op
273+
BenchmarkLARS_GithubParam 20000000 103 ns/op 0 B/op 0 allocs/op
274+
BenchmarkLARS_GithubAll 100000 21066 ns/op 0 B/op 0 allocs/op
275+
BenchmarkLARS_GPlusStatic 30000000 53.1 ns/op 0 B/op 0 allocs/op
276+
BenchmarkLARS_GPlusParam 20000000 70.3 ns/op 0 B/op 0 allocs/op
277+
BenchmarkLARS_GPlus2Params 20000000 84.4 ns/op 0 B/op 0 allocs/op
278+
BenchmarkLARS_GPlusAll 2000000 894 ns/op 0 B/op 0 allocs/op
279+
BenchmarkLARS_ParseStatic 20000000 53.5 ns/op 0 B/op 0 allocs/op
280+
BenchmarkLARS_ParseParam 20000000 60.4 ns/op 0 B/op 0 allocs/op
281+
BenchmarkLARS_Parse2Params 20000000 68.7 ns/op 0 B/op 0 allocs/op
282+
BenchmarkLARS_ParseAll 1000000 1602 ns/op 0 B/op 0 allocs/op
283+
BenchmarkLARS_StaticAll 100000 13777 ns/op 0 B/op 0 allocs/op
295284
```
296285

297286
Package Versioning

_examples/all-in-one/main.go

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,6 @@ type ApplicationGlobals struct {
2222
// .......
2323
}
2424

25-
// Reset gets called just before a new HTTP request starts calling
26-
// middleware + handlers
27-
func (g *ApplicationGlobals) Reset() {
28-
// DB = new database connection or reset....
29-
//
30-
// We don't touch translator + log as they don't change per request
31-
}
32-
33-
// Done gets called after the HTTP request has completed right before
34-
// Context gets put back into the pool
35-
func (g *ApplicationGlobals) Done() {
36-
// DB.Close()
37-
}
38-
3925
func newGlobals() *ApplicationGlobals {
4026

4127
logger := log.New(os.Stdout, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
@@ -55,25 +41,10 @@ func newGlobals() *ApplicationGlobals {
5541

5642
// MyContext is a custom context
5743
type MyContext struct {
58-
*lars.Ctx // a little dash of Duck Typing....
44+
*lars.Ctx // a little embedding
5945
AppContext *ApplicationGlobals
6046
}
6147

62-
// RequestStart overriding
63-
func (mc *MyContext) RequestStart(w http.ResponseWriter, r *http.Request) {
64-
65-
// call lars context reset, must be done
66-
67-
mc.Ctx.RequestStart(w, r) // MUST be called!
68-
mc.AppContext.Reset()
69-
}
70-
71-
// RequestEnd overriding
72-
func (mc *MyContext) RequestEnd() {
73-
mc.AppContext.Done()
74-
mc.Ctx.RequestEnd() // MUST be called!
75-
}
76-
7748
func newContext(l *lars.LARS) lars.Context {
7849
return &MyContext{
7950
Ctx: lars.NewContext(l),

context_17.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,22 @@ func (c *Ctx) RequestStart(w http.ResponseWriter, r *http.Request) {
8080
}
8181

8282
// Set is used to store a new key/value pair using the
83-
// golang.org/x/net/context contained on this Context.
83+
// context contained on this Ctx.
8484
// It is a shortcut for context.WithValue(..., ...)
8585
func (c *Ctx) Set(key interface{}, value interface{}) {
86-
c.request = c.request.WithContext(context.WithValue(c.request.Context(), key, value)) // temporarily shallow copying to avoid problems with external libraries
86+
c.request = c.request.WithContext(context.WithValue(c.request.Context(), key, value))
8787
}
8888

8989
// Get returns the value for the given key and is a shortcut
90-
// for the golang.org/x/net/context context.Value(...) ... but it
90+
// for context.Value(...) ... but it
9191
// also returns if the value was found or not.
9292
func (c *Ctx) Get(key interface{}) (value interface{}, exists bool) {
9393
value = c.request.Context().Value(key)
9494
exists = value != nil
9595
return
9696
}
9797

98-
// golang.org/x/net/context functions to comply with context.Context interface and keep context update on lars.Context object
98+
// context functions to comply with context.Context interface and keep context update on lars.Context object
9999

100100
// Context returns the request's context. To change the context, use
101101
// WithContext.
@@ -108,56 +108,56 @@ func (c *Ctx) Context() context.Context {
108108
// WithContext updates the underlying request's context with to ctx
109109
// The provided ctx must be non-nil.
110110
func (c *Ctx) WithContext(ctx context.Context) {
111-
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
111+
c.request = c.request.WithContext(ctx)
112112
}
113113

114-
// Deadline calls the underlying golang.org/x/net/context Deadline()
114+
// Deadline calls the underlying context.Deadline()
115115
func (c *Ctx) Deadline() (deadline time.Time, ok bool) {
116116
return c.request.Context().Deadline()
117117
}
118118

119-
// Done calls the underlying golang.org/x/net/context Done()
119+
// Done calls the underlying context.Done()
120120
func (c *Ctx) Done() <-chan struct{} {
121121
return c.request.Context().Done()
122122
}
123123

124-
// Err calls the underlying golang.org/x/net/context Err()
124+
// Err calls the underlying context.Err()
125125
func (c *Ctx) Err() error {
126126
return c.request.Context().Err()
127127
}
128128

129-
// Value calls the underlying golang.org/x/net/context Value()
129+
// Value calls the underlying context.Value()
130130
func (c *Ctx) Value(key interface{}) interface{} {
131131
return c.request.Context().Value(key)
132132
}
133133

134-
// WithCancel calls golang.org/x/net/context WithCancel and automatically
135-
// updates context on the containing las.Context object.
134+
// WithCancel calls context.WithCancel and automatically
135+
// updates context on the containing lars.Ctx object.
136136
func (c *Ctx) WithCancel() context.CancelFunc {
137137
ctx, cf := context.WithCancel(c.request.Context())
138-
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
138+
c.request = c.request.WithContext(ctx)
139139
return cf
140140
}
141141

142-
// WithDeadline calls golang.org/x/net/context WithDeadline and automatically
143-
// updates context on the containing las.Context object.
142+
// WithDeadline calls context.WithDeadline and automatically
143+
// updates context on the containing lars.Ctx object.
144144
func (c *Ctx) WithDeadline(deadline time.Time) context.CancelFunc {
145145
ctx, cf := context.WithDeadline(c.request.Context(), deadline)
146-
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
146+
c.request = c.request.WithContext(ctx)
147147
return cf
148148
}
149149

150-
// WithTimeout calls golang.org/x/net/context WithTimeout and automatically
151-
// updates context on the containing las.Context object.
150+
// WithTimeout calls context.WithTimeout and automatically
151+
// updates context on the containing lars.Ctx object.
152152
func (c *Ctx) WithTimeout(timeout time.Duration) context.CancelFunc {
153153
ctx, cf := context.WithTimeout(c.request.Context(), timeout)
154154
c.request = c.request.WithContext(ctx) // temporarily shallow copying to avoid problems with external libraries
155155
return cf
156156
}
157157

158158
// WithValue calls golang.org/x/net/context WithValue and automatically
159-
// updates context on the containing las.Context object.
160-
// Can also use Set() function on Context object (Recommended)
159+
// updates context on the containing lars.Ctx object.
160+
// Can also use Set() function on Ctx object (Recommended)
161161
func (c *Ctx) WithValue(key interface{}, val interface{}) {
162162
c.request = c.request.WithContext(context.WithValue(c.request.Context(), key, val)) // temporarily shallow copying to avoid problems with external libraries
163163
}

doc.go

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -92,21 +92,6 @@ example context + custom handlers
9292
*lars.Ctx // a little dash of Duck Typing....
9393
}
9494
95-
// RequestStart overriding
96-
func (mc *MyContext) RequestStart(w http.ResponseWriter, r *http.Request) {
97-
mc.Ctx.RequestStart(w, r) // MUST be called!
98-
99-
// do whatever you need to on request start, db connections, variable init...
100-
}
101-
102-
// RequestEnd overriding
103-
func (mc *MyContext) RequestEnd() {
104-
105-
// do whatever you need on request finish, reset variables, db connections...
106-
107-
mc.Ctx.RequestEnd() // MUST be called!
108-
}
109-
11095
// CustomContextFunction is a function that is specific to your applications
11196
// needs that you added
11297
func (mc *MyContext) CustomContextFunction() {

0 commit comments

Comments
 (0)