Skip to content

Commit

Permalink
daemon: pass context as an explicit parameter to request handlers
Browse files Browse the repository at this point in the history
Signed-off-by: Maciej Borzecki <[email protected]>
  • Loading branch information
bboozzoo committed Jun 27, 2024
1 parent 4f43162 commit f01e830
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 77 deletions.
6 changes: 3 additions & 3 deletions daemon/api_aliases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -608,7 +608,7 @@ func (s *aliasesSuite) TestInstallUnaliased(c *check.C) {
st := d.Overlord().State()
st.Lock()
defer st.Unlock()
_, _, err := inst.Dispatch()(inst, st)
_, _, err := inst.Dispatch()(context.Background(), inst, st)
c.Check(err, check.IsNil)

c.Check(calledFlags.Unaliased, check.Equals, true)
Expand All @@ -635,7 +635,7 @@ func (s *aliasesSuite) TestInstallIgnoreRunning(c *check.C) {
st := d.Overlord().State()
st.Lock()
defer st.Unlock()
_, _, err := inst.Dispatch()(inst, st)
_, _, err := inst.Dispatch()(context.Background(), inst, st)
c.Check(err, check.IsNil)

c.Check(calledFlags.IgnoreRunning, check.Equals, true)
Expand All @@ -661,7 +661,7 @@ func (s *aliasesSuite) TestInstallPrefer(c *check.C) {
st := d.Overlord().State()
st.Lock()
defer st.Unlock()
_, _, err := inst.Dispatch()(inst, st)
_, _, err := inst.Dispatch()(context.Background(), inst, st)
c.Check(err, check.IsNil)

c.Check(calledFlags.Prefer, check.Equals, true)
Expand Down
52 changes: 24 additions & 28 deletions daemon/api_snaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ func postSnap(c *Command, r *http.Request, user *auth.UserState) Response {
if err := decoder.Decode(&inst); err != nil {
return BadRequest("cannot decode request body into snap instruction: %v", err)
}
inst.ctx = r.Context()

st := c.d.overlord.State()
st.Lock()
Expand All @@ -145,8 +144,7 @@ func postSnap(c *Command, r *http.Request, user *auth.UserState) Response {
return BadRequest("unknown action %s", inst.Action)
}

// TODO use an explicit context parameter
msg, tsets, err := impl(&inst, st)
msg, tsets, err := impl(r.Context(), &inst, st)
if err != nil {
return inst.errToResponse(err)
}
Expand Down Expand Up @@ -220,7 +218,6 @@ type snapInstruction struct {

// The fields below should not be unmarshalled into. Do not export them.
userID int
ctx context.Context
}

func (inst *snapInstruction) revnoOpts() *snapstate.RevisionOptions {
Expand Down Expand Up @@ -413,7 +410,7 @@ func modeFlags(devMode, jailMode, classic bool) (snapstate.Flags, error) {
return flags, nil
}

func snapInstall(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
func snapInstall(ctx context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
if len(inst.Snaps[0]) == 0 {
return "", nil, fmt.Errorf(i18n.G("cannot install snap with empty name"))
}
Expand All @@ -430,7 +427,7 @@ func snapInstall(inst *snapInstruction, st *state.State) (string, []*state.TaskS
ckey = strutil.ElliptLeft(inst.CohortKey, 10)
logger.Noticef("Installing snap %q from cohort %q", inst.Snaps[0], ckey)
}
tset, err := snapstateInstall(inst.ctx, st, inst.Snaps[0], inst.revnoOpts(), inst.userID, flags)
tset, err := snapstateInstall(ctx, st, inst.Snaps[0], inst.revnoOpts(), inst.userID, flags)
if err != nil {
return "", nil, err
}
Expand All @@ -445,7 +442,7 @@ func snapInstall(inst *snapInstruction, st *state.State) (string, []*state.TaskS
return msg, []*state.TaskSet{tset}, nil
}

func snapUpdate(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
func snapUpdate(_ context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
// TODO: bail if revision is given (and != current?), *or* behave as with install --revision?
flags, err := inst.modeFlags()
if err != nil {
Expand Down Expand Up @@ -479,7 +476,7 @@ func snapUpdate(inst *snapInstruction, st *state.State) (string, []*state.TaskSe
return msg, []*state.TaskSet{ts}, nil
}

func snapRemove(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
func snapRemove(_ context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
ts, err := snapstate.Remove(st, inst.Snaps[0], inst.Revision, &snapstate.RemoveFlags{Purge: inst.Purge})
if err != nil {
return "", nil, err
Expand All @@ -489,7 +486,7 @@ func snapRemove(inst *snapInstruction, st *state.State) (string, []*state.TaskSe
return msg, []*state.TaskSet{ts}, nil
}

func snapRevert(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
func snapRevert(_ context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
var ts *state.TaskSet

flags, err := inst.modeFlags()
Expand All @@ -510,7 +507,7 @@ func snapRevert(inst *snapInstruction, st *state.State) (string, []*state.TaskSe
return msg, []*state.TaskSet{ts}, nil
}

func snapEnable(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
func snapEnable(_ context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
if !inst.Revision.Unset() {
return "", nil, errors.New("enable takes no revision")
}
Expand All @@ -523,7 +520,7 @@ func snapEnable(inst *snapInstruction, st *state.State) (string, []*state.TaskSe
return msg, []*state.TaskSet{ts}, nil
}

func snapDisable(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
func snapDisable(_ context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
if !inst.Revision.Unset() {
return "", nil, errors.New("disable takes no revision")
}
Expand All @@ -536,7 +533,7 @@ func snapDisable(inst *snapInstruction, st *state.State) (string, []*state.TaskS
return msg, []*state.TaskSet{ts}, nil
}

func snapSwitch(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
func snapSwitch(_ context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
if !inst.Revision.Unset() {
return "", nil, errors.New("switch takes no revision")
}
Expand All @@ -562,8 +559,8 @@ func snapSwitch(inst *snapInstruction, st *state.State) (string, []*state.TaskSe
}

// snapHold holds refreshes for one snap.
func snapHold(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
res, err := snapHoldMany(inst, st)
func snapHold(ctx context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
res, err := snapHoldMany(ctx, inst, st)
if err != nil {
return "", nil, err
}
Expand All @@ -572,16 +569,16 @@ func snapHold(inst *snapInstruction, st *state.State) (string, []*state.TaskSet,
}

// snapUnhold removes the hold on refreshes for one snap.
func snapUnhold(inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
res, err := snapUnholdMany(inst, st)
func snapUnhold(ctx context.Context, inst *snapInstruction, st *state.State) (string, []*state.TaskSet, error) {
res, err := snapUnholdMany(ctx, inst, st)
if err != nil {
return "", nil, err
}

return res.Summary, res.Tasksets, nil
}

type snapActionFunc func(*snapInstruction, *state.State) (string, []*state.TaskSet, error)
type snapActionFunc func(context.Context, *snapInstruction, *state.State) (string, []*state.TaskSet, error)

var snapInstructionDispTable = map[string]snapActionFunc{
"install": snapInstall,
Expand Down Expand Up @@ -644,7 +641,6 @@ func snapOpMany(c *Command, r *http.Request, user *auth.UserState) Response {
if err := decoder.Decode(&inst); err != nil {
return BadRequest("cannot decode request body into snap instruction: %v", err)
}
inst.ctx = r.Context()

// TODO: inst.Amend, etc?
if inst.Channel != "" || !inst.Revision.Unset() || inst.DevMode || inst.JailMode || inst.CohortKey != "" || inst.LeaveCohort || inst.Prefer {
Expand All @@ -666,8 +662,8 @@ func snapOpMany(c *Command, r *http.Request, user *auth.UserState) Response {
if op == nil {
return BadRequest("unsupported multi-snap operation %q", inst.Action)
}
// TODO make ctx an explicit parameter
res, err := op(&inst, st)

res, err := op(r.Context(), &inst, st)
if err != nil {
return inst.errToResponse(err)
}
Expand All @@ -688,7 +684,7 @@ func snapOpMany(c *Command, r *http.Request, user *auth.UserState) Response {
return AsyncResponse(res.Result, chg.ID())
}

type snapManyActionFunc func(*snapInstruction, *state.State) (*snapInstructionResult, error)
type snapManyActionFunc func(context.Context, *snapInstruction, *state.State) (*snapInstructionResult, error)

func (inst *snapInstruction) dispatchForMany() (op snapManyActionFunc) {
switch inst.Action {
Expand All @@ -713,7 +709,7 @@ func (inst *snapInstruction) dispatchForMany() (op snapManyActionFunc) {
return op
}

func snapInstallMany(inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
func snapInstallMany(_ context.Context, inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
for _, name := range inst.Snaps {
if len(name) == 0 {
return nil, fmt.Errorf(i18n.G("cannot install snap with empty name"))
Expand Down Expand Up @@ -745,7 +741,7 @@ func snapInstallMany(inst *snapInstruction, st *state.State) (*snapInstructionRe
}, nil
}

func snapUpdateMany(inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
func snapUpdateMany(ctx context.Context, inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
// we need refreshed snap-declarations to enforce refresh-control as best as
// we can, this also ensures that snap-declarations and their prerequisite
// assertions are updated regularly; update validation sets assertions only
Expand All @@ -758,7 +754,7 @@ func snapUpdateMany(inst *snapInstruction, st *state.State) (*snapInstructionRes
}

transaction := inst.Transaction
updated, tasksets, err := snapstateUpdateMany(inst.ctx, st, inst.Snaps, nil, inst.userID, &snapstate.Flags{
updated, tasksets, err := snapstateUpdateMany(ctx, st, inst.Snaps, nil, inst.userID, &snapstate.Flags{
IgnoreRunning: inst.IgnoreRunning,
Transaction: transaction,
})
Expand Down Expand Up @@ -795,7 +791,7 @@ func snapUpdateMany(inst *snapInstruction, st *state.State) (*snapInstructionRes
}, nil
}

func snapEnforceValidationSets(inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
func snapEnforceValidationSets(_ context.Context, inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
if len(inst.ValidationSets) > 0 && len(inst.Snaps) != 0 {
return nil, fmt.Errorf("snap names cannot be specified with validation sets to enforce")
}
Expand Down Expand Up @@ -885,7 +881,7 @@ func meetSnapConstraintsForEnforce(inst *snapInstruction, st *state.State, vErr
return snapstateResolveValSetsEnforcementError(context.TODO(), st, vErr, pinnedSeqs, inst.userID)
}

func snapRemoveMany(inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
func snapRemoveMany(_ context.Context, inst *snapInstruction, st *state.State) (*snapInstructionResult, error) {
flags := &snapstate.RemoveFlags{Purge: inst.Purge}
removed, tasksets, err := snapstateRemoveMany(st, inst.Snaps, flags)
if err != nil {
Expand Down Expand Up @@ -1002,7 +998,7 @@ func shouldSearchStore(r *http.Request) bool {
return false
}

func snapHoldMany(inst *snapInstruction, st *state.State) (res *snapInstructionResult, err error) {
func snapHoldMany(_ context.Context, inst *snapInstruction, st *state.State) (res *snapInstructionResult, err error) {
var msg string
var tss []*state.TaskSet
if len(inst.Snaps) == 0 {
Expand Down Expand Up @@ -1036,7 +1032,7 @@ func snapHoldMany(inst *snapInstruction, st *state.State) (res *snapInstructionR
}, nil
}

func snapUnholdMany(inst *snapInstruction, st *state.State) (res *snapInstructionResult, err error) {
func snapUnholdMany(_ context.Context, inst *snapInstruction, st *state.State) (res *snapInstructionResult, err error) {
var msg string
var tss []*state.TaskSet

Expand Down
Loading

0 comments on commit f01e830

Please sign in to comment.