consul: fix obscure bug when launching goroutines from for loop

This commit is contained in:
Ryan Uber 2014-10-08 13:28:59 -07:00
parent 0cafb129ee
commit a662acd794
4 changed files with 6 additions and 23 deletions

View File

@ -71,13 +71,13 @@ func (m *Internal) KeyringOperation(
args *structs.KeyringRequest,
reply *structs.KeyringResponses) error {
m.executeKeyringOp(args, reply, false)
if !args.Forwarded {
args.Forwarded = true
m.executeKeyringOp(args, reply, true)
return m.srv.globalRPC("Internal.KeyringOperation", args, reply)
}
m.executeKeyringOp(args, reply, false)
return nil
}

View File

@ -229,29 +229,23 @@ func (s *Server) forwardDC(method, dc string, args interface{}, reply interface{
func (s *Server) globalRPC(method string, args interface{},
reply structs.CompoundResponse) error {
totalDC := len(s.remoteConsuls)
if totalDC == 1 {
return nil
}
errorCh := make(chan error)
respCh := make(chan interface{})
// Make a new request into each datacenter
for dc, _ := range s.remoteConsuls {
info := &structs.GenericRPC{Datacenter: dc}
go func() {
go func(dc string) {
rr := reply.New()
if _, err := s.forward(method, info, args, &rr); err != nil {
if err := s.forwardDC(method, dc, args, &rr); err != nil {
errorCh <- err
return
}
respCh <- rr
}()
}(dc)
}
replies := 0
for replies < totalDC {
replies, total := 0, len(s.remoteConsuls)
for replies < total {
select {
case err := <-errorCh:
return err

View File

@ -127,16 +127,6 @@ type QueryMeta struct {
KnownLeader bool
}
// GenericRPC is the simplest possible RPCInfo implementation
type GenericRPC struct {
Datacenter string
QueryOptions
}
func (r *GenericRPC) RequestDatacenter() string {
return r.Datacenter
}
// RegisterRequest is used for the Catalog.Register endpoint
// to register a node as providing a service. If no service
// is provided, the node is registered.

View File

@ -35,7 +35,6 @@ func TestEncodeDecode(t *testing.T) {
func TestStructs_Implements(t *testing.T) {
var (
_ RPCInfo = &GenericRPC{}
_ RPCInfo = &RegisterRequest{}
_ RPCInfo = &DeregisterRequest{}
_ RPCInfo = &DCSpecificRequest{}