Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MiddlewareChain implements Middleware interface #87

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions middleware/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package middleware
import (
"fmt"
. "github.com/mailgun/vulcan/request"
"net/http"
"sort"
"sync"
)
Expand Down Expand Up @@ -49,6 +50,33 @@ func (c *MiddlewareChain) GetIter() *MiddlewareIter {
}
}

func (c *MiddlewareChain) ProcessRequest(r Request) (*http.Response, error) {
it := c.chain.getIter()
for v := it.next(); v != nil; v = it.next() {
// Track how deep we've gotten in the middleware chain
c.chain.callDepth = it.index + 1

resp, err := v.(Middleware).ProcessRequest(r)
if resp != nil || err != nil {
break
return resp, err
}
}
return nil, nil
}

func (c *MiddlewareChain) ProcessResponse(r Request, a Attempt) {
it := c.chain.getReverseIter()

// Set the iterator to start at the furthest spot we got in the chain during ProcessRequest
currentIndex := c.chain.callDepth
it.index = len(it.callbacks) - currentIndex

for v := it.next(); v != nil; v = it.next() {
v.(Middleware).ProcessResponse(r, a)
}
}

type MiddlewareIter struct {
iter *iter
}
Expand Down Expand Up @@ -122,6 +150,7 @@ func (c *ObserverChain) ObserveResponse(r Request, a Attempt) {
type chain struct {
mutex *sync.RWMutex
callbacks []*callback
callDepth int // how deep into the chain we get on ProcessRequest / ObserveRequest. 1 based
indexes map[string]int // Indexes for in place updates
iter *iter //current version of iterator
}
Expand Down