Browse Source

client: protect httpClusterClient with RWMutex

Brian Waldon 11 years ago
parent
commit
99d63eb62e
1 changed files with 10 additions and 0 deletions
  1. 10 0
      client/http.go

+ 10 - 0
client/http.go

@@ -20,6 +20,7 @@ import (
 	"io/ioutil"
 	"io/ioutil"
 	"net/http"
 	"net/http"
 	"net/url"
 	"net/url"
+	"sync"
 	"time"
 	"time"
 
 
 	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
 	"github.com/coreos/etcd/Godeps/_workspace/src/golang.org/x/net/context"
@@ -78,6 +79,7 @@ type httpClusterClient struct {
 	transport CancelableTransport
 	transport CancelableTransport
 	endpoints []string
 	endpoints []string
 	clients   []HTTPClient
 	clients   []HTTPClient
+	sync.RWMutex
 }
 }
 
 
 func (c *httpClusterClient) reset(tr CancelableTransport, eps []string) error {
 func (c *httpClusterClient) reset(tr CancelableTransport, eps []string) error {
@@ -111,6 +113,9 @@ func (c *httpClusterClient) reset(tr CancelableTransport, eps []string) error {
 }
 }
 
 
 func (c *httpClusterClient) Do(ctx context.Context, act HTTPAction) (resp *http.Response, body []byte, err error) {
 func (c *httpClusterClient) Do(ctx context.Context, act HTTPAction) (resp *http.Response, body []byte, err error) {
+	c.RLock()
+	defer c.RUnlock()
+
 	if len(c.clients) == 0 {
 	if len(c.clients) == 0 {
 		return nil, nil, ErrNoEndpoints
 		return nil, nil, ErrNoEndpoints
 	}
 	}
@@ -131,10 +136,15 @@ func (c *httpClusterClient) Do(ctx context.Context, act HTTPAction) (resp *http.
 }
 }
 
 
 func (c *httpClusterClient) Endpoints() []string {
 func (c *httpClusterClient) Endpoints() []string {
+	c.RLock()
+	defer c.RUnlock()
 	return c.endpoints
 	return c.endpoints
 }
 }
 
 
 func (c *httpClusterClient) Sync(ctx context.Context) error {
 func (c *httpClusterClient) Sync(ctx context.Context) error {
+	c.Lock()
+	defer c.Unlock()
+
 	mAPI := NewMembersAPI(c)
 	mAPI := NewMembersAPI(c)
 	ms, err := mAPI.List(ctx)
 	ms, err := mAPI.List(ctx)
 	if err != nil {
 	if err != nil {