Skip to content

Commit

Permalink
authz-service/postgres: cleanup superfluous 'context.WithCancel's
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <[email protected]>
  • Loading branch information
srenatus committed Jun 6, 2019
1 parent e013cd9 commit 892e989
Showing 1 changed file with 8 additions and 71 deletions.
79 changes: 8 additions & 71 deletions components/authz-service/storage/v2/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,14 @@ type Querier interface {

// CreatePolicy stores a new policy and its statements in postgres and returns the final policy.
func (p *pg) CreatePolicy(ctx context.Context, pol *v2.Policy) (*v2.Policy, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

// Note(sr): we're using BeginTx with the context that'll be cancelled in a
// `defer` when the function ends. This should rollback transactions that
// haven't been committed -- what would happen when any of the following
// `err != nil` cases return early.
// However, I haven't played with this extensively, so there's a bit of a
// chance that this understanding is just plain wrong.

// Note(sr): we're using BeginTx with the context that'll be cancelled when
// GRPC is done with the method call.
// This should rollback transactions that haven't been committed -- what would
// happen when any of the following `err != nil` cases return early.
//
// See https://gist.github.com/srenatus/ce12b31ea517f16c024e4f8736fa5f2b for
// a toy experiment asserting that the context actually is cancelled when the
// GRPC method is done.
tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -98,8 +96,6 @@ func (p *pg) CreatePolicy(ctx context.Context, pol *v2.Policy) (*v2.Policy, erro

func (p *pg) PurgeSubjectFromPolicies(ctx context.Context, sub string) ([]string, error) {
var polIDs []string
ctx, cancel := context.WithCancel(ctx)
defer cancel()
tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -175,9 +171,6 @@ func (p *pg) GetPolicy(ctx context.Context, id string) (*v2.Policy, error) {
}

func (p *pg) DeletePolicy(ctx context.Context, id string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return p.processError(err)
Expand Down Expand Up @@ -211,9 +204,6 @@ func (p *pg) DeletePolicy(ctx context.Context, id string) error {
}

func (p *pg) UpdatePolicy(ctx context.Context, pol *v2.Policy) (*v2.Policy, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -431,9 +421,6 @@ func (p *pg) queryPolicy(ctx context.Context, id string, q Querier) (*v2.Policy,
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

func (p *pg) ListPolicyMembers(ctx context.Context, id string) ([]v2.Member, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return nil, p.processError(err)
Expand All @@ -460,9 +447,6 @@ func (p *pg) ListPolicyMembers(ctx context.Context, id string) ([]v2.Member, err
}

func (p *pg) AddPolicyMembers(ctx context.Context, id string, members []v2.Member) ([]v2.Member, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -506,9 +490,6 @@ func (p *pg) AddPolicyMembers(ctx context.Context, id string, members []v2.Membe
}

func (p *pg) ReplacePolicyMembers(ctx context.Context, policyID string, members []v2.Member) ([]v2.Member, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -555,10 +536,6 @@ func (p *pg) ReplacePolicyMembers(ctx context.Context, policyID string, members
// list of members to remove and return the list of remaining users.
func (p *pg) RemovePolicyMembers(ctx context.Context,
policyID string, members []v2.Member) ([]v2.Member, error) {

ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -682,9 +659,6 @@ func (p *pg) getPolicyMembersWithQuerier(ctx context.Context, id string, q Queri
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

func (p *pg) CreateRole(ctx context.Context, role *v2.Role) (*v2.Role, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil /* use driver default */)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -739,9 +713,6 @@ func (p *pg) ListRoles(ctx context.Context) ([]*v2.Role, error) {
}

func (p *pg) GetRole(ctx context.Context, id string) (*v2.Role, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

projectsFilter, err := projectsListFromContext(ctx)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -776,9 +747,6 @@ func (p *pg) GetRole(ctx context.Context, id string) (*v2.Role, error) {
}

func (p *pg) DeleteRole(ctx context.Context, id string) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

projectsFilter, err := projectsListFromContext(ctx)
if err != nil {
return p.processError(err)
Expand Down Expand Up @@ -823,9 +791,6 @@ func (p *pg) DeleteRole(ctx context.Context, id string) error {
}

func (p *pg) UpdateRole(ctx context.Context, role *v2.Role) (*v2.Role, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

projectsFilter, err := projectsListFromContext(ctx)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -955,9 +920,6 @@ func (p *pg) insertRoleWithQuerier(ctx context.Context, role *v2.Role, q Querier
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

func (p *pg) CreateRule(ctx context.Context, rule *v2.Rule) (*v2.Rule, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -995,10 +957,6 @@ func (p *pg) UpdateRule(ctx context.Context, rule *v2.Rule) (*v2.Rule, error) {
if err != nil {
return nil, p.processError(err)
}

ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -1052,10 +1010,6 @@ func (p *pg) DeleteRule(ctx context.Context, id string) error {
if err != nil {
return p.processError(err)
}

ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil)
if err != nil {
return p.processError(err)
Expand Down Expand Up @@ -1089,10 +1043,6 @@ func (p *pg) GetRule(ctx context.Context, id string) (*v2.Rule, error) {
if err != nil {
return nil, p.processError(err)
}

ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -1123,10 +1073,6 @@ func (p *pg) ListRules(ctx context.Context) ([]*v2.Rule, error) {
if err != nil {
return nil, p.processError(err)
}

ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -1162,9 +1108,6 @@ func (p *pg) ListRules(ctx context.Context) ([]*v2.Rule, error) {
}

func (p *pg) ListRulesForProject(ctx context.Context, projectID string) ([]*v2.Rule, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

projectsFilter, err := projectsListFromContext(ctx)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -1210,9 +1153,6 @@ func (p *pg) ListRulesForProject(ctx context.Context, projectID string) ([]*v2.R
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

func (p *pg) CreateProject(ctx context.Context, project *v2.Project) (*v2.Project, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

tx, err := p.db.BeginTx(ctx, nil)
if err != nil {
return nil, p.processError(err)
Expand Down Expand Up @@ -1244,9 +1184,6 @@ func (p *pg) CreateProject(ctx context.Context, project *v2.Project) (*v2.Projec
}

func (p *pg) UpdateProject(ctx context.Context, project *v2.Project) (*v2.Project, error) {
ctx, cancel := context.WithCancel(ctx)
defer cancel()

projectsFilter, err := projectsListFromContext(ctx)
if err != nil {
return nil, p.processError(err)
Expand Down

0 comments on commit 892e989

Please sign in to comment.