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

Issue with example directory #121

Closed
quentinalbertone opened this issue Aug 14, 2022 · 5 comments
Closed

Issue with example directory #121

quentinalbertone opened this issue Aug 14, 2022 · 5 comments

Comments

@quentinalbertone
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.18.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

What version of Go-Guardian are you using ?

Go-Guardian Version:  v2.11.5
Libcache Version: v1.0.5

What did you do?

Hello,
I just want to follow the jwt example https://github.com/shaj13/go-guardian/blob/master/_examples/jwt/main.go
But the function RegisterOnExpired was deprecated and no-longer works.
Can you update the example directory on this repo.

What did you expect to see?

The program init and run

What did you see instead?

$> go run ./...
panic: RegisterOnExpired no longer available

goroutine 1 [running]:
github.com/shaj13/libcache/internal.(*Cache).RegisterOnExpired(0x18?, 0x7c80a0?)
	/home/xxx/go/pkg/mod/github.com/shaj13/libcache@v1.0.5/internal/cache.go:379 +0x27
github.com/shaj13/libcache.(*cache).RegisterOnExpired(0xc00009e120, 0x6?)
	/home/xxx/go/pkg/mod/github.com/shaj13/libcache@v1.0.5/cache.go:245 +0x56
main.setupGoGuardianJWT()
	/home/xxx/go/src/gitlab.com/xxx/my-project/cmd/go-guardian-jwt.go:33 +0x17d
main.main()
	/home/xxx/go/src/gitlab.com/xxx/my-project/cmd/main.go:20 +0x1d
exit status 2
@Ivasan7
Copy link

Ivasan7 commented Aug 26, 2022

+1

@Ivasan7
Copy link

Ivasan7 commented Aug 26, 2022

@quentinalbertone

I have found a workaround that works for me, maybe a bit hacky, but try it.

func setupGoGuardian() {
	keeper = jwt.StaticSecret{
		ID:        "secret-id",
		Secret:    []byte("secret"),
		Algorithm: jwt.HS256,
	}
	cache := libcache.FIFO.New(0)
	cache.SetTTL(time.Minute * 5)
	chl := make(chan libcache.Event)
	defer close(chl)

	cache.Notify(chl, libcache.Remove)
	
	go func(chl chan libcache.Event) {
		event := <-chl
		cache.Peek(event.Key)
	}(chl)
	basicStrategy := basic.NewCached(validateUser, cache)
	jwtStrategy := jwt.New(cache, keeper)
	strategy = union.New(jwtStrategy, basicStrategy)
}

@Ivasan7
Copy link

Ivasan7 commented Aug 26, 2022

Added also a PR
#122

@Ivasan7
Copy link

Ivasan7 commented Aug 26, 2022

@quentinalbertone

One more bug after, but then it sort of works. Apply the following change as well.

#123

@shaj13
Copy link
Owner

shaj13 commented Aug 30, 2022

@quentinalbertone will update the example asap.
@Ivasan7 thanks, will take a look into your PR,

with the new version of libcache you can clean the cache in two different mechanisms

  • Let the cache clean itself lazily on the read-write operation.
func setupGoGuardian() {
	keeper = jwt.StaticSecret{
		ID:        "secret-id",
		Secret:    []byte("secret"),
		Algorithm: jwt.HS256,
	}
	cache := libcache.FIFO.New(0)
	cache.SetTTL(time.Minute * 5)
	basicStrategy := basic.NewCached(validateUser, cache)
	jwtStrategy := jwt.New(cache, keeper)
	strategy = union.New(jwtStrategy, basicStrategy)
}
  • spawn a garbage collector to collect expired items on time
func setupGoGuardian() {
	keeper = jwt.StaticSecret{
		ID:        "secret-id",
		Secret:    []byte("secret"),
		Algorithm: jwt.HS256,
	}
	cache := libcache.FIFO.New(0)
	cache.SetTTL(time.Minute * 5)
        go libcache.GC(context.TODO(), cache)
	basicStrategy := basic.NewCached(validateUser, cache)
	jwtStrategy := jwt.New(cache, keeper)
	strategy = union.New(jwtStrategy, basicStrategy)
}

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