kataras / iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:
https://www.iris-go.com
BSD 3-Clause "New" or "Revised" License
25.19k stars 2.47k forks source link

iris set http request timeout #1833

Closed WyntersN closed 2 years ago

WyntersN commented 2 years ago

Describe the issue you are facing or ask for help

HTTP set Read 60s

HTTP set Write 120s

srv := &http.Server{
        Addr:         cf.Domain,
        ReadTimeout:  60 * time.Second,
        WriteTimeout: 120 * time.Second,
    }
    err := app.Run(iris.Server(srv))
    if err != nil {
        service.LogError.Fatalf("IRIS close %s", err)
    }

iris run 8803

image

http request

image

http request time

image

http timeout still 10s

kataras commented 2 years ago

Hello @WyntersN ,

I ran a simple example of your case:

package main

import (
    "net/http"
    "time"

    "github.com/kataras/iris/v12"
)

func main() {
    app := iris.New()
    app.Get("/", handler)

    srv := &http.Server{
        Addr:         ":8083",
        ReadTimeout:  60 * time.Second,
        WriteTimeout: 120 * time.Second,
    }
    app.Run(iris.Server(srv))
}

func handler(ctx iris.Context) {
    time.Sleep(15 * time.Second)
    ctx.WriteString("OK")
}

And it does NOT fire a timeout error on 10 seconds. So timeouts are set correctly. Could you please post a full producable example? Because I can't see the issue here.

WyntersN commented 2 years ago

Thank you for your reply. Sorry to have kept your waiting. @kataras I'd be absolutely delighted to IRIS one's pygmy effort

main.go

package main
import (
    "oa_iris/router"
    "github.com/kataras/iris/v12"
)
func main() {
    app := iris.New()

    router.RouteApi(app)
    service.LogInfo.Println("Api  Reg")
    //router
    app.Party("/api")
    {
        router.RouteApi(app)
        service.LogInfo.Println("Api   reg")
    }
    app.Party("/admin")
    {
        router.RouteAdmin(app)
        service.LogInfo.Println("Admin reg")
    }

        srv := &http.Server{
        Addr:         cf.Domain,
        ReadTimeout:  60 * time.Second,
        WriteTimeout: 120 * time.Second,
    }
    err := app.Run(iris.Server(srv))
    if err != nil {
        service.LogError.Fatalf("IRIS close %s", err)
    }
}

router\api.go

package router

import (
    "oa_iris/app/api"
    v1App "oa_iris/app/api/controllers"
    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/mvc"
)

func RouteApi(app *iris.Application) {
    mvc.New(app.Party("/api/v1/login")).
        Register(api.SessManager.Start).
        Handle(new(v1App.AppLoginController))
    mvc.New(app.Party("/api/v1/user", api.SessionLoginSignAuthApp)).
        Register(api.SessManager.Start).
        Handle(new(v1App.AppUserController))
}

route\api.go token verification

func SessionLoginSignAuthApp(c iris.Context) {
       token:= c.GetHeader("token")
      if token != "" {
                claim, err := common.ParseToken(token)
              if  err != nil  {
                 c.JSON(err)
                return
               }
                common.SessManager = SessManager.Start(c)
                common.SessManager.Set("USER_ID",claim.uid)
               c.Next()
              return
      }
   c.JSON(err)
}

router\admin.go

package router

import (
    "oa_iris/app/admin/controllers"

    "github.com/kataras/iris/v12"
    "github.com/kataras/iris/v12/mvc"
)

func RouteAdmin(app *iris.Application) {
    mvc.New(app.Party("/admin/login")).
     Handle(new(controllers.AdminController))

}

app\api\controllers\user.go

package app

import (
    "oa_iris/app/api"
    "oa_iris/app/api/model/app"
)

type AppUserController struct {
    api.AppController
}

func (c *AppUserController) GetInfo() {
    var retData api.ReturnJson
    retData.Code , retData.Message,retData.Data = app.Info(c.Ctx)
    c.Ctx.JSON(retData)
}

Q1: No SessionLoginSignAuthApp request timeout set success. Exist SessionLoginSignAuthApp request timeout set fail. How can I achieve it? Exist SessionLoginSignAuthApp request timeout set success Q2: user A(uid:10001) user B(uid:10002) user C(uid:10003) If user A get uid set time.Sleep 10s, user B get uid set time.Sleep 5s, user C get uid set time.Sleep 1s. Result use common.SessManager.Get("USER_ID"),User A B C uid become 10003 How to get IRIS uid correctly?

kataras commented 2 years ago

Hello @WyntersN, no worries about the delay, I late to answer too.

  1. Request timeout doesn't mean that your functions (http handlers) will be canceled while they are running (refer to net/http documentation and go in general). To cancel operations you have to make use of the iris.Context.Done() channel inside your services.

  2. Do not fire SessionManager.Start twice. Use the following snippet instead to work with session:

Register the handler

sess := sessions.New(sessions.Config{Cookie: cookieNameForSessionID, AllowReclaim: true})
app.Use(sess.Handler())

To set

session := sessions.Get(ctx)
session.Set("USER_ID", ...)

To retrieve

session := sessions.Get(ctx)
userID := session.Get("USER_ID")
WyntersN commented 2 years ago

Hello @kataras ,Thank you for your reply I need request timeout keep wait.However token verification implement ctx.Next(),function Main set request timeout fail.

WyntersN commented 2 years ago

Ctx.Next(),Set request timeout fail.

WyntersN commented 2 years ago

@kataras

WyntersN commented 2 years ago

@kataras

WyntersN commented 2 years ago

@kataras
Ctx.Next(),Set request timeout fail. @kataras

WyntersN commented 2 years ago

@kataras

WyntersN commented 2 years ago

@kataras

WyntersN commented 2 years ago

@kataras

mvc.New(app.Party("/api/v1/user", api.SessionLoginSignAuthApp)).
        Register(common.SessManager.Start).
        Handle(new(v1App.AppUserController))

api.SessionLoginSignAuthApp
ctx.next()  //Set request timeout fail.
WyntersN commented 2 years ago

@kataras

kataras commented 2 years ago

Hello @WyntersN, could you please provide me a complete example (source code in the comments) than I can copy-paste and re-produce the issue you mention? Thank you a lot