mirror of https://github.com/status-im/consul.git
api: Add a context to ServiceRegisterOpts
So that operations can timeout instead of hanging forever
This commit is contained in:
parent
119e945c3e
commit
d083d4717b
15
api/agent.go
15
api/agent.go
|
@ -3,6 +3,7 @@ package api
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -264,12 +265,23 @@ type AgentServiceRegistration struct {
|
||||||
Namespace string `json:",omitempty" bexpr:"-" hash:"ignore"`
|
Namespace string `json:",omitempty" bexpr:"-" hash:"ignore"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//ServiceRegisterOpts is used to pass extra options to the service register.
|
// ServiceRegisterOpts is used to pass extra options to the service register.
|
||||||
type ServiceRegisterOpts struct {
|
type ServiceRegisterOpts struct {
|
||||||
//Missing healthchecks will be deleted from the agent.
|
//Missing healthchecks will be deleted from the agent.
|
||||||
//Using this parameter allows to idempotently register a service and its checks without
|
//Using this parameter allows to idempotently register a service and its checks without
|
||||||
//having to manually deregister checks.
|
//having to manually deregister checks.
|
||||||
ReplaceExistingChecks bool
|
ReplaceExistingChecks bool
|
||||||
|
|
||||||
|
// ctx is an optional context pass through to the underlying HTTP
|
||||||
|
// request layer. Use WithContext() to set the context.
|
||||||
|
ctx context.Context
|
||||||
|
}
|
||||||
|
|
||||||
|
// WithContext sets the context to be used for the request on a new ServiceRegisterOpts,
|
||||||
|
// and returns the opts.
|
||||||
|
func (o ServiceRegisterOpts) WithContext(ctx context.Context) ServiceRegisterOpts {
|
||||||
|
o.ctx = ctx
|
||||||
|
return o
|
||||||
}
|
}
|
||||||
|
|
||||||
// AgentCheckRegistration is used to register a new check
|
// AgentCheckRegistration is used to register a new check
|
||||||
|
@ -686,6 +698,7 @@ func (a *Agent) ServiceRegisterOpts(service *AgentServiceRegistration, opts Serv
|
||||||
func (a *Agent) serviceRegister(service *AgentServiceRegistration, opts ServiceRegisterOpts) error {
|
func (a *Agent) serviceRegister(service *AgentServiceRegistration, opts ServiceRegisterOpts) error {
|
||||||
r := a.c.newRequest("PUT", "/v1/agent/service/register")
|
r := a.c.newRequest("PUT", "/v1/agent/service/register")
|
||||||
r.obj = service
|
r.obj = service
|
||||||
|
r.ctx = opts.ctx
|
||||||
if opts.ReplaceExistingChecks {
|
if opts.ReplaceExistingChecks {
|
||||||
r.params.Set("replace-existing-checks", "true")
|
r.params.Set("replace-existing-checks", "true")
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
@ -208,7 +210,9 @@ func TestAPI_AgentServiceAndReplaceChecks(t *testing.T) {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := agent.ServiceRegisterOpts(regupdate, ServiceRegisterOpts{ReplaceExistingChecks: true}); err != nil {
|
ctx := context.Background()
|
||||||
|
opts := ServiceRegisterOpts{ReplaceExistingChecks: true}.WithContext(ctx)
|
||||||
|
if err := agent.ServiceRegisterOpts(regupdate, opts); err != nil {
|
||||||
t.Fatalf("err: %v", err)
|
t.Fatalf("err: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,6 +251,18 @@ func TestAPI_AgentServiceAndReplaceChecks(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestAgent_ServiceRegisterOpts_WithContextTimeout(t *testing.T) {
|
||||||
|
c, err := NewClient(DefaultConfig())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
|
||||||
|
t.Cleanup(cancel)
|
||||||
|
|
||||||
|
opts := ServiceRegisterOpts{}.WithContext(ctx)
|
||||||
|
err = c.Agent().ServiceRegisterOpts(&AgentServiceRegistration{}, opts)
|
||||||
|
require.True(t, errors.Is(err, context.DeadlineExceeded), "expected timeout")
|
||||||
|
}
|
||||||
|
|
||||||
func TestAPI_AgentServices(t *testing.T) {
|
func TestAPI_AgentServices(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
c, s := makeClient(t)
|
c, s := makeClient(t)
|
||||||
|
|
Loading…
Reference in New Issue