Skip to content

Commit 3183a46

Browse files
zeripathlunny
andauthored
Make modules/context.Context a context.Context (#16031)
* Make modules/context.Context a context.Context Signed-off-by: Andrew Thornton <art27@cantab.net> * Simplify context calls Signed-off-by: Andrew Thornton <art27@cantab.net> * Set the base context for requests to the HammerContext Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>
1 parent 518ed50 commit 3183a46

File tree

14 files changed

+46
-23
lines changed

14 files changed

+46
-23
lines changed

modules/context/context.go

+21-1
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ func (ctx *Context) ParamsInt64(p string) int64 {
509509

510510
// SetParams set params into routes
511511
func (ctx *Context) SetParams(k, v string) {
512-
chiCtx := chi.RouteContext(ctx.Req.Context())
512+
chiCtx := chi.RouteContext(ctx)
513513
chiCtx.URLParams.Add(strings.TrimPrefix(k, ":"), url.PathEscape(v))
514514
}
515515

@@ -528,6 +528,26 @@ func (ctx *Context) Status(status int) {
528528
ctx.Resp.WriteHeader(status)
529529
}
530530

531+
// Deadline is part of the interface for context.Context and we pass this to the request context
532+
func (ctx *Context) Deadline() (deadline time.Time, ok bool) {
533+
return ctx.Req.Context().Deadline()
534+
}
535+
536+
// Done is part of the interface for context.Context and we pass this to the request context
537+
func (ctx *Context) Done() <-chan struct{} {
538+
return ctx.Req.Context().Done()
539+
}
540+
541+
// Err is part of the interface for context.Context and we pass this to the request context
542+
func (ctx *Context) Err() error {
543+
return ctx.Req.Context().Err()
544+
}
545+
546+
// Value is part of the interface for context.Context and we pass this to the request context
547+
func (ctx *Context) Value(key interface{}) interface{} {
548+
return ctx.Req.Context().Value(key)
549+
}
550+
531551
// Handler represents a custom handler
532552
type Handler func(*Context)
533553

modules/graceful/server_http.go

+3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package graceful
66

77
import (
8+
"context"
89
"crypto/tls"
10+
"net"
911
"net/http"
1012
)
1113

@@ -16,6 +18,7 @@ func newHTTPServer(network, address, name string, handler http.Handler) (*Server
1618
WriteTimeout: DefaultWriteTimeOut,
1719
MaxHeaderBytes: DefaultMaxHeaderBytes,
1820
Handler: handler,
21+
BaseContext: func(net.Listener) context.Context { return GetManager().HammerContext() },
1922
}
2023
server.OnShutdown = func() {
2124
httpServer.SetKeepAlivesEnabled(false)

routers/admin/users.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ func NewUserPost(ctx *context.Context) {
113113
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserNew, &form)
114114
return
115115
}
116-
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
116+
pwned, err := password.IsPwned(ctx, form.Password)
117117
if pwned {
118118
ctx.Data["Err_Password"] = true
119119
errMsg := ctx.Tr("auth.password_pwned")
@@ -256,7 +256,7 @@ func EditUserPost(ctx *context.Context) {
256256
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplUserEdit, &form)
257257
return
258258
}
259-
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
259+
pwned, err := password.IsPwned(ctx, form.Password)
260260
if pwned {
261261
ctx.Data["Err_Password"] = true
262262
errMsg := ctx.Tr("auth.password_pwned")

routers/api/v1/admin/user.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func CreateUser(ctx *context.APIContext) {
8888
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
8989
return
9090
}
91-
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
91+
pwned, err := password.IsPwned(ctx, form.Password)
9292
if pwned {
9393
if err != nil {
9494
log.Error(err.Error())
@@ -162,7 +162,7 @@ func EditUser(ctx *context.APIContext) {
162162
ctx.Error(http.StatusBadRequest, "PasswordComplexity", err)
163163
return
164164
}
165-
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
165+
pwned, err := password.IsPwned(ctx, form.Password)
166166
if pwned {
167167
if err != nil {
168168
log.Error(err.Error())

routers/events/events.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func Events(ctx *context.Context) {
4242
}
4343

4444
// Listen to connection close and un-register messageChan
45-
notify := ctx.Req.Context().Done()
45+
notify := ctx.Done()
4646
ctx.Resp.Flush()
4747

4848
shutdownCtx := graceful.GetManager().ShutdownContext()

routers/install.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ func InstallPost(ctx *context.Context) {
400400
}
401401

402402
// Re-read settings
403-
PostInstallInit(ctx.Req.Context())
403+
PostInstallInit(ctx)
404404

405405
// Create admin account
406406
if len(form.AdminName) > 0 {
@@ -454,7 +454,7 @@ func InstallPost(ctx *context.Context) {
454454

455455
// Now get the http.Server from this request and shut it down
456456
// NB: This is not our hammerable graceful shutdown this is http.Server.Shutdown
457-
srv := ctx.Req.Context().Value(http.ServerContextKey).(*http.Server)
457+
srv := ctx.Value(http.ServerContextKey).(*http.Server)
458458
go func() {
459459
if err := srv.Shutdown(graceful.GetManager().HammerContext()); err != nil {
460460
log.Error("Unable to shutdown the install server! Error: %v", err)

routers/private/manager.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func FlushQueues(ctx *context.PrivateContext) {
3535
})
3636
return
3737
}
38-
err := queue.GetManager().FlushAll(ctx.Req.Context(), opts.Timeout)
38+
err := queue.GetManager().FlushAll(ctx, opts.Timeout)
3939
if err != nil {
4040
ctx.JSON(http.StatusRequestTimeout, map[string]interface{}{
4141
"err": fmt.Sprintf("%v", err),

routers/private/restore_repo.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func RestoreRepo(ctx *myCtx.PrivateContext) {
3636
}
3737

3838
if err := migrations.RestoreRepository(
39-
ctx.Req.Context(),
39+
ctx,
4040
params.RepoDir,
4141
params.OwnerName,
4242
params.RepoName,

routers/repo/blame.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func RefBlame(ctx *context.Context) {
124124
return
125125
}
126126

127-
blameReader, err := git.CreateBlameReader(ctx.Req.Context(), models.RepoPath(userName, repoName), commitID, fileName)
127+
blameReader, err := git.CreateBlameReader(ctx, models.RepoPath(userName, repoName), commitID, fileName)
128128
if err != nil {
129129
ctx.NotFound("CreateBlameReader", err)
130130
return

routers/repo/lfs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ func LFSPointerFiles(ctx *context.Context) {
414414
err = func() error {
415415
pointerChan := make(chan lfs.PointerBlob)
416416
errChan := make(chan error, 1)
417-
go lfs.SearchPointerBlobs(ctx.Req.Context(), ctx.Repo.GitRepo, pointerChan, errChan)
417+
go lfs.SearchPointerBlobs(ctx, ctx.Repo.GitRepo, pointerChan, errChan)
418418

419419
numPointers := 0
420420
var numAssociated, numNoExist, numAssociatable int

routers/user/auth.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1011,9 +1011,9 @@ func LinkAccountPostRegister(ctx *context.Context) {
10111011
case setting.ImageCaptcha:
10121012
valid = context.GetImageCaptcha().VerifyReq(ctx.Req)
10131013
case setting.ReCaptcha:
1014-
valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse)
1014+
valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
10151015
case setting.HCaptcha:
1016-
valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse)
1016+
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
10171017
default:
10181018
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
10191019
return
@@ -1153,9 +1153,9 @@ func SignUpPost(ctx *context.Context) {
11531153
case setting.ImageCaptcha:
11541154
valid = context.GetImageCaptcha().VerifyReq(ctx.Req)
11551155
case setting.ReCaptcha:
1156-
valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse)
1156+
valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
11571157
case setting.HCaptcha:
1158-
valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse)
1158+
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
11591159
default:
11601160
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
11611161
return
@@ -1191,7 +1191,7 @@ func SignUpPost(ctx *context.Context) {
11911191
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplSignUp, &form)
11921192
return
11931193
}
1194-
pwned, err := password.IsPwned(ctx.Req.Context(), form.Password)
1194+
pwned, err := password.IsPwned(ctx, form.Password)
11951195
if pwned {
11961196
errMsg := ctx.Tr("auth.password_pwned")
11971197
if err != nil {
@@ -1620,7 +1620,7 @@ func ResetPasswdPost(ctx *context.Context) {
16201620
ctx.Data["Err_Password"] = true
16211621
ctx.RenderWithErr(password.BuildComplexityError(ctx), tplResetPassword, nil)
16221622
return
1623-
} else if pwned, err := password.IsPwned(ctx.Req.Context(), passwd); pwned || err != nil {
1623+
} else if pwned, err := password.IsPwned(ctx, passwd); pwned || err != nil {
16241624
errMsg := ctx.Tr("auth.password_pwned")
16251625
if err != nil {
16261626
log.Error(err.Error())

routers/user/auth_openid.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,13 @@ func RegisterOpenIDPost(ctx *context.Context) {
385385
ctx.ServerError("", err)
386386
return
387387
}
388-
valid, err = recaptcha.Verify(ctx.Req.Context(), form.GRecaptchaResponse)
388+
valid, err = recaptcha.Verify(ctx, form.GRecaptchaResponse)
389389
case setting.HCaptcha:
390390
if err := ctx.Req.ParseForm(); err != nil {
391391
ctx.ServerError("", err)
392392
return
393393
}
394-
valid, err = hcaptcha.Verify(ctx.Req.Context(), form.HcaptchaResponse)
394+
valid, err = hcaptcha.Verify(ctx, form.HcaptchaResponse)
395395
default:
396396
ctx.ServerError("Unknown Captcha Type", fmt.Errorf("Unknown Captcha Type: %s", setting.Service.CaptchaType))
397397
return

routers/user/setting/account.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func AccountPost(ctx *context.Context) {
5858
ctx.Flash.Error(ctx.Tr("form.password_not_match"))
5959
} else if !password.IsComplexEnough(form.Password) {
6060
ctx.Flash.Error(password.BuildComplexityError(ctx))
61-
} else if pwned, err := password.IsPwned(ctx.Req.Context(), form.Password); pwned || err != nil {
61+
} else if pwned, err := password.IsPwned(ctx, form.Password); pwned || err != nil {
6262
errMsg := ctx.Tr("auth.password_pwned")
6363
if err != nil {
6464
log.Error(err.Error())

services/archiver/archiver.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func (aReq *ArchiveRequest) IsComplete() bool {
7676
func (aReq *ArchiveRequest) WaitForCompletion(ctx *context.Context) bool {
7777
select {
7878
case <-aReq.cchan:
79-
case <-ctx.Req.Context().Done():
79+
case <-ctx.Done():
8080
}
8181

8282
return aReq.IsComplete()
@@ -92,7 +92,7 @@ func (aReq *ArchiveRequest) TimedWaitForCompletion(ctx *context.Context, dur tim
9292
case <-time.After(dur):
9393
timeout = true
9494
case <-aReq.cchan:
95-
case <-ctx.Req.Context().Done():
95+
case <-ctx.Done():
9696
}
9797

9898
return aReq.IsComplete(), timeout

0 commit comments

Comments
 (0)