当前位置: 首页>>代码示例>>Golang>>正文


Golang RequestCtx.SetConnectionClose方法代码示例

本文整理汇总了Golang中github.com/valyala/fasthttp.RequestCtx.SetConnectionClose方法的典型用法代码示例。如果您正苦于以下问题:Golang RequestCtx.SetConnectionClose方法的具体用法?Golang RequestCtx.SetConnectionClose怎么用?Golang RequestCtx.SetConnectionClose使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/valyala/fasthttp.RequestCtx的用法示例。


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

示例1: mainHandler

func mainHandler(ctx *fasthttp.RequestCtx) {
	// Performance hack for prefork mode - periodically close keepalive
	// connections for evenly distributing connections among available
	// processes.
	if *prefork {
		maxDuration := maxConnDuration + time.Millisecond*time.Duration(atomic.LoadUint64(&connDurationJitter))
		if time.Since(ctx.ConnTime()) > maxDuration {
			atomic.StoreUint64(&connDurationJitter, uint64(rand.Intn(100)))
			ctx.SetConnectionClose()
		}
	}

	path := ctx.Path()
	switch string(path) {
	case "/plaintext":
		plaintextHandler(ctx)
	case "/json":
		jsonHandler(ctx)
	case "/db":
		dbHandler(ctx)
	case "/queries":
		queriesHandler(ctx)
	case "/fortune":
		fortuneHandler(ctx)
	case "/update":
		updateHandler(ctx)
	default:
		ctx.Error("unexpected path", fasthttp.StatusBadRequest)
	}
}
开发者ID:tussion,项目名称:FrameworkBenchmarks,代码行数:30,代码来源:hello.go

示例2: pubRawHandler

// /raw/msgs/:topic/:ver
func (this *Gateway) pubRawHandler(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
	var (
		topic  string
		ver    string
		appid  string
		pubkey string
	)

	ver = params.ByName(UrlParamVersion)
	topic = params.ByName(UrlParamTopic)
	header := ctx.Request.Header
	appid = string(header.Peek(HttpHeaderAppid))
	pubkey = string(header.Peek(HttpHeaderPubkey))

	if err := manager.Default.OwnTopic(appid, pubkey, topic); err != nil {
		log.Error("app[%s] %s %+v: %v", appid, ctx.RemoteAddr(), params, err)

		ctx.SetConnectionClose()
		ctx.Error("invalid secret", fasthttp.StatusUnauthorized)
		return
	}

	cluster, found := manager.Default.LookupCluster(appid)
	if !found {
		log.Error("cluster not found for app: %s", appid)

		ctx.Error("invalid appid", fasthttp.StatusBadRequest)
		return
	}

	var out = map[string]string{
		"store":       "kafka",
		"broker.list": strings.Join(meta.Default.BrokerList(cluster), ","),
		"topic":       manager.Default.KafkaTopic(appid, topic, ver),
	}

	b, _ := json.Marshal(out)
	ctx.SetContentType("application/json; charset=utf8")
	ctx.Write(b)
}
开发者ID:funkygao,项目名称:gafka,代码行数:41,代码来源:handler_fastpub.go

示例3: pubHandler

// /topics/:topic/:ver?key=mykey&async=1&delay=100
func (this *Gateway) pubHandler(ctx *fasthttp.RequestCtx, params fasthttprouter.Params) {
	t1 := time.Now()

	topic := params.ByName(UrlParamTopic)
	header := ctx.Request.Header
	appid := string(header.Peek(HttpHeaderAppid))
	pubkey := string(header.Peek(HttpHeaderPubkey))
	if err := manager.Default.OwnTopic(appid, pubkey, topic); err != nil {
		log.Error("app[%s] %s %+v: %v", appid, ctx.RemoteAddr(), params, err)

		ctx.SetConnectionClose()
		ctx.Error("invalid secret", fasthttp.StatusUnauthorized)
		return
	}

	msgLen := ctx.Request.Header.ContentLength()
	switch {
	case msgLen == -1:
		log.Warn("pub[%s] %s %+v invalid content length", appid, ctx.RemoteAddr(), params)
		ctx.Error("invalid content length", fasthttp.StatusBadRequest)
		return

	case int64(msgLen) > options.MaxPubSize:
		log.Warn("pub[%s] %s %+v too big content length:%d", appid, ctx.RemoteAddr(), params, msgLen)
		ctx.Error(ErrTooBigPubMessage.Error(), fasthttp.StatusBadRequest)
		return

	case msgLen < options.MinPubSize:
		log.Warn("pub[%s] %s %+v too small content length:%d", appid, ctx.RemoteAddr(), params, msgLen)
		ctx.Error(ErrTooSmallPubMessage.Error(), fasthttp.StatusBadRequest)
		return
	}

	ver := params.ByName(UrlParamVersion)
	queryArgs := ctx.Request.URI().QueryArgs()
	key := queryArgs.Peek("key")
	asyncArg := queryArgs.Peek("async")
	async := len(asyncArg) == 1 && asyncArg[0] == '1'
	//delay := hack.String(queryArgs.Peek("delay"))

	if options.Debug {
		log.Debug("pub[%s] %s {topic:%s, ver:%s, key:%s, async:%+v} %s",
			appid, ctx.RemoteAddr(),
			topic, ver, key, async,
			string(ctx.Request.Body()))
	}

	if !options.DisableMetrics {
		this.pubMetrics.PubQps.Mark(1)
		this.pubMetrics.PubMsgSize.Update(int64(len(ctx.PostBody())))
	}

	pubMethod := store.DefaultPubStore.SyncPub
	if async {
		pubMethod = store.DefaultPubStore.AsyncPub
	}

	cluster, found := manager.Default.LookupCluster(appid)
	if !found {
		log.Error("cluster not found for app: %s", appid)

		ctx.Error("invalid appid", fasthttp.StatusBadRequest)
		return
	}

	err := pubMethod(cluster,
		manager.Default.KafkaTopic(appid, topic, ver),
		key, ctx.PostBody())
	if err != nil {
		if !options.DisableMetrics {
			this.pubMetrics.PubFail(appid, topic, ver)
		}

		log.Error("%s: %v", ctx.RemoteAddr(), err)

		ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
		return
	}

	// write the reponse
	ctx.Write(ResponseOk)
	if !options.DisableMetrics {
		this.pubMetrics.PubOk(appid, topic, ver)
		this.pubMetrics.PubLatency.Update(time.Since(t1).Nanoseconds() / 1e6) // in ms
	}
}
开发者ID:funkygao,项目名称:gafka,代码行数:87,代码来源:handler_fastpub.go

示例4: handshakeHandler

func handshakeHandler(ctx *fasthttp.RequestCtx) {
	streamingHandler(ctx)

	// Explicitly close connection after each response.
	ctx.SetConnectionClose()
}
开发者ID:stellar,项目名称:bridge-server,代码行数:6,代码来源:inmemory_listener_timing_test.go


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