+
Skip to content

Expose Client.Client #2015

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Expose Client.Client #2015

wants to merge 1 commit into from

Conversation

gmlewis
Copy link
Collaborator

@gmlewis gmlewis commented Jul 20, 2021

Fixes: #1859.

@gmlewis gmlewis added the Breaking API Change PR will require a bump to the major version num in next release. Look here to see the change(s). label Jul 20, 2021
@google-cla google-cla bot added the cla: yes Indication that the PR author has signed a Google Contributor License Agreement. label Jul 20, 2021
@codecov
Copy link

codecov bot commented Jul 20, 2021

Codecov Report

Merging #2015 (897288d) into master (b8294c3) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #2015   +/-   ##
=======================================
  Coverage   97.86%   97.86%           
=======================================
  Files         107      107           
  Lines        6897     6897           
=======================================
  Hits         6750     6750           
  Misses         81       81           
  Partials       66       66           
Impacted Files Coverage Δ
github/actions_artifacts.go 100.00% <100.00%> (ø)
github/actions_workflow_jobs.go 100.00% <100.00%> (ø)
github/github.go 97.07% <100.00%> (ø)
github/migrations.go 94.20% <100.00%> (ø)
github/migrations_user.go 93.93% <100.00%> (ø)
github/repos.go 98.75% <100.00%> (ø)
github/repos_contents.go 89.68% <100.00%> (ø)
github/repos_releases.go 89.37% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b8294c3...897288d. Read the comment docs.

@@ -137,7 +137,7 @@ var errNonNilContext = errors.New("context must be non-nil")
// A Client manages communication with the GitHub API.
type Client struct {
clientMu sync.Mutex // clientMu protects the client during calls that modify the CheckRedirect func.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will exposing client directly lead to problems with this mutex? I think two different github.Client objects would each use separate locks to protect the same *http.Client object, leading to race conditions.

Maybe instead there could be a new Client() or HTTPClient() function that returns a shallow copy of the internal client field? That would enable reuse of the transport and timeout while allowing each instance to safely update the redirect function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm... that's an interesting question...

The idea was to prevent multiple calls that check for redirects to avoid swapping out the client at the same time.

But maybe a read-only Client() method as you recommend would be a better solution anyway.

@jeffmendoza
Copy link

Tested this with my real-world code that could use it (below). Works great, I'm not using any concurrency.

// Check performs the policy check for Binary Artifacts based on the
// configuration stored in the org/repo, implementing policydef.Policy.Check()
func (b Binary) Check(ctx context.Context, c *github.Client, owner,
	repo string) (*policydef.Result, error) {
	oc, rc := getConfig(ctx, c, owner, repo)
	enabled := config.IsEnabled(oc.OptConfig, rc.OptConfig, repo)
	log.Info().
		Str("org", owner).
		Str("repo", repo).
		Str("area", polName).
		Bool("enabled", enabled).
		Msg("Check repo enabled")

	gh32c := gh32.NewClient(c.Client)
	repoClient := githubrepo.CreateGithubRepoClient(ctx, gh32c)
	if err := repoClient.InitRepo(owner, repo); err != nil {
		return nil, err
	}
	l := logger{}
	cr := &checker.CheckRequest{
		Ctx:         ctx,
		Client:      gh32c,
		RepoClient:  repoClient,
		HTTPClient:  nil,
		Owner:       owner,
		Repo:        repo,
		GraphClient: nil,
		Logf:        l.Logf,
	}
	// TODO, likely this should be a "scorecard" policy that runs multiple checks
	// here, and uses config to enable/disable checks.
	res := checks.BinaryArtifacts(cr)
	if res.Error != nil {
		return nil, res.Error
	}

@gmlewis
Copy link
Collaborator Author

gmlewis commented Jul 20, 2021

Thank you, @jeffmendoza - hey, how about if I make a read-only version of the Client as described by @bluekeyes above ?

@jeffmendoza
Copy link

I don't see any reason why that wouldn't work as well.

@gmlewis gmlewis mentioned this pull request Jul 20, 2021
@gmlewis
Copy link
Collaborator Author

gmlewis commented Jul 20, 2021

Closing due to preferred solution in #2016.

@gmlewis gmlewis closed this Jul 20, 2021
@gmlewis gmlewis deleted the expose-client branch July 20, 2021 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking API Change PR will require a bump to the major version num in next release. Look here to see the change(s). cla: yes Indication that the PR author has signed a Google Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Modules: Sub-Dependency is using v33, main project v35 - Type incompatibility + Module replacement
3 participants
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载