@@ -9,11 +9,14 @@ import (
99
1010 "github.com/hashicorp/consul/api"
1111 "github.com/hashicorp/consul/command/connect/proxy"
12+ log "github.com/sirupsen/logrus"
1213)
1314
1415const (
15- defaultDownstreamBindAddr = "0.0.0.0"
16- defaultUpstreamBindAddr = "127.0.0.1"
16+ DefaultDownstreamBindAddr = "0.0.0.0"
17+ DefaultUpstreamBindAddr = "127.0.0.1"
18+ DefaultReadTimeout = 60 * time .Second
19+ DefaultConnectTimeout = 30 * time .Second
1720
1821 errorWaitTime = 5 * time .Second
1922 preparedQueryPollInterval = 30 * time .Second
@@ -26,6 +29,8 @@ type upstream struct {
2629 Datacenter string
2730 Protocol string
2831 Nodes []* api.ServiceEntry
32+ ReadTimeout time.Duration
33+ ConnectTimeout time.Duration
2934
3035 done bool
3136}
@@ -38,6 +43,8 @@ type downstream struct {
3843 TargetPort int
3944 EnableForwardFor bool
4045 AppNameHeaderName string
46+ ReadTimeout time.Duration
47+ ConnectTimeout time.Duration
4148}
4249
4350type certLeaf struct {
@@ -115,9 +122,11 @@ func (w *Watcher) Run() error {
115122}
116123
117124func (w * Watcher ) handleProxyChange (first bool , srv * api.AgentService ) {
118- w .downstream .LocalBindAddress = defaultDownstreamBindAddr
125+ w .downstream .LocalBindAddress = DefaultDownstreamBindAddr
119126 w .downstream .LocalBindPort = srv .Port
120- w .downstream .TargetAddress = defaultUpstreamBindAddr
127+ w .downstream .TargetAddress = DefaultUpstreamBindAddr
128+ w .downstream .ReadTimeout = DefaultReadTimeout
129+ w .downstream .ConnectTimeout = DefaultConnectTimeout
121130
122131 if srv .Proxy != nil && srv .Proxy .Config != nil {
123132 if c , ok := srv .Proxy .Config ["protocol" ].(string ); ok {
@@ -135,6 +144,22 @@ func (w *Watcher) handleProxyChange(first bool, srv *api.AgentService) {
135144 if a , ok := srv .Proxy .Config ["appname_header" ].(string ); ok {
136145 w .downstream .AppNameHeaderName = a
137146 }
147+ if a , ok := srv .Proxy .Config ["connect_timeout" ].(string ); ok {
148+ to , err := time .ParseDuration (a )
149+ if err != nil {
150+ log .Errorf ("bad connect_timeout value in config: %s. Using default: %s" , err , DefaultConnectTimeout )
151+ } else {
152+ w .downstream .ConnectTimeout = to
153+ }
154+ }
155+ if a , ok := srv .Proxy .Config ["read_timeout" ].(string ); ok {
156+ to , err := time .ParseDuration (a )
157+ if err != nil {
158+ log .Errorf ("bad read_timeout value in config: %s. Using default: %s" , err , DefaultReadTimeout )
159+ } else {
160+ w .downstream .ReadTimeout = to
161+ }
162+ }
138163 }
139164
140165 keep := make (map [string ]bool )
@@ -168,20 +193,46 @@ func (w *Watcher) handleProxyChange(first bool, srv *api.AgentService) {
168193 }
169194}
170195
171- func (w * Watcher ) startUpstreamService (up api.Upstream , name string ) {
172- w .log .Infof ("consul: watching upstream for service %s" , up .DestinationName )
173-
196+ func (w * Watcher ) buildUpstream (up api.Upstream , name string ) * upstream {
174197 u := & upstream {
175198 LocalBindAddress : up .LocalBindAddress ,
176199 LocalBindPort : up .LocalBindPort ,
177200 Name : name ,
178201 Datacenter : up .Datacenter ,
202+ ReadTimeout : DefaultReadTimeout ,
203+ ConnectTimeout : DefaultConnectTimeout ,
179204 }
180205
181206 if p , ok := up .Config ["protocol" ].(string ); ok {
182207 u .Protocol = p
183208 }
184209
210+ if a , ok := up .Config ["read_timeout" ].(string ); ok {
211+ to , err := time .ParseDuration (a )
212+ if err != nil {
213+ log .Errorf ("upstream %s: bad read_timeout value in config: %s. Using default: %s" , name , err , DefaultReadTimeout )
214+ } else {
215+ u .ReadTimeout = to
216+ }
217+ }
218+
219+ if a , ok := up .Config ["connect_timeout" ].(string ); ok {
220+ to , err := time .ParseDuration (a )
221+ if err != nil {
222+ log .Errorf ("upstream %s: bad connect_timeout value in config: %s. Using default: %s" , name , err , DefaultConnectTimeout )
223+ } else {
224+ u .ConnectTimeout = to
225+ }
226+ }
227+
228+ return u
229+ }
230+
231+ func (w * Watcher ) startUpstreamService (up api.Upstream , name string ) {
232+ w .log .Infof ("consul: watching upstream for service %s" , up .DestinationName )
233+
234+ u := w .buildUpstream (up , name )
235+
185236 w .lock .Lock ()
186237 w .upstreams [name ] = u
187238 w .lock .Unlock ()
@@ -219,16 +270,7 @@ func (w *Watcher) startUpstreamService(up api.Upstream, name string) {
219270func (w * Watcher ) startUpstreamPreparedQuery (up api.Upstream , name string ) {
220271 w .log .Infof ("consul: watching upstream for prepared_query %s" , up .DestinationName )
221272
222- u := & upstream {
223- LocalBindAddress : up .LocalBindAddress ,
224- LocalBindPort : up .LocalBindPort ,
225- Name : name ,
226- Datacenter : up .Datacenter ,
227- }
228-
229- if p , ok := up .Config ["protocol" ].(string ); ok {
230- u .Protocol = p
231- }
273+ u := w .buildUpstream (up , name )
232274
233275 interval := preparedQueryPollInterval
234276 if p , ok := up .Config ["poll_interval" ].(string ); ok {
@@ -429,6 +471,8 @@ func (w *Watcher) genCfg() Config {
429471 TargetAddress : w .downstream .TargetAddress ,
430472 TargetPort : w .downstream .TargetPort ,
431473 Protocol : w .downstream .Protocol ,
474+ ConnectTimeout : w .downstream .ConnectTimeout ,
475+ ReadTimeout : w .downstream .ReadTimeout ,
432476 EnableForwardFor : w .downstream .EnableForwardFor ,
433477 AppNameHeaderName : w .downstream .AppNameHeaderName ,
434478
@@ -446,6 +490,8 @@ func (w *Watcher) genCfg() Config {
446490 LocalBindAddress : up .LocalBindAddress ,
447491 LocalBindPort : up .LocalBindPort ,
448492 Protocol : up .Protocol ,
493+ ConnectTimeout : up .ConnectTimeout ,
494+ ReadTimeout : up .ConnectTimeout ,
449495 TLS : TLS {
450496 CAs : w .certCAs ,
451497 Cert : w .leaf .Cert ,
0 commit comments