mirror of
https://github.com/portainer/portainer.git
synced 2025-07-25 08:19:40 +02:00
chore(code): reduce the code duplication EE-7278 (#11969)
This commit is contained in:
parent
39bdfa4512
commit
9ee092aa5e
85 changed files with 520 additions and 618 deletions
20
api/logoutcontext/logout_context.go
Normal file
20
api/logoutcontext/logout_context.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
package logoutcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
const LogoutPrefix = "logout-"
|
||||
|
||||
func GetContext(token string) context.Context {
|
||||
return GetService(logoutToken(token)).GetLogoutCtx()
|
||||
}
|
||||
|
||||
func Cancel(token string) {
|
||||
GetService(logoutToken(token)).Cancel()
|
||||
RemoveService(logoutToken(token))
|
||||
}
|
||||
|
||||
func logoutToken(token string) string {
|
||||
return LogoutPrefix + token
|
||||
}
|
28
api/logoutcontext/service.go
Normal file
28
api/logoutcontext/service.go
Normal file
|
@ -0,0 +1,28 @@
|
|||
package logoutcontext
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type (
|
||||
Service struct {
|
||||
ctx context.Context
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
)
|
||||
|
||||
func NewService() *Service {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
return &Service{
|
||||
ctx: ctx,
|
||||
cancel: cancel,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) Cancel() {
|
||||
s.cancel()
|
||||
}
|
||||
|
||||
func (s *Service) GetLogoutCtx() context.Context {
|
||||
return s.ctx
|
||||
}
|
34
api/logoutcontext/service_factory.go
Normal file
34
api/logoutcontext/service_factory.go
Normal file
|
@ -0,0 +1,34 @@
|
|||
package logoutcontext
|
||||
|
||||
import "sync"
|
||||
|
||||
type (
|
||||
ServiceFactory struct {
|
||||
mu sync.Mutex
|
||||
services map[string]*Service
|
||||
}
|
||||
)
|
||||
|
||||
var serviceFactory = ServiceFactory{
|
||||
services: make(map[string]*Service),
|
||||
}
|
||||
|
||||
func GetService(token string) *Service {
|
||||
serviceFactory.mu.Lock()
|
||||
defer serviceFactory.mu.Unlock()
|
||||
|
||||
service, ok := serviceFactory.services[token]
|
||||
if !ok {
|
||||
service = NewService()
|
||||
serviceFactory.services[token] = service
|
||||
}
|
||||
|
||||
return service
|
||||
}
|
||||
|
||||
func RemoveService(token string) {
|
||||
serviceFactory.mu.Lock()
|
||||
defer serviceFactory.mu.Unlock()
|
||||
|
||||
delete(serviceFactory.services, token)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue