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


Golang UUID.Array方法代码示例

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


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

示例1: tagHasPermission

func tagHasPermission(tag string, uuidBytes uuid.UUID, uuidString string) bool {
	var hasPerm bool

	uuidarr := uuidBytes.Array()
	permcacheLock.Lock()
	taginfo, ok := permcache[tag]
	if !ok {
		/* Cache Miss: never seen this token */
		taginfo = &TagInfo{name: tag, permissions: make(map[uuid.Array]bool)}
		lruListLock.Lock()
		taginfo.element = lruList.PushFront(taginfo)
		lruListLock.Unlock()
		permcache[tag] = taginfo
	} else {
		lruListLock.Lock()
		lruList.MoveToFront(taginfo.element) // most recently used
		lruListLock.Unlock()
		hasPerm, ok = taginfo.permissions[uuidBytes.Array()]
		if ok {
			/* Cache Hit */
			permcacheLock.Unlock()
			return hasPerm
		}
		/* Cache Miss: never seen this UUID */
	}
	permcacheLock.Unlock()

	/* Ask the metadata server for the metadata of the corresponding stream. */
	query := fmt.Sprintf("select * where uuid = \"%s\";", uuidString)
	mdReq, err := http.NewRequest("POST", fmt.Sprintf("%s?tags=%s", mdServer, tag), strings.NewReader(query))
	if err != nil {
		return false
	}

	mdReq.Header.Set("Content-Type", "text")
	mdReq.Header.Set("Content-Length", fmt.Sprintf("%v", len(query)))
	resp, err := http.DefaultClient.Do(mdReq)

	if err != nil {
		return false
	}

	/* If the response is [] we lack permission; if it's longer we have permission. */
	buf := make([]byte, 3)
	n, err := io.ReadFull(resp.Body, buf)
	resp.Body.Close()

	if n == 3 && buf[0] == '[' {
		hasPerm = true
	} else if n == 2 && err == io.ErrUnexpectedEOF && buf[0] == '[' && buf[1] == ']' {
		hasPerm = false
	} else {
		/* Server error. */
		fmt.Printf("Metadata server error: %v %c %c %c\n", n, buf[0], buf[1], buf[2])
		return false
	}

	/* If we didn't return early due to some kind of error, cache the result and return it. */
	permcacheLock.Lock()
	if taginfo.element != nil { // If this has been evicted from the cache, don't bother
		_, ok := taginfo.permissions[uuidarr]
		taginfo.permissions[uuidarr] = hasPerm // still update cached value
		if !ok {                               // If a different goroutine added it before we got here, then skip this part
			totalCached += 1
			if totalCached > MAX_CACHED {
				// Make this access return quickly, so start pruning in a new goroutine
				fmt.Println("Pruning cache")
				go pruneCache()
			}
		}
	}
	permcacheLock.Unlock()

	return hasPerm
}
开发者ID:SoftwareDefinedBuildings,项目名称:mr-plotter,代码行数:75,代码来源:permcache.go


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