mirror of
https://github.com/hanzoai/oxy.git
synced 2026-09-30 23:42:31 +00:00
Add Servers method
This commit is contained in:
@@ -110,6 +110,13 @@ func NewRebalancer(handler balancerHandler, opts ...rbOptSetter) (*Rebalancer, e
|
||||
return rb, nil
|
||||
}
|
||||
|
||||
func (rb *Rebalancer) Servers() []*url.URL {
|
||||
rb.mtx.Lock()
|
||||
defer rb.mtx.Unlock()
|
||||
|
||||
return rb.next.Servers()
|
||||
}
|
||||
|
||||
func (rb *Rebalancer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
pw := &utils.ProxyWriter{W: w}
|
||||
start := rb.clock.UtcNow()
|
||||
|
||||
@@ -44,6 +44,7 @@ func (s *RBSuite) TestRebalancerNormalOperation(c *C) {
|
||||
c.Assert(err, IsNil)
|
||||
|
||||
rb.UpsertServer(testutils.ParseURI(a.URL))
|
||||
c.Assert(rb.Servers()[0].String(), Equals, a.URL)
|
||||
|
||||
proxy := httptest.NewServer(rb)
|
||||
defer proxy.Close()
|
||||
|
||||
@@ -117,6 +117,17 @@ func (r *RoundRobin) RemoveServer(u *url.URL) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (rr *RoundRobin) Servers() []*url.URL {
|
||||
rr.mutex.Lock()
|
||||
defer rr.mutex.Unlock()
|
||||
|
||||
out := make([]*url.URL, len(rr.servers))
|
||||
for i, srv := range rr.servers {
|
||||
out[i] = srv.url
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (rr *RoundRobin) ServerWeight(u *url.URL) (int, bool) {
|
||||
rr.mutex.Lock()
|
||||
defer rr.mutex.Unlock()
|
||||
@@ -223,6 +234,7 @@ func sameURL(a, b *url.URL) bool {
|
||||
}
|
||||
|
||||
type balancerHandler interface {
|
||||
Servers() []*url.URL
|
||||
ServeHTTP(w http.ResponseWriter, req *http.Request)
|
||||
ServerWeight(u *url.URL) (int, bool)
|
||||
RemoveServer(u *url.URL) error
|
||||
|
||||
Reference in New Issue
Block a user