• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang request.Request类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/mailgun/vulcan/request.Request的典型用法代码示例。如果您正苦于以下问题:Golang Request类的具体用法?Golang Request怎么用?Golang Request使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。



在下文中一共展示了Request类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。

示例1: RequestToClientIp

// RequestToClientIp is a TokenMapper that maps the request to the client IP.
func RequestToClientIp(req request.Request) (string, error) {
	vals := strings.SplitN(req.GetHttpRequest().RemoteAddr, ":", 2)
	if len(vals[0]) == 0 {
		return "", fmt.Errorf("Failed to parse client IP")
	}
	return vals[0], nil
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:8,代码来源:limiter.go


示例2: proxyToEndpoint

// Proxy the request to the given endpoint, execute observers and middlewares chains
func (l *HttpLocation) proxyToEndpoint(tr *http.Transport, o *Options, endpoint endpoint.Endpoint, req request.Request) (*http.Response, error) {

	a := &request.BaseAttempt{Endpoint: endpoint}

	l.observerChain.ObserveRequest(req)
	defer l.observerChain.ObserveResponse(req, a)
	defer req.AddAttempt(a)

	it := l.middlewareChain.GetIter()
	defer l.unwindIter(it, req, a)

	for v := it.Next(); v != nil; v = it.Next() {
		a.Response, a.Error = v.ProcessRequest(req)
		if a.Response != nil || a.Error != nil {
			// Move the iterator forward to count it again once we unwind the chain
			it.Next()
			log.Errorf("Midleware intercepted request with response=%s, error=%s", a.Response.Status, a.Error)
			return a.Response, a.Error
		}
	}

	// Forward the request and mirror the response
	start := o.TimeProvider.UtcNow()
	a.Response, a.Error = tr.RoundTrip(req.GetHttpRequest())
	a.Duration = o.TimeProvider.UtcNow().Sub(start)
	return a.Response, a.Error
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:28,代码来源:httploc.go


示例3: NextEndpoint

func (r *RoundRobin) NextEndpoint(req request.Request) (endpoint.Endpoint, error) {
	r.mutex.Lock()
	defer r.mutex.Unlock()

	e, err := r.nextEndpoint(req)
	if err != nil {
		return nil, err
	}
	lastAttempt := req.GetLastAttempt()
	// This is the first try, so just return the selected endpoint
	if lastAttempt == nil {
		return e, nil
	}
	// Try to prevent failover to the same endpoint that we've seen before,
	// that reduces the probability of the scenario when failover hits same endpoint
	// on the next attempt and fails, so users will see a failed request.
	var endpoint endpoint.Endpoint
	for _ = range r.endpoints {
		endpoint, err = r.nextEndpoint(req)
		if err != nil {
			return nil, err
		}
		if !hasAttempted(req, endpoint) {
			return endpoint, nil
		}
	}
	return endpoint, nil
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:28,代码来源:roundrobin.go


示例4: getMetrics

func getMetrics(r request.Request) *metrics.RoundTripMetrics {
	m, ok := r.GetUserData(cbreakerMetrics)
	if !ok {
		return nil
	}
	return m.(*metrics.RoundTripMetrics)
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:7,代码来源:predicates.go


示例5: ProcessRequest

func (tl *TokenLimiter) ProcessRequest(r request.Request) (*http.Response, error) {
	tl.mutex.Lock()
	defer tl.mutex.Unlock()

	token, amount, err := tl.mapper(r)
	if err != nil {
		return nil, err
	}

	bucketI, exists := tl.buckets.Get(token)
	if !exists {
		bucketI, err = NewTokenBucket(tl.rate, tl.options.Burst+1, tl.options.TimeProvider)
		if err != nil {
			return nil, err
		}
		// We set ttl as 10 times rate period. E.g. if rate is 100 requests/second per client ip
		// the counters for this ip will expire after 10 seconds of inactivity
		tl.buckets.Set(token, bucketI, int(tl.rate.Period/time.Second)*10+1)
	}
	bucket := bucketI.(*TokenBucket)
	delay, err := bucket.Consume(amount)
	if err != nil {
		return nil, err
	}
	if delay > 0 {
		return netutils.NewTextResponse(r.GetHttpRequest(), errors.StatusTooManyRequests, "Too many requests"), nil
	}
	return nil, nil
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:29,代码来源:tokenlimiter.go


示例6: ProcessRequest

func (tl *TokenLimiter) ProcessRequest(r request.Request) (*http.Response, error) {
	tl.mutex.Lock()
	defer tl.mutex.Unlock()

	token, amount, err := tl.mapper(r)
	if err != nil {
		return nil, err
	}

	effectiveRates := tl.effectiveRates(r)
	bucketSetI, exists := tl.bucketSets.Get(token)
	var bucketSet *tokenBucketSet

	if exists {
		bucketSet = bucketSetI.(*tokenBucketSet)
		bucketSet.update(effectiveRates)
	} else {
		bucketSet = newTokenBucketSet(effectiveRates, tl.clock)
		// We set ttl as 10 times rate period. E.g. if rate is 100 requests/second per client ip
		// the counters for this ip will expire after 10 seconds of inactivity
		tl.bucketSets.Set(token, bucketSet, int(bucketSet.maxPeriod/time.Second)*10+1)
	}

	delay, err := bucketSet.consume(amount)
	if err != nil {
		return nil, err
	}
	if delay > 0 {
		return netutils.NewTextResponse(r.GetHttpRequest(), errors.StatusTooManyRequests, "Too many requests"), nil
	}
	return nil, nil
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:32,代码来源:tokenlimiter.go


示例7: RoundTrip

// Round trips the request to one of the endpoints and returns the response.
func (l *HttpLocation) RoundTrip(req request.Request) (*http.Response, error) {
	// Get options and transport as one single read transaction.
	// Options and transport may change if someone calls SetOptions
	o, tr := l.GetOptionsAndTransport()
	originalRequest := req.GetHttpRequest()

	//  Check request size first, if that exceeds the limit, we don't bother reading the request.
	if l.isRequestOverLimit(req) {
		return nil, errors.FromStatus(http.StatusRequestEntityTooLarge)
	}

	// Read the body while keeping this location's limits in mind. This reader controls the maximum bytes
	// to read into memory and disk. This reader returns anerror if the total request size exceeds the
	// prefefined MaxSizeBytes. This can occur if we got chunked request, in this case ContentLength would be set to -1
	// and the reader would be unbounded bufio in the http.Server
	body, err := netutils.NewBodyBufferWithOptions(originalRequest.Body, netutils.BodyBufferOptions{
		MemBufferBytes: o.Limits.MaxMemBodyBytes,
		MaxSizeBytes:   o.Limits.MaxBodyBytes,
	})
	if err != nil {
		return nil, err
	}
	if body == nil {
		return nil, fmt.Errorf("Empty body")
	}

	// Set request body to buffered reader that can replay the read and execute Seek
	req.SetBody(body)
	// Note that we don't change the original request Body as it's handled by the http server
	defer body.Close()

	for {
		_, err := req.GetBody().Seek(0, 0)
		if err != nil {
			return nil, err
		}

		endpoint, err := l.loadBalancer.NextEndpoint(req)
		if err != nil {
			log.Errorf("Load Balancer failure: %s", err)
			return nil, err
		}

		// Adds headers, changes urls. Note that we rewrite request each time we proxy it to the
		// endpoint, so that each try gets a fresh start
		req.SetHttpRequest(l.copyRequest(originalRequest, req.GetBody(), endpoint))

		// In case if error is not nil, we allow load balancer to choose the next endpoint
		// e.g. to do request failover. Nil error means that we got proxied the request successfully.
		response, err := l.proxyToEndpoint(tr, &o, endpoint, req)
		if o.ShouldFailover(req) {
			continue
		} else {
			return response, err
		}
	}
	log.Errorf("All endpoints failed!")
	return nil, fmt.Errorf("All endpoints failed")
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:60,代码来源:httploc.go


示例8: hasAttempted

func hasAttempted(req request.Request, endpoint endpoint.Endpoint) bool {
	for _, a := range req.GetAttempts() {
		if a.GetEndpoint().GetId() == endpoint.GetId() {
			return true
		}
	}
	return false
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:8,代码来源:roundrobin.go


示例9: match

func (m *methodMatcher) match(req request.Request) location.Location {
	for _, c := range m.methods {
		if req.GetHttpRequest().Method == c {
			return m.matcher.match(req)
		}
	}
	return nil
}
开发者ID:tomzhang,项目名称:golang-devops-stuff,代码行数:8,代码来源:matcher.go


示例10: Route

func (e *ExpRouter) Route(req request.Request) (location.Location, error) {
	l, err := e.r.Route(req.GetHttpRequest())
	if err != nil {
		return nil, err
	}
	if l == nil {
		return nil, nil
	}
	return l.(location.Location), nil
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:10,代码来源:exproute.go


示例11: match

// Takes the request and returns the location if the request path matches any of it's paths
// returns nil if none of the requests matches
func (p *trie) match(r request.Request) location.Location {
	if p.root == nil {
		return nil
	}

	path := r.GetHttpRequest().URL.Path
	if len(path) == 0 {
		path = "/"
	}
	return p.root.match(-1, path, r)
}
开发者ID:WIZARD-CXY,项目名称:golang-devops-stuff,代码行数:13,代码来源:trie.go


示例12: checkCondition

func (c *CircuitBreaker) checkCondition(r request.Request) bool {
	if !c.timeToCheck() {
		return false
	}

	c.m.Lock()
	defer c.m.Unlock()

	// Other goroutine could have updated the lastCheck variable before we grabbed mutex
	if !c.tm.UtcNow().After(c.lastCheck) {
		return false
	}
	c.lastCheck = c.tm.UtcNow().Add(c.checkPeriod)
	// Each requests holds a context attached to it, we use it to attach the metrics to the request
	// so condition checker function can use it for analysis on the next line.
	r.SetUserData(cbreakerMetrics, c.metrics)
	return c.condition(r)
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:18,代码来源:cbreaker.go


示例13: ProcessRequest

func (rw *Rewriter) ProcessRequest(r request.Request) (*http.Response, error) {
	req := r.GetHttpRequest()

	if clientIP, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
		if rw.TrustForwardHeader {
			if prior, ok := req.Header[headers.XForwardedFor]; ok {
				clientIP = strings.Join(prior, ", ") + ", " + clientIP
			}
		}
		req.Header.Set(headers.XForwardedFor, clientIP)
	}

	if xfp := req.Header.Get(headers.XForwardedProto); xfp != "" && rw.TrustForwardHeader {
		req.Header.Set(headers.XForwardedProto, xfp)
	} else if req.TLS != nil {
		req.Header.Set(headers.XForwardedProto, "https")
	} else {
		req.Header.Set(headers.XForwardedProto, "http")
	}

	if req.Host != "" {
		req.Header.Set(headers.XForwardedHost, req.Host)
	}
	req.Header.Set(headers.XForwardedServer, rw.Hostname)

	// Remove hop-by-hop headers to the backend.  Especially important is "Connection" because we want a persistent
	// connection, regardless of what the client sent to us.
	netutils.RemoveHeaders(headers.HopHeaders, req.Header)

	// We need to set ContentLength based on known request size. The incoming request may have been
	// set without content length or using chunked TransferEncoding
	totalSize, err := r.GetBody().TotalSize()
	if err != nil {
		return nil, err
	}
	req.ContentLength = totalSize
	// Remove TransferEncoding that could have been previously set
	req.TransferEncoding = []string{}

	return nil, nil
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:41,代码来源:rewrite.go


示例14: ProcessRequest

func (cl *ConnectionLimiter) ProcessRequest(r request.Request) (*http.Response, error) {
	cl.mutex.Lock()
	defer cl.mutex.Unlock()

	token, amount, err := cl.mapper(r)
	if err != nil {
		return nil, err
	}

	connections := cl.connections[token]
	if connections >= cl.maxConnections {
		return netutils.NewTextResponse(
			r.GetHttpRequest(),
			errors.StatusTooManyRequests,
			fmt.Sprintf("Connection limit reached. Max is: %d, yours: %d", cl.maxConnections, connections)), nil
	}

	cl.connections[token] += amount
	cl.totalConnections += int64(amount)
	return nil, nil
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:21,代码来源:connlimiter.go


示例15: isRequestOverLimit

func (l *HttpLocation) isRequestOverLimit(req request.Request) bool {
	if l.options.Limits.MaxBodyBytes <= 0 {
		return false
	}
	return req.GetHttpRequest().ContentLength > l.options.Limits.MaxBodyBytes
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:6,代码来源:httploc.go


示例16: IsNetworkError

// Failover in case if last attempt resulted in error
func IsNetworkError(req request.Request) bool {
	attempts := len(req.GetAttempts())
	return attempts != 0 && req.GetAttempts()[attempts-1].GetError() != nil
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:5,代码来源:failover.go


示例17: RoundTrip

func (l *ConstHttpLocation) RoundTrip(r request.Request) (*http.Response, error) {
	req := r.GetHttpRequest()
	req.URL = netutils.MustParseUrl(l.Url)
	return http.DefaultTransport.RoundTrip(req)
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:5,代码来源:location.go


示例18: shouldRecordMetrics

func (c *CircuitBreaker) shouldRecordMetrics(r request.Request) bool {
	_, ok := r.GetUserData(cbreakerRecordMetrics)
	return ok
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:4,代码来源:cbreaker.go


示例19: markToRecordMetrics

func (c *CircuitBreaker) markToRecordMetrics(r request.Request) {
	r.SetUserData(cbreakerRecordMetrics, true)
}
开发者ID:GitHub9527,项目名称:vulcan,代码行数:3,代码来源:cbreaker.go


示例20: RequestToHost

// RequestToHost maps request to the host value
func RequestToHost(req request.Request) (string, error) {
	return req.GetHttpRequest().Host, nil
}
开发者ID:irasmaster,项目名称:vulcan,代码行数:4,代码来源:limiter.go



注:本文中的github.com/mailgun/vulcan/request.Request类示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang cli.NewApp函数代码示例发布时间:2022-05-23
下一篇:
Golang netutils.CopyHeaders函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap