Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Sep 9, 2024
1 parent 1e66167 commit bc59486
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 15 deletions.
2 changes: 2 additions & 0 deletions contracts/session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ type Manager interface {
Driver(name ...string) (Driver, error)
// Extend extends the session manager with a custom driver.
Extend(driver string, handler func() Driver) error
// ReleaseSession releases the session back to the pool.
ReleaseSession(session Session)
}
10 changes: 7 additions & 3 deletions session/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func NewManager(config config.Config, json foundation.Json) *Manager {
}

func (m *Manager) BuildSession(handler sessioncontract.Driver, sessionID ...string) sessioncontract.Session {
if handler == nil {
panic("session driver cannot be nil")
}
session := m.AcquireSession()
session.setDriver(handler)
session.setJson(m.json)
Expand Down Expand Up @@ -82,9 +85,10 @@ func (m *Manager) AcquireSession() *Session {
return session
}

func (m *Manager) ReleaseSession(session *Session) {
session.reset()
m.sessionPool.Put(session)
func (m *Manager) ReleaseSession(session sessioncontract.Session) {
s := session.(*Session)
s.reset()
m.sessionPool.Put(s)
}

func (m *Manager) getDefaultDriver() string {
Expand Down
7 changes: 6 additions & 1 deletion session/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,13 @@ func (s *ManagerTestSuite) TestExtend() {
}

func (s *ManagerTestSuite) TestBuildSession() {
driver, err := s.manager.Driver("file")
s.Nil(err)
s.NotNil(driver)
s.Equal("*driver.File", fmt.Sprintf("%T", driver))

s.mockConfig.On("GetString", "session.cookie").Return("test_cookie").Once()
session := s.manager.BuildSession(nil)
session := s.manager.BuildSession(driver)
s.NotNil(session)
s.Equal("test_cookie", session.GetName())
}
Expand Down
3 changes: 1 addition & 2 deletions session/middleware/start_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ func StartSession() http.Middleware {
}

// Release session
// TODO - any better way to release the session?
session.SessionFacade.(*session.Manager).ReleaseSession(s.(*session.Session))
session.SessionFacade.ReleaseSession(s)
}
}
16 changes: 8 additions & 8 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,14 +180,6 @@ func (s *Session) SetName(name string) sessioncontract.Session {
return s
}

func (s *Session) setDriver(driver sessioncontract.Driver) {
s.driver = driver
}

func (s *Session) setJson(json foundation.Json) {
s.json = json
}

func (s *Session) Start() bool {
s.loadSession()

Expand Down Expand Up @@ -288,6 +280,14 @@ func (s *Session) reset() {
s.started = false
}

func (s *Session) setDriver(driver sessioncontract.Driver) {
s.driver = driver
}

func (s *Session) setJson(json foundation.Json) {
s.json = json
}

// toStringSlice converts an interface slice to a string slice.
func toStringSlice(anySlice []any) []string {
strSlice := make([]string, len(anySlice))
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion translation/file_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (f *FileLoaderTestSuite) SetupSuite() {
}

func (f *FileLoaderTestSuite) TearDownSuite() {
assert.Nil(f.T(), file.Remove("lang"))
f.Nil(file.Remove("lang"))
}

func (f *FileLoaderTestSuite) SetupTest() {
Expand Down

0 comments on commit bc59486

Please sign in to comment.