Добавить сессии в Valkey и вход через Basic с Bearer-токеном
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// Package valkey connects to the Valkey session store.
|
||||
//
|
||||
// Timeouts are constants: every authenticated request reads a session, so
|
||||
// they bound how long a request can wait when the store is slow. Retries are
|
||||
// disabled for the same reason: a request that cannot reach the store fails
|
||||
// fast with 503 instead of waiting through a backoff.
|
||||
package valkey
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
dialTimeout = 2 * time.Second
|
||||
ioTimeout = time.Second
|
||||
poolSize = 10
|
||||
noRetries = -1
|
||||
)
|
||||
|
||||
// NewClient connects to url (redis:// scheme) and pings the server. Errors
|
||||
// never include the URL, which carries the password.
|
||||
func NewClient(ctx context.Context, url string) (*redis.Client, error) {
|
||||
opts, err := redis.ParseURL(url)
|
||||
if err != nil {
|
||||
return nil, errors.New("session store url is invalid")
|
||||
}
|
||||
opts.DialTimeout = dialTimeout
|
||||
opts.ReadTimeout = ioTimeout
|
||||
opts.WriteTimeout = ioTimeout
|
||||
opts.PoolSize = poolSize
|
||||
opts.MaxRetries = noRetries
|
||||
|
||||
client := redis.NewClient(opts)
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
_ = client.Close()
|
||||
return nil, fmt.Errorf("ping session store at %s: %w", opts.Addr, err)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
Reference in New Issue
Block a user