Skip to content

Commit

Permalink
Added response body modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
akyoto committed Jun 3, 2019
1 parent 2210a92 commit b7c5c9d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 19 deletions.
60 changes: 41 additions & 19 deletions Context.go
Expand Up @@ -20,19 +20,25 @@ import (
jsoniter "github.com/json-iterator/go"
)

// This should be close to the MTU size of a TCP packet.
// Regarding performance it makes no sense to compress smaller files.
// Bandwidth can be saved however the savings are minimal for small files
// and the overhead of compressing can lead up to a 75% reduction
// in server speed under high load. Therefore in this case
// we're trying to optimize for performance, not bandwidth.
const gzipThreshold = 1450

// This defines the maximum number of parameters per route.
const maxParams = 16
const (
// gzipThreshold should be close to the MTU size of a TCP packet.
// Regarding performance it makes no sense to compress smaller files.
// Bandwidth can be saved however the savings are minimal for small files
// and the overhead of compressing can lead up to a 75% reduction
// in server speed under high load. Therefore in this case
// we're trying to optimize for performance, not bandwidth.
gzipThreshold = 1450

// maxParams defines the maximum number of parameters per route.
maxParams = 16

// maxModifiers defines the maximum number of modifiers per context.
maxModifiers = 4
)

// Context represents the interface for a request & response context.
type Context interface {
AddModifier(Modifier)
App() *Application
Bytes([]byte) error
Close()
Expand Down Expand Up @@ -66,15 +72,24 @@ type Context interface {

// context represents a request & response context.
type context struct {
app *Application
status int
request request
response response
session *session.Session
handler Handler
paramNames [maxParams]string
paramValues [maxParams]string
paramCount int
app *Application
status int
request request
response response
session *session.Session
handler Handler
paramNames [maxParams]string
paramValues [maxParams]string
paramCount int
modifiers [maxModifiers]Modifier
modifierCount int
}

// AddModifier adds a modifier that can change the response body
// contents of in-memory responses before the actual response happens.
func (ctx *context) AddModifier(modifier Modifier) {
ctx.modifiers[ctx.modifierCount] = modifier
ctx.modifierCount++
}

// App returns the application the context occurred in.
Expand All @@ -90,6 +105,13 @@ func (ctx *context) Bytes(body []byte) error {
return errors.New("Request interrupted by the client")
}

// If we registered any response body modifiers, invoke them.
if ctx.modifierCount > 0 {
for i := 0; i < ctx.modifierCount; i++ {
body = ctx.modifiers[i](body)
}
}

// Small response
if len(body) < gzipThreshold {
ctx.response.inner.WriteHeader(ctx.status)
Expand Down
5 changes: 5 additions & 0 deletions Modifier.go
@@ -0,0 +1,5 @@
package aero

// Modifier is a function that modifies the
// response body before it is sent to the client.
type Modifier = func([]byte) []byte

0 comments on commit b7c5c9d

Please sign in to comment.