mirror of
https://github.com/tgdrive/teldrive.git
synced 2025-10-03 03:44:59 +08:00
chore: Add mutex for session storage in kv package
This commit is contained in:
parent
e786597e94
commit
c500e0f8e0
1 changed files with 6 additions and 0 deletions
|
@ -3,6 +3,7 @@ package kv
|
|||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"sync"
|
||||
|
||||
"github.com/gotd/td/telegram"
|
||||
)
|
||||
|
@ -10,6 +11,7 @@ import (
|
|||
type Session struct {
|
||||
kv KV
|
||||
key string
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func NewSession(kv KV, key string) telegram.SessionStorage {
|
||||
|
@ -17,6 +19,8 @@ func NewSession(kv KV, key string) telegram.SessionStorage {
|
|||
}
|
||||
|
||||
func (s *Session) LoadSession(_ context.Context) ([]byte, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
b, err := s.kv.Get(s.key)
|
||||
if err != nil {
|
||||
|
@ -29,5 +33,7 @@ func (s *Session) LoadSession(_ context.Context) ([]byte, error) {
|
|||
}
|
||||
|
||||
func (s *Session) StoreSession(_ context.Context, data []byte) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
return s.kv.Set(s.key, data)
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue