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


Golang retry.New函数代码示例

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


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

示例1: bytesFromBucket

func (c AWSStorageService) bytesFromBucket(bucketName, objectKey string) ([]byte, error) {

	var resp *s3.GetObjectOutput

	work := func() error {
		svc := s3.New(c.Config)

		params := &s3.GetObjectInput{
			Bucket: aws.String(bucketName),
			Key:    aws.String(objectKey),
		}

		var err error
		if resp, err = svc.GetObject(params); err != nil {
			Log.Println(err.Error())
			return err
		}
		return nil
	}

	if err := retry.New(5*time.Second, 3, retry.DefaultBackoffFunc).Try(work); err != nil {
		return nil, err
	}

	return ioutil.ReadAll(resp.Body)
}
开发者ID:ae6rt,项目名称:decap,代码行数:26,代码来源:aws.go

示例2: DeleteBranchRestriction

// GetRepository returns a repository representation for the given Stash Project key and repository slug.
func (client Client) DeleteBranchRestriction(projectKey, repositorySlug string, id int) error {
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	work := func() error {
		req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/rest/branch-permissions/1.0/projects/%s/repos/%s/restricted/%d", client.baseURL.String(), projectKey, repositorySlug, id), nil)
		if err != nil {
			return err
		}
		log.Printf("stash.DeleteBranchRestriction %s\n", req.URL)
		req.Header.Set("Accept", "application/json")
		req.SetBasicAuth(client.userName, client.password)

		responseCode, _, err := consumeResponse(req)
		if err != nil {
			return err
		}

		if responseCode != http.StatusNoContent {
			var reason string = "unhandled reason"
			switch {
			case responseCode == http.StatusNotFound:
				reason = "Not found"
			case responseCode == http.StatusUnauthorized:
				reason = "Unauthorized"
			}
			return errorResponse{StatusCode: responseCode, Reason: reason}
		}

		return nil
	}

	return retry.Try(work)
}
开发者ID:xoom,项目名称:branchlock,代码行数:34,代码来源:stash.go

示例3: Get

// Get fetches existing ingresses from the api server
func (listener IngressListener) Get() (Ingresses, error) {
	req, err := http.NewRequest("GET", fmt.Sprintf("https://%s%s", listener.masterIP, listener.getPath), nil)
	if err != nil {
		return Ingresses{}, err
	}

	req.Header.Set("Accept", "application/json")
	req.SetBasicAuth(listener.user, listener.password)

	var data []byte
	work := func() error {
		response, err := listener.httpClient.Do(req)
		if err != nil {
			return err
		}

		if response.StatusCode != 200 {
			return fmt.Errorf("Non-200 response: %d\n", response.StatusCode)
		}

		data, err = ioutil.ReadAll(response.Body)
		return err
	}
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	var ingresses Ingresses
	if err := retry.Try(work); err != nil {
		return ingresses, err
	}

	err = json.Unmarshal(data, &ingresses)
	return ingresses, err
}
开发者ID:ae6rt,项目名称:ingressc,代码行数:34,代码来源:ingress.go

示例4: GetRawFile

func (client Client) GetRawFile(repositoryProjectKey, repositorySlug, filePath, branch string) ([]byte, error) {
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	var data []byte
	work := func() error {
		req, err := http.NewRequest("GET", fmt.Sprintf("%s/projects/%s/repos/%s/browse/%s?at=%s&raw", client.baseURL.String(), strings.ToLower(repositoryProjectKey), strings.ToLower(repositorySlug), filePath, branch), nil)
		if err != nil {
			return err
		}
		log.Printf("stash.GetRawFile %s\n", req.URL)
		req.SetBasicAuth(client.userName, client.password)

		var responseCode int
		responseCode, data, err = consumeResponse(req)
		if err != nil {
			return err
		}
		if responseCode != http.StatusOK {
			var reason string = "unhandled reason"
			switch {
			case responseCode == http.StatusNotFound:
				reason = "Not found"
			case responseCode == http.StatusUnauthorized:
				reason = "Unauthorized"
			}
			return errorResponse{StatusCode: responseCode, Reason: reason}
		}
		return nil
	}

	return data, retry.Try(work)
}
开发者ID:xoom,项目名称:branchlock,代码行数:32,代码来源:stash.go

示例5: DeleteBranch

func (client Client) DeleteBranch(projectKey, repositorySlug, branchName string) error {
	work := func() error {
		buffer := bytes.NewBufferString((fmt.Sprintf(`{"name":"refs/heads/%s","dryRun":false}`, branchName)))
		req, err := http.NewRequest("DELETE", fmt.Sprintf("%s/rest/branch-utils/1.0/projects/%s/repos/%s/branches", client.baseURL.String(), projectKey, repositorySlug), buffer)
		if err != nil {
			return err
		}
		req.Header.Set("Content-type", "application/json")
		req.SetBasicAuth(client.userName, client.password)

		responseCode, _, err := consumeResponse(req)
		if err != nil {
			return err
		}

		switch responseCode {
		case http.StatusNoContent:
			return nil
		case http.StatusBadRequest:
			return errorResponse{StatusCode: responseCode, Reason: "Bad Requeest"}
		case http.StatusUnauthorized:
			return errorResponse{StatusCode: responseCode, Reason: "Unauthorized"}
		default:
			return errorResponse{StatusCode: responseCode, Reason: "(unhandled reason)"}
		}
	}
	return retry.New(3*time.Second, 3, retry.DefaultBackoffFunc).Try(work)
}
开发者ID:anthonydahanne,项目名称:stash,代码行数:28,代码来源:stash.go

示例6: TestOKWithTimeout

func TestOKWithTimeout(t *testing.T) {
	r := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
	err := r.Try(func() error {
		return nil
	})
	if err != nil {
		t.Fatalf("Unexpected error: %v\n", err)
	}
}
开发者ID:ae6rt,项目名称:decap,代码行数:9,代码来源:retry_test.go

示例7: GetTags

// GetTags returns a map of tags indexed by tag display name for the given repository.
func (client Client) GetTags(projectKey, repositorySlug string) (map[string]Tag, error) {
	start := 0
	tags := make(map[string]Tag)
	morePages := true
	for morePages {
		var data []byte
		retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
		work := func() error {
			req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s/tags?start=%d&limit=%d", client.baseURL.String(), projectKey, repositorySlug, start, stashPageLimit), nil)
			if err != nil {
				return err
			}
			req.Header.Set("Accept", "application/json")

			// use credentials if we have them.  If not, the repository must be public.
			if client.userName != "" && client.password != "" {
				req.SetBasicAuth(client.userName, client.password)
			}

			var responseCode int
			responseCode, data, err = consumeResponse(req)
			if err != nil {
				return err
			}

			if responseCode != http.StatusOK {
				var reason string = "unhandled reason"
				switch {
				case responseCode == http.StatusNotFound:
					reason = "Not found"
				case responseCode == http.StatusUnauthorized:
					reason = "Unauthorized"
				}
				return errorResponse{StatusCode: responseCode, Reason: reason}
			}
			return nil
		}
		if err := retry.Try(work); err != nil {
			return nil, err
		}

		var r Tags
		if err := json.Unmarshal(data, &r); err != nil {
			return nil, err
		}
		for _, tag := range r.Tags {
			tags[tag.DisplayID] = tag
		}
		morePages = !r.IsLastPage
		start = r.NextPageStart
	}
	return tags, nil
}
开发者ID:anthonydahanne,项目名称:stash,代码行数:54,代码来源:stash.go

示例8: GetBranches

func (gh GithubClient) GetBranches(owner, repository string) ([]GithubBranch, error) {

	branches := make([]GithubBranch, 0)
	var data []byte
	url := fmt.Sprintf("%s/repos/%s/%s/git/refs?client_id=%s&client_secret=%s&page=1", gh.baseURL, owner, repository, gh.ClientID, gh.ClientSecret)
	var response *http.Response

	morePages := true
	for morePages {
		work := func() error {
			req, err := http.NewRequest("GET", url, nil)
			if err != nil {
				return err
			}
			req.Header.Set("Accept", "application/json")

			response, err = httpClient.Do(req)
			if err != nil {
				return err
			}
			defer func() {
				response.Body.Close()
			}()

			if response.StatusCode != http.StatusOK {
				return fmt.Errorf("git/refs non 200 status code (%d): %s", response.StatusCode, string(data))
			}

			data, err = ioutil.ReadAll(response.Body)
			if err != nil {
				return err
			}

			return nil
		}

		if err := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc).Try(work); err != nil {
			return nil, err
		}

		var b []GithubBranch
		if err := json.Unmarshal(data, &b); err != nil {
			return nil, err
		}

		branches = append(branches, b...)
		url = nextLink(response.Header.Get("Link"))
		morePages = url != ""
	}

	return branches, nil
}
开发者ID:ae6rt,项目名称:githubsdk,代码行数:52,代码来源:githubclient.go

示例9: GetRepositories

// GetRepositories returns a map of repositories indexed by repository URL.
func (client Client) GetRepositories() (map[int]Repository, error) {
	start := 0
	repositories := make(map[int]Repository)
	morePages := true
	for morePages {
		retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
		var data []byte
		work := func() error {
			req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/repos?start=%d&limit=%d", client.baseURL.String(), start, stashPageLimit), nil)
			if err != nil {
				return err
			}
			Log.Printf("stash.GetRepositories URL %s\n", req.URL)
			req.Header.Set("Accept", "application/json")
			// use credentials if we have them.  If not, the repository must be public.
			if client.userName != "" && client.password != "" {
				req.SetBasicAuth(client.userName, client.password)
			}

			var responseCode int
			responseCode, data, err = consumeResponse(req)
			if err != nil {
				return err
			}
			if responseCode != http.StatusOK {
				var reason string = "unhandled reason"
				switch {
				case responseCode == http.StatusBadRequest:
					reason = "Bad request."
				}
				return errorResponse{StatusCode: responseCode, Reason: reason}
			}
			return nil
		}
		if err := retry.Try(work); err != nil {
			return nil, err
		}

		var r Repositories
		err := json.Unmarshal(data, &r)
		if err != nil {
			return nil, err
		}
		for _, repo := range r.Repository {
			repositories[repo.ID] = repo
		}
		morePages = !r.IsLastPage
		start = r.NextPageStart
	}
	return repositories, nil
}
开发者ID:anthonydahanne,项目名称:stash,代码行数:52,代码来源:stash.go

示例10: TestTimeout

func TestTimeout(t *testing.T) {
	r := retry.New(500*time.Millisecond, 1, retry.DefaultBackoffFunc)
	err := r.Try(func() error {
		time.Sleep(1000 * time.Millisecond)
		return nil
	})
	if err == nil {
		t.Fatalf("Expected error\n")
	}
	fmt.Println(err)
	if !retry.IsTimeout(err) {
		t.Fatalf("Expected retry.timeoutError\n")
	}
}
开发者ID:ae6rt,项目名称:decap,代码行数:14,代码来源:retry_test.go

示例11: TestRetryExceeded

func TestRetryExceeded(t *testing.T) {
	r := retry.New(0*time.Second, 3, retry.DefaultBackoffFunc)
	tries := 0
	err := r.Try(func() error {
		tries += 1
		return errors.New("")
	})
	if err == nil {
		t.Fatalf("Expecting error\n")
	}
	if tries != 3 {
		t.Fatalf("Expecting 3 but got %d\n", tries)
	}
}
开发者ID:ae6rt,项目名称:decap,代码行数:14,代码来源:retry_test.go

示例12: GetBranches

// GetBranches returns a map of branches indexed by branch display name for the given repository.
func (client Client) GetBranches(projectKey, repositorySlug string) (map[string]Branch, error) {
	start := 0
	branches := make(map[string]Branch)
	morePages := true
	for morePages {
		var data []byte
		retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
		workit := func() error {
			req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s/branches?start=%d&limit=%d", client.baseURL.String(), projectKey, repositorySlug, start, stashPageLimit), nil)
			if err != nil {
				return err
			}
			req.Header.Set("Accept", "application/json")
			req.SetBasicAuth(client.userName, client.password)

			var responseCode int
			responseCode, data, err = consumeResponse(req)
			if err != nil {
				return err
			}

			if responseCode != http.StatusOK {
				var reason string = "unhandled reason"
				switch {
				case responseCode == http.StatusNotFound:
					reason = "Not found"
				case responseCode == http.StatusUnauthorized:
					reason = "Unauthorized"
				}
				return errorResponse{StatusCode: responseCode, Reason: reason}
			}
			return nil
		}
		if err := retry.Try(workit); err != nil {
			return nil, err
		}

		var r Branches
		if err := json.Unmarshal(data, &r); err != nil {
			return nil, err
		}
		for _, branch := range r.Branch {
			branches[branch.DisplayID] = branch
		}
		morePages = !r.IsLastPage
		start = r.NextPageStart
	}
	return branches, nil
}
开发者ID:xoom,项目名称:branchlock,代码行数:50,代码来源:stash.go

示例13: GetPullRequests

// GetPullRequests returns a list of pull requests for a project / slug.
func (client Client) GetPullRequests(projectKey, projectSlug, state string) ([]PullRequest, error) {
	start := 0
	pullRequests := make([]PullRequest, 0)
	morePages := true
	for morePages {
		retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)
		var data []byte
		work := func() error {
			req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s/pull-requests?state=%s&start=%d&limit=%d", client.baseURL.String(), projectKey, projectSlug, state, start, stashPageLimit), nil)
			if err != nil {
				return err
			}
			req.Header.Set("Accept", "application/json")
			req.SetBasicAuth(client.userName, client.password)

			var responseCode int
			responseCode, data, err = consumeResponse(req)
			if err != nil {
				return err
			}
			if responseCode != http.StatusOK {
				var reason string = "unhandled reason"
				switch {
				case responseCode == http.StatusBadRequest:
					reason = "Bad request."
				}
				return errorResponse{StatusCode: responseCode, Reason: reason}
			}
			return nil
		}
		if err := retry.Try(work); err != nil {
			return nil, err
		}

		var r PullRequests
		err := json.Unmarshal(data, &r)
		if err != nil {
			return nil, err
		}
		for _, pr := range r.PullRequests {
			pullRequests = append(pullRequests, pr)
		}
		morePages = !r.IsLastPage
		start = r.NextPageStart
	}
	return pullRequests, nil
}
开发者ID:xoom,项目名称:branchlock,代码行数:48,代码来源:stash.go

示例14: GetRepository

// GetRepository returns a repository representation for the given Stash Project key and repository slug.
func (client Client) GetRepository(projectKey, repositorySlug string) (Repository, error) {
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	var r Repository
	work := func() error {
		req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/api/1.0/projects/%s/repos/%s", client.baseURL.String(), projectKey, repositorySlug), nil)
		if err != nil {
			return err
		}
		Log.Printf("stash.GetRepository %s\n", req.URL)
		req.Header.Set("Accept", "application/json")
		// use credentials if we have them.  If not, the repository must be public.
		if client.userName != "" && client.password != "" {
			req.SetBasicAuth(client.userName, client.password)
		}

		responseCode, data, err := consumeResponse(req)
		if err != nil {
			return err
		}

		if responseCode != http.StatusOK {
			var reason string = "unhandled reason"
			switch {
			case responseCode == http.StatusNotFound:
				reason = "Not found"
			case responseCode == http.StatusUnauthorized:
				reason = "Unauthorized"
			}
			return errorResponse{StatusCode: responseCode, Reason: reason}
		}

		err = json.Unmarshal(data, &r)
		if err != nil {
			return err
		}
		return nil
	}

	return r, retry.Try(work)
}
开发者ID:anthonydahanne,项目名称:stash,代码行数:42,代码来源:stash.go

示例15: GetBranchRestrictions

func (client Client) GetBranchRestrictions(projectKey, repositorySlug string) (BranchRestrictions, error) {
	retry := retry.New(3*time.Second, 3, retry.DefaultBackoffFunc)

	var branchRestrictions BranchRestrictions
	work := func() error {
		req, err := http.NewRequest("GET", fmt.Sprintf("%s/rest/branch-permissions/1.0/projects/%s/repos/%s/restricted", client.baseURL.String(), projectKey, repositorySlug), nil)
		if err != nil {
			return err
		}
		log.Printf("stash.GetBranchRestrictions %s\n", req.URL)
		req.Header.Set("Accept", "application/json")
		req.SetBasicAuth(client.userName, client.password)

		responseCode, data, err := consumeResponse(req)
		if err != nil {
			return err
		}

		if responseCode != http.StatusOK {
			var reason string = "unhandled reason"
			switch {
			case responseCode == http.StatusNotFound:
				reason = "Not found"
			case responseCode == http.StatusUnauthorized:
				reason = "Unauthorized"
			}
			return errorResponse{StatusCode: responseCode, Reason: reason}
		}

		err = json.Unmarshal(data, &branchRestrictions)
		if err != nil {
			return err
		}
		return nil
	}

	return branchRestrictions, retry.Try(work)
}
开发者ID:xoom,项目名称:branchlock,代码行数:38,代码来源:stash.go


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