mw: github.com/morhekil/mw/chaotic Index | Examples | Files | Directories

package chaotic

import "github.com/morhekil/mw/chaotic"

Package chaotic provides stdlib-compatible middleware to inject configurable delays and failures into the requests processed by its underlying HTTP stack.

It comes with a web page to monitor and configure its behaviour, which also includes a simple visualisation of requests going through the stack, and failures introduced.

An example of a configuration page in action can be found on GitHub (https://github.com/morhekil/mw/tree/master/chaotic#chaotic).

The minimum viable example of an application with chaotic middleware installed could be the following:

app := http.NewServeMux()
app.Handle("/", http.NotFoundHandler())

http.ListenAndServe(":1234",
	// wrap application handler with chaotic.H,
	// installing its pages under /chaotic URL
	mw.Chaotic("/chaotic")(app),
)

Or it can be cleanly composed with other middleware using alice (https://github.com/justinas/alice). For example, if we have middlewares called "mw.Logger" and "mw.Recover", the full stack can be composed with alice this way:

	a := alice.New(
		mw.Logger,
		mw.Chaotic("/chaotic"),
                mw.Recover,
	).Then(app)
	http.ListenAndServe(":1234", a)

Keep in mind, that chaotic will delay or fail middlewares installed after it, but will not affect middlewares installed earlier - e.g. in this example only mw.Recover middleware is affected by chaotic's behaviour, but mw.Logger will run unaffected every time. This can be used to inject the failure into the required part of the stack, or even introduce multiple points of failure by mounting their configuration pages under different URLs.

Code:

app := http.NotFoundHandler()
c := chaotic.H("/chaotic")
httptest.NewServer(c(app))

hc := http.Client{}
hc.Get("/404")     // Hits app's NotFoundHandler
hc.Get("/chaotic") // Goes to chaotic's configuration page

Index

Examples

Package Files

assets.go chaotic.go log.go policy.go policy_api.go

func H Uses

func H(url string) func(h http.Handler) http.Handler

H is a net/http handler that installs chaotic's own http routes under the given base URL, and processes the rest of the stack accordingly to the active policy.

type Action Uses

type Action struct {
    Index   int64
    Start   time.Time
    Time    time.Duration
    Delayed bool
    Failed  bool
    Text    string
}

Action performed in regards to a single request

type Policy Uses

type Policy struct {
    // Public representation of current policy settings
    Delay    string
    DelayP   float32
    FailureP float32
    // Custom function to implement the delay, defaults to time.Sleep.
    DelayFunc func(time.Duration) `json:"-"`
    // contains filtered or unexported fields
}

Policy describes the desired chaotic behaviour

func (*Policy) ServeHTTP Uses

func (p *Policy) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Policy) Validate Uses

func (p *Policy) Validate() error

Validate should be called to validate public policy data (usually - after a change), and convert it into internal policy rules, if validation has succeded.

Directories

PathSynopsis
bindata

Package chaotic imports 9 packages (graph) and is imported by 1 packages. Updated 2016-07-23. Refresh now. Tools for package owners.