-
Notifications
You must be signed in to change notification settings - Fork 20
Add support for prepared queries #50
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ package consul | |
|
|
||
| import ( | ||
| "crypto/x509" | ||
| "fmt" | ||
| "reflect" | ||
| "sync" | ||
| "time" | ||
|
|
||
|
|
@@ -13,13 +15,14 @@ const ( | |
| defaultDownstreamBindAddr = "0.0.0.0" | ||
| defaultUpstreamBindAddr = "127.0.0.1" | ||
|
|
||
| errorWaitTime = 5 * time.Second | ||
| errorWaitTime = 5 * time.Second | ||
| preparedQueryPollInterval = 30 * time.Second | ||
| ) | ||
|
|
||
| type upstream struct { | ||
| LocalBindAddress string | ||
| LocalBindPort int | ||
| Service string | ||
| Name string | ||
| Datacenter string | ||
| Protocol string | ||
| Nodes []*api.ServiceEntry | ||
|
|
@@ -138,12 +141,18 @@ func (w *Watcher) handleProxyChange(first bool, srv *api.AgentService) { | |
|
|
||
| if srv.Proxy != nil { | ||
| for _, up := range srv.Proxy.Upstreams { | ||
| keep[up.DestinationName] = true | ||
| name := fmt.Sprintf("%s_%s", up.DestinationType, up.DestinationName) | ||
| keep[name] = true | ||
| w.lock.Lock() | ||
| _, ok := w.upstreams[up.DestinationName] | ||
| _, ok := w.upstreams[name] | ||
| w.lock.Unlock() | ||
| if !ok { | ||
| w.startUpstream(up) | ||
| switch up.DestinationType { | ||
| case api.UpstreamDestTypePreparedQuery: | ||
| w.startUpstreamPreparedQuery(up, name) | ||
| default: | ||
| w.startUpstreamService(up, name) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
@@ -159,24 +168,22 @@ func (w *Watcher) handleProxyChange(first bool, srv *api.AgentService) { | |
| } | ||
| } | ||
|
|
||
| func (w *Watcher) startUpstream(up api.Upstream) { | ||
| func (w *Watcher) startUpstreamService(up api.Upstream, name string) { | ||
| w.log.Infof("consul: watching upstream for service %s", up.DestinationName) | ||
|
|
||
| u := &upstream{ | ||
| LocalBindAddress: up.LocalBindAddress, | ||
| LocalBindPort: up.LocalBindPort, | ||
| Service: up.DestinationName, | ||
| Name: name, | ||
| Datacenter: up.Datacenter, | ||
| } | ||
|
|
||
| if up.Config["protocol"] != nil { | ||
| if p, ok := up.Config["protocol"].(string); ok { | ||
| u.Protocol = p | ||
| } | ||
| if p, ok := up.Config["protocol"].(string); ok { | ||
| u.Protocol = p | ||
| } | ||
|
|
||
| w.lock.Lock() | ||
| w.upstreams[up.DestinationName] = u | ||
| w.upstreams[name] = u | ||
| w.lock.Unlock() | ||
|
|
||
| go func() { | ||
|
|
@@ -209,6 +216,75 @@ func (w *Watcher) startUpstream(up api.Upstream) { | |
| }() | ||
| } | ||
|
|
||
| func (w *Watcher) startUpstreamPreparedQuery(up api.Upstream, name string) { | ||
| w.log.Infof("consul: watching upstream for prepared_query %s", up.DestinationName) | ||
|
|
||
| u := &upstream{ | ||
| LocalBindAddress: up.LocalBindAddress, | ||
| LocalBindPort: up.LocalBindPort, | ||
| Name: name, | ||
| Datacenter: up.Datacenter, | ||
| } | ||
|
|
||
| if p, ok := up.Config["protocol"].(string); ok { | ||
| u.Protocol = p | ||
| } | ||
|
|
||
| interval := preparedQueryPollInterval | ||
| if p, ok := up.Config["poll_interval"].(string); ok { | ||
| dur, err := time.ParseDuration(p) | ||
| if err != nil { | ||
| w.log.Errorf( | ||
| "consul: upstream %s %s: invalid poll interval %s: %s", | ||
| up.DestinationType, | ||
| up.DestinationName, | ||
| p, | ||
| err, | ||
| ) | ||
| return | ||
| } | ||
| interval = dur | ||
| } | ||
|
|
||
| w.lock.Lock() | ||
| w.upstreams[name] = u | ||
| w.lock.Unlock() | ||
|
|
||
| go func() { | ||
| var last []*api.ServiceEntry | ||
| for { | ||
| if u.done { | ||
| return | ||
| } | ||
| nodes, _, err := w.consul.PreparedQuery().Execute(up.DestinationName, &api.QueryOptions{ | ||
| Connect: true, | ||
| Datacenter: up.Datacenter, | ||
| WaitTime: 10 * time.Minute, | ||
| }) | ||
| if err != nil { | ||
| w.log.Errorf("consul: error fetching service definition for service %s: %s", up.DestinationName, err) | ||
| time.Sleep(errorWaitTime) | ||
| continue | ||
| } | ||
|
|
||
| nodesP := []*api.ServiceEntry{} | ||
| for i := range nodes.Nodes { | ||
| nodesP = append(nodesP, &nodes.Nodes[i]) | ||
| } | ||
|
|
||
| if !reflect.DeepEqual(last, nodesP) { | ||
| w.lock.Lock() | ||
| u.Nodes = nodesP | ||
| w.lock.Unlock() | ||
| w.notifyChanged() | ||
| last = nodesP | ||
| } | ||
|
|
||
| time.Sleep(interval) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am Ok with 30s as a default duration, but could you make it configurable ?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, sorry |
||
| } | ||
| }() | ||
| } | ||
|
|
||
| func (w *Watcher) removeUpstream(name string) { | ||
| w.log.Infof("consul: removing upstream for service %s", name) | ||
|
|
||
|
|
@@ -366,7 +442,7 @@ func (w *Watcher) genCfg() Config { | |
|
|
||
| for _, up := range w.upstreams { | ||
| upstream := Upstream{ | ||
| Service: up.Service, | ||
| Name: up.Name, | ||
| LocalBindAddress: up.LocalBindAddress, | ||
| LocalBindPort: up.LocalBindPort, | ||
| Protocol: up.Protocol, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you do a truncated binary exponential backoff instead of a fixed one ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can, however, that's how errors are handled in all the other watches. I can make this a separate PR.