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


Golang blobserver.BlobEnumerator类代码示例

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


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

示例1: handleEnumerateBlobs

func handleEnumerateBlobs(conn http.ResponseWriter, req *http.Request, storage blobserver.BlobEnumerator) {
	if w, ok := storage.(blobserver.ContextWrapper); ok {
		storage = w.WrapContext(req)
	}

	// Potential input parameters
	formValueLimit := req.FormValue("limit")
	formValueMaxWaitSec := req.FormValue("maxwaitsec")
	formValueAfter := req.FormValue("after")

	maxEnumerate := defaultMaxEnumerate
	if config, ok := blobserver.Unwrap(storage).(blobserver.MaxEnumerateConfig); ok {
		maxEnumerate = config.MaxEnumerate() - 1 // Since we'll add one below.
	}

	limit := defaultEnumerateSize
	if formValueLimit != "" {
		n, err := strconv.ParseUint(formValueLimit, 10, 32)
		if err != nil || n > uint64(maxEnumerate) {
			limit = maxEnumerate
		} else {
			limit = int(n)
		}
	}

	waitSeconds := 0
	if formValueMaxWaitSec != "" {
		waitSeconds, _ = strconv.Atoi(formValueMaxWaitSec)
		if waitSeconds != 0 && formValueAfter != "" {
			conn.WriteHeader(http.StatusBadRequest)
			fmt.Fprintf(conn, errMsgMaxWaitSecWithAfter)
			return
		}
		switch {
		case waitSeconds < 0:
			waitSeconds = 0
		case waitSeconds > 30:
			// TODO: don't hard-code 30.  push this up into a blobserver interface
			// for getting the configuration of the server (ultimately a flag in
			// in the binary)
			waitSeconds = 30
		}
	}

	conn.Header().Set("Content-Type", "text/javascript; charset=utf-8")
	fmt.Fprintf(conn, "{\n  \"blobs\": [\n")

	blobch := make(chan blobref.SizedBlobRef, 100)
	resultch := make(chan error, 1)
	go func() {
		resultch <- storage.EnumerateBlobs(blobch, formValueAfter, limit+1, time.Duration(waitSeconds)*time.Second)
	}()

	after := ""
	needsComma := false

	endsReached := 0
	gotBlobs := 0
	for endsReached < 2 {
		select {
		case sb, ok := <-blobch:
			if !ok {
				endsReached++
				if gotBlobs <= limit {
					after = ""
				}
				continue
			}
			gotBlobs++
			if gotBlobs > limit {
				// We requested one more from storage than the user asked for.
				// Now we know to return a "continueAfter" response key.
				// But we don't return this blob.
				continue
			}
			blobName := sb.BlobRef.String()
			if needsComma {
				fmt.Fprintf(conn, ",\n")
			}
			fmt.Fprintf(conn, "    {\"blobRef\": \"%s\", \"size\": %d}",
				blobName, sb.Size)
			after = blobName
			needsComma = true
		case err := <-resultch:
			if err != nil {
				log.Printf("Error during enumerate: %v", err)
				fmt.Fprintf(conn, "{{{ SERVER ERROR }}}")
				return
			}
			endsReached++
		}
	}
	fmt.Fprintf(conn, "\n  ]")
	if after != "" {
		fmt.Fprintf(conn, ",\n  \"continueAfter\": \"%s\"", after)
	}
	const longPollSupported = true
	if longPollSupported {
		fmt.Fprintf(conn, ",\n  \"canLongPoll\": true")
	}
//.........这里部分代码省略.........
开发者ID:hagna,项目名称:camlistore,代码行数:101,代码来源:enumerate.go

示例2: handleEnumerateBlobs

func handleEnumerateBlobs(rw http.ResponseWriter, req *http.Request, storage blobserver.BlobEnumerator) {
	// Potential input parameters
	formValueLimit := req.FormValue("limit")
	formValueMaxWaitSec := req.FormValue("maxwaitsec")
	formValueAfter := req.FormValue("after")

	maxEnumerate := defaultMaxEnumerate
	if config, ok := storage.(blobserver.MaxEnumerateConfig); ok {
		maxEnumerate = config.MaxEnumerate()
	}

	limit := defaultEnumerateSize
	if formValueLimit != "" {
		n, err := strconv.ParseUint(formValueLimit, 10, 32)
		if err != nil || n > uint64(maxEnumerate) {
			limit = maxEnumerate
		} else {
			limit = int(n)
		}
	}

	waitSeconds := 0
	if formValueMaxWaitSec != "" {
		waitSeconds, _ = strconv.Atoi(formValueMaxWaitSec)
		if waitSeconds != 0 && formValueAfter != "" {
			rw.WriteHeader(http.StatusBadRequest)
			fmt.Fprintf(rw, errMsgMaxWaitSecWithAfter)
			return
		}
		switch {
		case waitSeconds < 0:
			waitSeconds = 0
		case waitSeconds > 30:
			// TODO: don't hard-code 30.  push this up into a blobserver interface
			// for getting the configuration of the server (ultimately a flag in
			// in the binary)
			waitSeconds = 30
		}
	}

	rw.Header().Set("Content-Type", "text/javascript; charset=utf-8")
	io.WriteString(rw, "{\n  \"blobs\": [\n")

	loop := true
	needsComma := false
	deadline := time.Now().Add(time.Duration(waitSeconds) * time.Second)
	after := ""
	for loop && (waitSeconds == 0 || time.Now().After(deadline)) {
		if waitSeconds == 0 {
			loop = false
		}

		blobch := make(chan blob.SizedRef, 100)
		resultch := make(chan error, 1)
		go func() {
			resultch <- storage.EnumerateBlobs(context.TODO(), blobch, formValueAfter, limit)
		}()

		gotBlobs := 0
		for sb := range blobch {
			gotBlobs++
			loop = false
			blobName := sb.Ref.String()
			if needsComma {
				io.WriteString(rw, ",\n")
			}
			fmt.Fprintf(rw, "    {\"blobRef\": \"%s\", \"size\": %d}",
				blobName, sb.Size)
			after = blobName
			needsComma = true
		}
		if gotBlobs < limit {
			after = ""
		}
		if err := <-resultch; err != nil {
			log.Printf("Error during enumerate: %v", err)
			fmt.Fprintf(rw, "{{{ SERVER ERROR }}}")
			return
		}

		if loop {
			blobserver.WaitForBlob(storage, deadline, nil)
		}
	}
	io.WriteString(rw, "\n  ]")
	if after != "" {
		fmt.Fprintf(rw, ",\n  \"continueAfter\": \"%s\"", after)
	}
	const longPollSupported = true
	if longPollSupported {
		io.WriteString(rw, ",\n  \"canLongPoll\": true")
	}
	io.WriteString(rw, "\n}\n")
}
开发者ID:rfistman,项目名称:camlistore,代码行数:94,代码来源:enumerate.go


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