示例#1
0
文件: upstream.go 项目: na--/nedomi
// NewSimple creates a simple RoundTripper with the default configuration that
// proxies requests to the supplied URL
func NewSimple(url *url.URL) (*Upstream, error) {
	host, port, err := httputils.ParseURLHost(url)
	if err != nil {
		return nil, fmt.Errorf("Invalid upstream address %s: %s", url, err)
	}

	up := &types.UpstreamAddress{
		URL:         *url,
		Hostname:    host,
		Port:        port,
		OriginalURL: url,
		Weight:      1,
	}

	return &Upstream{
		upTransport: getTransport(config.GetDefaultUpstreamSettings()),
		addressGetter: func(_ string) (*types.UpstreamAddress, error) {
			// Always return the same single url - no balancing needed
			return up, nil
		},
	}, nil
}
示例#2
0
文件: upstream.go 项目: na--/nedomi
// New creates a new RoundTripper from the supplied upstream config
func New(conf *config.Upstream, logger types.Logger) (*Upstream, error) {

	balancingAlgo, err := balancing.New(conf.Balancing)
	if err != nil {
		return nil, err
	}

	up := &Upstream{
		upTransport:   getTransport(conf.Settings),
		config:        conf,
		logger:        logger,
		addressGetter: balancingAlgo.Get,
	}

	// Feed the unresolved addresses while waiting for DNS resolver
	unresolved := make([]*types.UpstreamAddress, len(conf.Addresses))
	for i, addr := range conf.Addresses {
		host, port, err := httputils.ParseURLHost(addr.URL)
		if err != nil {
			return nil, fmt.Errorf("Invalid upstream address %s: %s", addr.URL, err)
		}

		unresolved[i] = &types.UpstreamAddress{
			URL:         *addr.URL,
			Hostname:    host,
			Port:        port,
			OriginalURL: addr.URL,
			Weight:      addr.Weight,
		}
	}
	balancingAlgo.Set(unresolved)

	if conf.Settings.ResolveAddresses {
		//!TODO: get app cancel channel to the dns resolver
		go up.initDNSResolver(balancingAlgo, unresolved)
	}

	return up, nil
}