Перенесите реализацию ЛР 4 на стенд ЛР 5
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
// 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. Command
|
||||
// retries are disabled and a connection is dialled once for the same reason:
|
||||
// a request that cannot reach the store fails fast with 503 instead of
|
||||
// waiting through a backoff. On the compose stand Docker DNS takes seconds
|
||||
// to give up on a stopped container, so one dial attempt is already bounded
|
||||
// only by dialTimeout.
|
||||
package valkey
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
dialTimeout = time.Second
|
||||
ioTimeout = time.Second
|
||||
poolSize = 10
|
||||
noRetries = -1
|
||||
singleAttempt = 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
|
||||
opts.DialerRetries = singleAttempt
|
||||
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package valkey_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/valkey"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
func TestNewClientPingsAndBoundsWaiting(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
kv, err := testenv.StartValkey(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer kv.Close()
|
||||
|
||||
client, err := valkey.NewClient(ctx, kv.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
opts := client.Options()
|
||||
if opts.DialTimeout != time.Second || opts.ReadTimeout != time.Second || opts.WriteTimeout != time.Second {
|
||||
t.Fatalf("timeouts = %v/%v/%v, want 1s each", opts.DialTimeout, opts.ReadTimeout, opts.WriteTimeout)
|
||||
}
|
||||
// go-redis dials up to five times by default; with Docker DNS taking
|
||||
// seconds to give up on a stopped container that alone would exceed the
|
||||
// request deadline.
|
||||
if opts.DialerRetries != 1 {
|
||||
t.Fatalf("DialerRetries = %d, want a single dial attempt", opts.DialerRetries)
|
||||
}
|
||||
// go-redis stores the requested -1 ("no retries") as 0 after init; its
|
||||
// default would be 3.
|
||||
if opts.PoolSize != 10 || opts.MaxRetries != 0 {
|
||||
t.Fatalf("PoolSize = %d, MaxRetries = %d; want 10 and 0 (fail fast)", opts.PoolSize, opts.MaxRetries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewClientErrorsNeverEchoTheURL(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for name, url := range map[string]string{
|
||||
"malformed": "redis://:s3cret@host:port/x",
|
||||
"unreachable": "redis://:s3cret@127.0.0.1:1/0",
|
||||
} {
|
||||
_, err := valkey.NewClient(ctx, url)
|
||||
if err == nil {
|
||||
t.Fatalf("%s: want error", name)
|
||||
}
|
||||
if strings.Contains(err.Error(), "s3cret") {
|
||||
t.Fatalf("%s: error leaks the password: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user