Example #1
0
/*
Create a proxy that listens on 0.0.0.0:27017 and foward to hosts
registered under service key /srv/mongo_router/debug, trying them in
order.

New set of hosts is obtained on nodes joining or leaving service
/srv/mongo_router/debug, followed by connection reset.

See coreos/etcd https://github.com/coreos/etcd for more information on
discovery backend
*/
func ExampleSrv_discovery() {
	pxyOpts := &proxy.ConnOptions{
		Net:  "tcp4",
		From: ":27017",
		Discovery: &proxy.DiscOptions{
			Service:   "/srv/mongo_router/debug",
			Endpoints: []string{"http://10.0.1.11:2379", "http://10.0.2.13:2379"},
		},
	}

	context, cancel := ctx.WithCancel(ctx.Background())
	defer cancel()

	err := proxy.Srv(context, pxyOpts)
	log.Warning(err)
}
Example #2
0
func listen(wk ctx.Context, opts *proxy.ConnOptions) (halt <-chan struct{}) {
	var err error

	ending := make(chan struct{}, 1)
	go func() {
		defer close(ending)
		logger := log.WithFields(log.Fields{
			"Net":     opts.Net,
			"To":      opts.To,
			"Balance": opts.Balance,
		})
		if len(opts.FromRange) > 0 {
			logger = logger.WithFields(log.Fields{
				"FromRange": opts.FromRange,
			})
		} else {
			logger = logger.WithFields(log.Fields{
				"From": opts.From,
			})
		}
		if opts.Discovery != nil {
			logger = logger.WithFields(log.Fields{
				"Endpoints": opts.Discovery.Endpoints,
				"Service":   opts.Discovery.Service,
			})
		}
		logger.Info("begin")
		if opts.Discovery != nil {
			if len(opts.FromRange) == 0 {
				err = proxy.Srv(wk, opts)
			} else {
				err = proxy.ClusterSrv(wk, opts)
			}
		} else if len(opts.To) != 0 {
			if len(opts.FromRange) == 0 {
				err = proxy.To(wk, opts)
			} else {
				err = proxy.ClusterTo(wk, opts)
			}
		} else {
			err = errors.New("Misconfigured connect options")
		}
		logger.WithFields(log.Fields{"err": err}).Warning("end")

	}()
	return ending
}