當前位置: 首頁>>代碼示例>>Golang>>正文


Golang GetArgs.HostPort方法代碼示例

本文整理匯總了Golang中github.com/cmu440/tribbler/rpc/storagerpc.GetArgs.HostPort方法的典型用法代碼示例。如果您正苦於以下問題:Golang GetArgs.HostPort方法的具體用法?Golang GetArgs.HostPort怎麽用?Golang GetArgs.HostPort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/cmu440/tribbler/rpc/storagerpc.GetArgs的用法示例。


在下文中一共展示了GetArgs.HostPort方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: GetList

func (ls *libstore) GetList(key string) ([]string, error) {
	v := ls.getFromCache(key)
	if v != nil {
		return v.([]string), nil
	}

	// make args and reply
	var reply storagerpc.GetListReply
	args := storagerpc.GetArgs{
		Key: key,
	}

	if ls.needLease(key) {
		args.WantLease = true
		args.HostPort = ls.myHostPort
	}

	// do rpc
	rpcHandler := ls.getStorageRPCHandler(key)
	err := rpcHandler.Call(rpcNames[getListCall], &args, &reply)
	if err != nil {
		return nil, err
	}

	switch reply.Status {
	case storagerpc.OK:
	case storagerpc.KeyNotFound:
		return nil, ErrorKeyNotFound
	case storagerpc.ItemNotFound:
		return nil, ErrorItemNotFound
	case storagerpc.WrongServer:
		return nil, ErrorWrongServer
	case storagerpc.ItemExists:
		return nil, ErrorItemExisits
	case storagerpc.NotReady:
		return nil, ErrorNotReady
	default:
		panic("")
	}

	// add to cache
	if reply.Lease.Granted {
		ls.cache.Lock()
		ls.cache.c[key] = &cachedItem{
			value:          reply.Value,
			expirationTime: time.Now().Add(time.Second * time.Duration(reply.Lease.ValidSeconds)),
		}
		ls.cache.Unlock()
	}

	return reply.Value, nil
}
開發者ID:oldady,項目名稱:ds_p2,代碼行數:52,代碼來源:libstore_impl.go

示例2: GetList

func (ls *libstore) GetList(key string) ([]string, error) {
	var args storagerpc.GetArgs
	var reply storagerpc.GetListReply

	args.Key = key
	args.WantLease = false
	args.HostPort = ls.hostport

	// FIRST CHECK FROM LOCAL CACHE!!!
	val, err := ls.leases.Get(key, &args)
	if err == nil {
		return val.([]string), nil
	}

	// CHECK IF WE NEED LEASE
	if ls.mode == Always {
		args.WantLease = true
	} else if ls.mode == Never {
		args.WantLease = false
	}

	// pick the right server
	cli, err := ls.GetServer(key)
	if err != nil {
		return nil, err
	}

	// call rpc to storage server
	err = cli.Call("StorageServer.GetList", &args, &reply)
	if err != nil {
		return nil, err
	}

	// check reply status
	if reply.Status != storagerpc.OK {
		err = GetErrorType(reply.Status)
		return nil, err
	}

	// check if allow lease
	if reply.Lease.Granted {
		ls.leases.Insert(key, reply.Value, reply.Lease)
	}

	return reply.Value, nil
}
開發者ID:oldady,項目名稱:Tribbler,代碼行數:46,代碼來源:libstore_impl.go

示例3: Get

func (ls *libstore) Get(key string) (string, error) {
	var args storagerpc.GetArgs
	var reply storagerpc.GetReply

	//set wantlease to be false
	args.Key = key
	args.WantLease = false
	args.HostPort = ls.hostport

	// check if it is in cache
	val, err := ls.leases.Get(key, &args)
	if err == nil {
		return val.(string), nil
	}

	//CHECK IF WE NEED LEASE
	if ls.mode == Always {
		args.WantLease = true
	} else if ls.mode == Never {
		args.WantLease = false
	}

	// pick the right server
	cli, err := ls.GetServer(key)
	if err != nil {
		return "", err
	}

	// call rpc
	err = cli.Call("StorageServer.Get", &args, &reply)
	if err != nil {
		return "", err
	}

	if reply.Status != storagerpc.OK {
		err = GetErrorType(reply.Status)
		return "", err
	}

	// insert into cache
	if reply.Lease.Granted {
		ls.leases.Insert(key, reply.Value, reply.Lease)
	}

	return reply.Value, nil
}
開發者ID:oldady,項目名稱:Tribbler,代碼行數:46,代碼來源:libstore_impl.go

示例4: GetList

func (ls *libstore) GetList(key string) ([]string, error) {
	var args storagerpc.GetArgs
	var reply storagerpc.GetListReply

	args.Key = key
	args.WantLease = false
	args.HostPort = ls.hostport

	val, err := ls.localCache.Get(&args)
	if err == nil {
		return val.([]string), nil
	}

	if ls.mode == Always {
		args.WantLease = true
	} else if ls.mode == Never {
		args.WantLease = false
	}

	cli, err := ls.GetServer(key)
	if err != nil {
		return nil, err
	}

	err = cli.Call("StorageServer.GetList", &args, &reply)

	if err != nil {
		return nil, err
	}

	if reply.Status != storagerpc.OK {
		err = GetErrorType(reply.Status)
		return nil, err
	}

	if reply.Lease.Granted {
		ls.localCache.Put(key, reply.Value, reply.Lease)
	}
	return reply.Value, nil
}
開發者ID:201101050424,項目名稱:Tribbler,代碼行數:40,代碼來源:libstore_impl.go

示例5: GetList

func (ls *libstore) GetList(key string) ([]string, error) {
	var args storagerpc.GetArgs
	var reply storagerpc.GetListReply

	current_time := time.Now().Unix()

	// Check whether the key is in the cache
	// If yes, return directly
	ls.list_cache_locker.Lock()
	if list_cache_elem, ok := ls.list_cache[key]; ok {
		if list_cache_elem.lease_end >= current_time {
			ls.list_cache_locker.Unlock()
			return list_cache_elem.content, nil
		} else {
			delete(ls.list_cache, key)
		}
	}
	ls.list_cache_locker.Unlock()

	args.Key = key
	args.HostPort = ls.host_port

	if ls.lease_mode == Normal {
		ls.list_cache_locker.Lock()
		if query_list, ok := ls.query_record[key]; ok {
			query_list.PushBack(current_time)
		} else {
			new_list := list.New()
			new_list.PushBack(current_time)
			ls.query_record[key] = new_list
		}
		if query_list, ok := ls.query_record[key]; ok {
			for elem := query_list.Front(); elem != nil; elem = elem.Next() {
				if elem.Value.(int64)+storagerpc.QueryCacheSeconds < current_time {
					query_list.Remove(elem)
				} else {
					break
				}
			}
			if query_list.Len() >= storagerpc.QueryCacheThresh {
				args.WantLease = true
			} else {
				args.WantLease = false
			}
		}
		ls.list_cache_locker.Unlock()
	} else if ls.lease_mode == Always {
		args.WantLease = true
	}

	target_server := ls.GetServerIndex(key)
	ls.rpc_connection[target_server].Call("StorageServer.GetList", args, &reply)
	if reply.Status != storagerpc.OK {
		return nil, errors.New("Error in Getlist")
	}
	if reply.Lease.Granted {
		ls.list_cache_locker.Lock()
		defer ls.list_cache_locker.Unlock()
		ls.list_cache[key] = &ListCacheElement{lease_end: time.Now().Unix() + int64(reply.Lease.ValidSeconds), content: reply.Value}
	}
	return reply.Value, nil
}
開發者ID:mallocanswer,項目名稱:Tribbler,代碼行數:62,代碼來源:libstore_impl.go


注:本文中的github.com/cmu440/tribbler/rpc/storagerpc.GetArgs.HostPort方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。