Skip to content
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

int value not stored #29

Closed
tgarm opened this issue Jul 27, 2020 · 4 comments
Closed

int value not stored #29

tgarm opened this issue Jul 27, 2020 · 4 comments

Comments

@tgarm
Copy link

tgarm commented Jul 27, 2020

It seems the int value is not stored correctly in session.
When serving with the following test code, GET from /, the value of session['incr'] is expected to increment each time, but it always keep the same.

The code uses sqlite3 as the provider.

Is the code written wrongly or session does not support int type, or it is a bug of the session provider?

Please have a look at this code:

package main

import (
    "fmt"
    "log"

    "github.com/fasthttp/router"
    "github.com/fasthttp/session/v2"
    "github.com/fasthttp/session/v2/providers/sqlite3"
    "github.com/valyala/fasthttp"
)

var serverSession *session.Session

func incrHandler(ctx *fasthttp.RequestCtx) {
    store, err := serverSession.Get(ctx)
    if err != nil {
        ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
        return
    }   
    defer func() {
        if err := serverSession.Save(ctx, store); err != nil {
            ctx.Error(err.Error(), fasthttp.StatusInternalServerError)
        }   
    }() 

    cval,ok := store.Get("incr").(int)
    if ok {
        cval++
    }else{
        log.Println("get incr failed,cval",cval)
        cval = 1 
    }   

    store.Set("incr", cval)

    sessionID := store.GetSessionID()
    ctx.SetBodyString(fmt.Sprintf("Session(%s) SET: incr='%d' --> OK", sessionID, store.Get("incr").(int)))
}

func init() {
    provider, err := sqlite3.New(sqlite3.NewConfigWith("test.db", "session"))
    if err != nil {
        log.Fatal(err)
    }

    cfg := session.NewDefaultConfig()
    cfg.EncodeFunc = session.Base64Encode
    cfg.DecodeFunc = session.Base64Decode
    serverSession = session.New(cfg)

    if err = serverSession.SetProvider(provider); err != nil {
        log.Fatal(err)
    }
}

func main() {
    r := router.New()
    r.GET("/", incrHandler)

    addr := "0.0.0.0:8086"
    log.Println("Now Listen on %s",addr)

    err := fasthttp.ListenAndServe(addr, r.Handler)
    if err != nil {
        log.Fatal(err)
    }
}
@thomasvvugt
Copy link

Can you test whether int64 works?
I however use Redis as the session provider, but that did the trick for me.

@tgarm
Copy link
Author

tgarm commented Jul 30, 2020

Just tried, int64 works.

    cval,ok := store.Get("incr").(int64)
    if ok {
        cval++
    }else{
        log.Println("get incr failed,cval",cval)
        cval = 1 
    }   

    store.Set("incr", cval)

    sessionID := store.GetSessionID()
    ctx.SetBodyString(fmt.Sprintf("Session(%s) SET: incr='%d' --> OK", sessionID, store.Get("incr").(int64)))

Can you test whether int64 works?
I however use Redis as the session provider, but that did the trick for me.

@savsgio
Copy link
Member

savsgio commented Aug 11, 2020

Hi @tgarm and @thomasvvugt,

Sorry for delayed answer, i was in vacation.

The base enconder/decoder of session is msgp that's encodes int values as int64, independently of the chosen provider.
You could see that here: https://github.com/tinylib/msgp/blob/master/msgp/write_bytes.go#L359

So the "trick" or the correct way is to always use int64 type.

@savsgio savsgio closed this as completed Aug 11, 2020
@thomasvvugt
Copy link

Hi @savsgio, thanks for your response.
I hope you had a great vacation!
Thanks for the clarification regarding the use of int64 as the correct data type.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants