2019-06-03 19:44:43 +02:00
|
|
|
// Package mucp transparently forwards the incoming request using a go-micro client.
|
|
|
|
package mucp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-07-24 20:03:13 +02:00
|
|
|
"fmt"
|
2019-06-03 19:44:43 +02:00
|
|
|
"io"
|
|
|
|
"strings"
|
2019-06-27 13:56:52 +02:00
|
|
|
"sync"
|
2019-06-03 19:44:43 +02:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/client"
|
|
|
|
"github.com/micro/go-micro/codec"
|
|
|
|
"github.com/micro/go-micro/codec/bytes"
|
2019-06-12 13:45:42 +02:00
|
|
|
"github.com/micro/go-micro/config/options"
|
2019-08-05 18:44:33 +02:00
|
|
|
"github.com/micro/go-micro/proxy"
|
|
|
|
"github.com/micro/go-micro/router"
|
2019-06-03 19:44:43 +02:00
|
|
|
"github.com/micro/go-micro/server"
|
|
|
|
)
|
|
|
|
|
2019-06-07 14:42:39 +02:00
|
|
|
// Proxy will transparently proxy requests to an endpoint.
|
|
|
|
// If no endpoint is specified it will call a service using the client.
|
|
|
|
type Proxy struct {
|
|
|
|
// embed options
|
|
|
|
options.Options
|
|
|
|
|
2019-07-24 19:13:51 +02:00
|
|
|
// Endpoint specifies the fixed service endpoint to call.
|
2019-06-03 19:44:43 +02:00
|
|
|
Endpoint string
|
|
|
|
|
|
|
|
// The client to use for outbound requests
|
|
|
|
Client client.Client
|
2019-06-27 13:56:52 +02:00
|
|
|
|
|
|
|
// The router for routes
|
|
|
|
Router router.Router
|
|
|
|
|
|
|
|
// A fib of routes service:address
|
|
|
|
sync.RWMutex
|
2019-07-27 14:58:51 +02:00
|
|
|
Routes map[string]map[uint64]router.Route
|
2019-07-24 20:03:13 +02:00
|
|
|
|
|
|
|
// The channel to monitor watcher errors
|
|
|
|
errChan chan error
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read client request and write to server
|
|
|
|
func readLoop(r server.Request, s client.Stream) error {
|
|
|
|
// request to backend server
|
|
|
|
req := s.Request()
|
|
|
|
|
|
|
|
for {
|
|
|
|
// get data from client
|
|
|
|
// no need to decode it
|
|
|
|
body, err := r.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
}
|
2019-06-18 19:51:52 +02:00
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the header from client
|
|
|
|
hdr := r.Header()
|
|
|
|
msg := &codec.Message{
|
|
|
|
Type: codec.Request,
|
|
|
|
Header: hdr,
|
|
|
|
Body: body,
|
|
|
|
}
|
2019-06-18 19:51:52 +02:00
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
// write the raw request
|
|
|
|
err = req.Codec().Write(msg, nil)
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-24 19:13:51 +02:00
|
|
|
// toNodes returns a list of node addresses from given routes
|
2019-07-27 14:58:51 +02:00
|
|
|
func toNodes(routes map[uint64]router.Route) []string {
|
2019-07-24 19:13:51 +02:00
|
|
|
var nodes []string
|
|
|
|
for _, node := range routes {
|
|
|
|
address := node.Address
|
|
|
|
if len(node.Gateway) > 0 {
|
|
|
|
address = node.Gateway
|
2019-06-27 13:56:52 +02:00
|
|
|
}
|
2019-07-24 19:13:51 +02:00
|
|
|
nodes = append(nodes, address)
|
2019-06-27 13:56:52 +02:00
|
|
|
}
|
2019-07-24 19:13:51 +02:00
|
|
|
return nodes
|
|
|
|
}
|
2019-06-27 13:56:52 +02:00
|
|
|
|
2019-07-24 19:13:51 +02:00
|
|
|
func (p *Proxy) getRoute(service string) ([]string, error) {
|
2019-06-27 13:56:52 +02:00
|
|
|
// lookup the route cache first
|
2019-07-24 19:13:51 +02:00
|
|
|
p.Lock()
|
2019-06-27 13:56:52 +02:00
|
|
|
routes, ok := p.Routes[service]
|
|
|
|
if ok {
|
|
|
|
p.Unlock()
|
|
|
|
return toNodes(routes), nil
|
|
|
|
}
|
2019-07-27 14:58:51 +02:00
|
|
|
p.Routes[service] = make(map[uint64]router.Route)
|
2019-07-24 19:13:51 +02:00
|
|
|
p.Unlock()
|
2019-06-27 13:56:52 +02:00
|
|
|
|
2019-07-24 19:13:51 +02:00
|
|
|
// lookup the routes in the router
|
2019-07-27 14:58:51 +02:00
|
|
|
results, err := p.Router.Lookup(router.NewQuery(router.QueryService(service)))
|
2019-07-24 19:13:51 +02:00
|
|
|
if err != nil {
|
2019-08-12 18:06:08 +02:00
|
|
|
// check the status of the router
|
|
|
|
if status := p.Router.Status(); status.Code == router.Error {
|
|
|
|
return nil, status.Error
|
|
|
|
}
|
|
|
|
// otherwise return the error
|
2019-07-24 19:13:51 +02:00
|
|
|
return nil, err
|
2019-06-27 13:56:52 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 19:13:51 +02:00
|
|
|
// update the proxy cache
|
|
|
|
p.Lock()
|
|
|
|
for _, route := range results {
|
|
|
|
p.Routes[service][route.Hash()] = route
|
2019-06-27 13:56:52 +02:00
|
|
|
}
|
2019-07-24 19:13:51 +02:00
|
|
|
routes = p.Routes[service]
|
|
|
|
p.Unlock()
|
2019-06-27 13:56:52 +02:00
|
|
|
|
|
|
|
return toNodes(routes), nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 20:03:13 +02:00
|
|
|
// manageRouteCache applies action on a given route to Proxy route cache
|
2019-07-27 14:58:51 +02:00
|
|
|
func (p *Proxy) manageRouteCache(route router.Route, action string) error {
|
2019-07-24 20:03:13 +02:00
|
|
|
switch action {
|
|
|
|
case "create", "update":
|
|
|
|
if _, ok := p.Routes[route.Service]; !ok {
|
2019-07-27 14:58:51 +02:00
|
|
|
p.Routes[route.Service] = make(map[uint64]router.Route)
|
2019-07-24 20:03:13 +02:00
|
|
|
}
|
|
|
|
p.Routes[route.Service][route.Hash()] = route
|
|
|
|
case "delete":
|
|
|
|
if _, ok := p.Routes[route.Service]; !ok {
|
|
|
|
return fmt.Errorf("route not found")
|
|
|
|
}
|
|
|
|
delete(p.Routes[route.Service], route.Hash())
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("unknown action: %s", action)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// watchRoutes watches service routes and updates proxy cache
|
|
|
|
func (p *Proxy) watchRoutes() {
|
|
|
|
// this is safe to do as the only way watchRoutes returns is
|
|
|
|
// when some error is written into error channel - we want to bail then
|
|
|
|
defer close(p.errChan)
|
|
|
|
|
|
|
|
// route watcher
|
|
|
|
w, err := p.Router.Watch()
|
|
|
|
if err != nil {
|
|
|
|
p.errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
event, err := w.Next()
|
|
|
|
if err != nil {
|
|
|
|
p.errChan <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Lock()
|
|
|
|
if err := p.manageRouteCache(event.Route, fmt.Sprintf("%s", event.Type)); err != nil {
|
|
|
|
// TODO: should we bail here?
|
2019-07-24 20:13:05 +02:00
|
|
|
p.Unlock()
|
2019-07-24 20:03:13 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
p.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
// ServeRequest honours the server.Router interface
|
2019-06-07 14:42:39 +02:00
|
|
|
func (p *Proxy) ServeRequest(ctx context.Context, req server.Request, rsp server.Response) error {
|
2019-06-03 19:44:43 +02:00
|
|
|
// service name
|
|
|
|
service := req.Service()
|
|
|
|
endpoint := req.Endpoint()
|
2019-06-27 13:56:52 +02:00
|
|
|
var addresses []string
|
2019-06-03 19:44:43 +02:00
|
|
|
|
2019-06-27 13:56:52 +02:00
|
|
|
// call a specific backend endpoint either by name or address
|
2019-06-07 14:42:39 +02:00
|
|
|
if len(p.Endpoint) > 0 {
|
2019-06-03 19:44:43 +02:00
|
|
|
// address:port
|
2019-06-07 14:42:39 +02:00
|
|
|
if parts := strings.Split(p.Endpoint, ":"); len(parts) > 1 {
|
2019-06-27 13:56:52 +02:00
|
|
|
addresses = []string{p.Endpoint}
|
2019-06-03 19:44:43 +02:00
|
|
|
} else {
|
2019-06-27 13:56:52 +02:00
|
|
|
// get route for endpoint from router
|
|
|
|
addr, err := p.getRoute(p.Endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// set the address
|
|
|
|
addresses = addr
|
|
|
|
// set the name
|
2019-06-07 14:42:39 +02:00
|
|
|
service = p.Endpoint
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
2019-06-27 13:56:52 +02:00
|
|
|
} else {
|
|
|
|
// no endpoint was specified just lookup the route
|
|
|
|
// get route for endpoint from router
|
|
|
|
addr, err := p.getRoute(service)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
addresses = addr
|
|
|
|
}
|
|
|
|
|
|
|
|
var opts []client.CallOption
|
|
|
|
|
|
|
|
// set address if available
|
|
|
|
if len(addresses) > 0 {
|
|
|
|
opts = append(opts, client.WithAddress(addresses...))
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// read initial request
|
|
|
|
body, err := req.Read()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// create new request with raw bytes body
|
|
|
|
creq := p.Client.NewRequest(service, endpoint, &bytes.Frame{body}, client.WithContentType(req.ContentType()))
|
|
|
|
|
2019-08-16 18:24:17 +02:00
|
|
|
// not a stream so make a client.Call request
|
2019-08-16 17:46:29 +02:00
|
|
|
if !req.Stream() {
|
2019-08-16 18:24:17 +02:00
|
|
|
crsp := new(bytes.Frame)
|
|
|
|
|
|
|
|
// make a call to the backend
|
|
|
|
if err := p.Client.Call(ctx, creq, crsp, opts...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// write the response
|
|
|
|
if err := rsp.Write(crsp.Data); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2019-08-16 17:46:29 +02:00
|
|
|
}
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
// create new stream
|
|
|
|
stream, err := p.Client.Stream(ctx, creq, opts...)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer stream.Close()
|
|
|
|
|
2019-08-16 17:46:29 +02:00
|
|
|
// create client request read loop if streaming
|
2019-08-16 18:24:17 +02:00
|
|
|
go readLoop(req, stream)
|
2019-06-03 19:44:43 +02:00
|
|
|
|
|
|
|
// get raw response
|
|
|
|
resp := stream.Response()
|
|
|
|
|
2019-07-24 20:03:13 +02:00
|
|
|
// route watcher error
|
|
|
|
var watchErr error
|
|
|
|
|
2019-06-03 19:44:43 +02:00
|
|
|
// create server response write loop
|
|
|
|
for {
|
2019-07-24 20:03:13 +02:00
|
|
|
select {
|
|
|
|
case err := <-p.errChan:
|
|
|
|
if err != nil {
|
|
|
|
watchErr = err
|
|
|
|
}
|
|
|
|
return watchErr
|
|
|
|
default:
|
|
|
|
// read backend response body
|
|
|
|
body, err := resp.Read()
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-03 19:44:43 +02:00
|
|
|
|
2019-07-24 20:03:13 +02:00
|
|
|
// read backend response header
|
|
|
|
hdr := resp.Header()
|
2019-06-03 19:44:43 +02:00
|
|
|
|
2019-07-24 20:03:13 +02:00
|
|
|
// write raw response header to client
|
|
|
|
rsp.WriteHeader(hdr)
|
2019-06-03 19:44:43 +02:00
|
|
|
|
2019-07-24 20:03:13 +02:00
|
|
|
// write raw response body to client
|
|
|
|
err = rsp.Write(body)
|
|
|
|
if err == io.EOF {
|
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-07 14:42:39 +02:00
|
|
|
// NewSingleHostProxy returns a proxy which sends requests to a single backend
|
|
|
|
func NewSingleHostProxy(endpoint string) *Proxy {
|
|
|
|
return &Proxy{
|
|
|
|
Options: options.NewOptions(),
|
|
|
|
Endpoint: endpoint,
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 14:42:39 +02:00
|
|
|
// NewProxy returns a new proxy which will route based on mucp headers
|
|
|
|
func NewProxy(opts ...options.Option) proxy.Proxy {
|
|
|
|
p := new(Proxy)
|
|
|
|
p.Options = options.NewOptions(opts...)
|
|
|
|
p.Options.Init(options.WithString("mucp"))
|
|
|
|
|
|
|
|
// get endpoint
|
|
|
|
ep, ok := p.Options.Values().Get("proxy.endpoint")
|
|
|
|
if ok {
|
|
|
|
p.Endpoint = ep.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get client
|
|
|
|
c, ok := p.Options.Values().Get("proxy.client")
|
|
|
|
if ok {
|
|
|
|
p.Client = c.(client.Client)
|
|
|
|
}
|
|
|
|
|
2019-07-24 19:13:51 +02:00
|
|
|
// set the default client
|
|
|
|
if p.Client == nil {
|
|
|
|
p.Client = client.DefaultClient
|
|
|
|
}
|
|
|
|
|
2019-06-26 17:12:57 +02:00
|
|
|
// get router
|
|
|
|
r, ok := p.Options.Values().Get("proxy.router")
|
|
|
|
if ok {
|
2019-06-27 13:56:52 +02:00
|
|
|
p.Router = r.(router.Router)
|
2019-06-26 17:12:57 +02:00
|
|
|
}
|
|
|
|
|
2019-07-24 19:13:51 +02:00
|
|
|
// create default router and start it
|
|
|
|
if p.Router == nil {
|
|
|
|
p.Router = router.DefaultRouter
|
|
|
|
}
|
|
|
|
|
|
|
|
// routes cache
|
2019-07-27 14:58:51 +02:00
|
|
|
p.Routes = make(map[string]map[uint64]router.Route)
|
2019-07-24 19:13:51 +02:00
|
|
|
|
2019-07-24 20:03:13 +02:00
|
|
|
// watch router service routes
|
|
|
|
p.errChan = make(chan error, 1)
|
|
|
|
go p.watchRoutes()
|
|
|
|
|
2019-06-07 14:42:39 +02:00
|
|
|
return p
|
2019-06-03 19:44:43 +02:00
|
|
|
}
|