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

modifier: adding modifier tag concept #69

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
11 changes: 11 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proxy

import (
"net/http"
"strings"
"time"

"github.com/eBay/fabio/config"
Expand Down Expand Up @@ -52,6 +53,16 @@ func (p *Proxy) ServeHTTP(w http.ResponseWriter, r *http.Request) {
h = newHTTPProxy(t.URL, p.tr)
}

// strip the prefix, if the target was tagged properly
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was digging through this, and it seems like the Target object could have a Modify method which accepts an http.Request object and modifies the request to its liking. In the case that we had more modifier tags, this could be quite useful

if t.StripPrefix {
pieces := strings.SplitN(r.URL.Path, "/", 3)
if len(pieces) == 3 {
r.URL.Path = pieces[2]
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs refactoring, was just getting something working 😄

} else {
r.URL.Path = "/"
}
}

start := time.Now()
h.ServeHTTP(w, r)
p.requests.UpdateSince(start)
Expand Down
22 changes: 21 additions & 1 deletion route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package route
import (
"fmt"
"net/url"
"regexp"
"sort"
"strings"

Expand All @@ -11,6 +12,8 @@ import (
gometrics "github.com/rcrowley/go-metrics"
)

var StripPrefixRegex *regexp.Regexp = regexp.MustCompile("modifier-/strip-prefix")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would love to hear thoughts on naming here. I'm not overly opinionated, I also considered urlmodifier-/strip-prefix and modifier-/strip


// Route maps a path prefix to one or more target URLs.
// routes can have a share value which describes the
// amount of traffic this route should get. You can specify
Expand Down Expand Up @@ -51,7 +54,24 @@ func (r *Route) addTarget(service string, targetURL *url.URL, fixedWeight float6
name := metrics.TargetName(service, r.Host, r.Path, targetURL)
timer := gometrics.GetOrRegisterTimer(name, metrics.ServiceRegistry)

t := &Target{Service: service, Tags: tags, URL: targetURL, FixedWeight: fixedWeight, Timer: timer, timerName: name}
stripPrefix := false
// by passing a tag of `url-prefix-host/modifier#strip-prefix you can
// drop whatever prefix the service is namespaced by
for _, tag := range tags {
if StripPrefixRegex.MatchString(tag) {
stripPrefix = true
}
}

t := &Target{
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about a NewTarget method, which accepts the raw parameters and makes some decisions based upon which tags are specified?

Service: service,
Tags: tags,
URL: targetURL,
StripPrefix: stripPrefix,
FixedWeight: fixedWeight,
Timer: timer,
timerName: name}

r.Targets = append(r.Targets, t)
r.weighTargets()
}
Expand Down
3 changes: 3 additions & 0 deletions route/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ type Target struct {
// URL is the endpoint the service instance listens on
URL *url.URL

// whether or not to strip the service prefix
StripPrefix bool

// FixedWeight is the weight assigned to this target.
// If the value is 0 the targets weight is dynamic.
FixedWeight float64
Expand Down