Fix proxy selector memory leak
This commit is contained in:
		| @@ -20,6 +20,7 @@ import ( | |||||||
| 	"github.com/micro/go-micro/v3/proxy" | 	"github.com/micro/go-micro/v3/proxy" | ||||||
| 	"github.com/micro/go-micro/v3/router" | 	"github.com/micro/go-micro/v3/router" | ||||||
| 	"github.com/micro/go-micro/v3/router/registry" | 	"github.com/micro/go-micro/v3/router/registry" | ||||||
|  | 	"github.com/micro/go-micro/v3/selector" | ||||||
| 	"github.com/micro/go-micro/v3/selector/roundrobin" | 	"github.com/micro/go-micro/v3/selector/roundrobin" | ||||||
| 	"github.com/micro/go-micro/v3/server" | 	"github.com/micro/go-micro/v3/server" | ||||||
| ) | ) | ||||||
| @@ -45,6 +46,9 @@ type Proxy struct { | |||||||
| 	// A fib of routes service:address | 	// A fib of routes service:address | ||||||
| 	sync.RWMutex | 	sync.RWMutex | ||||||
| 	Routes map[string]map[uint64]router.Route | 	Routes map[string]map[uint64]router.Route | ||||||
|  |  | ||||||
|  | 	// selector used for load balancing | ||||||
|  | 	Selector selector.Selector | ||||||
| } | } | ||||||
|  |  | ||||||
| // read client request and write to server | // read client request and write to server | ||||||
| @@ -402,7 +406,7 @@ func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server | |||||||
| 	//nolint:prealloc | 	//nolint:prealloc | ||||||
| 	opts := []client.CallOption{ | 	opts := []client.CallOption{ | ||||||
| 		// set strategy to round robin | 		// set strategy to round robin | ||||||
| 		client.WithSelector(roundrobin.NewSelector()), | 		client.WithSelector(p.Selector), | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	// if the address is already set just serve it | 	// if the address is already set just serve it | ||||||
| @@ -608,6 +612,11 @@ func NewProxy(opts ...proxy.Option) proxy.Proxy { | |||||||
| 	if p.Router == nil { | 	if p.Router == nil { | ||||||
| 		p.Router = registry.NewRouter() | 		p.Router = registry.NewRouter() | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if p.Selector == nil { | ||||||
|  | 		p.Selector = roundrobin.NewSelector() | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	// set the links | 	// set the links | ||||||
| 	if options.Links != nil { | 	if options.Links != nil { | ||||||
| 		// get client | 		// get client | ||||||
|   | |||||||
| @@ -93,12 +93,7 @@ func (r *roundrobin) String() string { | |||||||
| } | } | ||||||
|  |  | ||||||
| func (r *roundrobin) cleanRoutes() { | func (r *roundrobin) cleanRoutes() { | ||||||
| 	for { | 	for _ = range r.ticker.C { | ||||||
| 		// watch for ticks until the ticker is closed |  | ||||||
| 		if _, ok := <-r.ticker.C; !ok { |  | ||||||
| 			return |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		r.Lock() | 		r.Lock() | ||||||
|  |  | ||||||
| 		// copy the slice to prevent concurrent map iteration and map write | 		// copy the slice to prevent concurrent map iteration and map write | ||||||
| @@ -109,6 +104,7 @@ func (r *roundrobin) cleanRoutes() { | |||||||
| 				delete(r.routes, hash) | 				delete(r.routes, hash) | ||||||
| 			} | 			} | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		r.Unlock() | 		r.Unlock() | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user