Documentation ¶
Index ¶
- Variables
- type HttpHeader
- type InMemory
- func (s *InMemory) AddSessionTTL(ctx context.Context, sID string, howMuch time.Duration) error
- func (s *InMemory) Close() error
- func (s *InMemory) DeleteAllRequests(ctx context.Context, sID string) error
- func (s *InMemory) DeleteRequest(ctx context.Context, sID, rID string) error
- func (s *InMemory) DeleteSession(ctx context.Context, sID string) error
- func (s *InMemory) GetAllRequests(ctx context.Context, sID string) (map[string]Request, error)
- func (s *InMemory) GetRequest(ctx context.Context, sID, rID string) (*Request, error)
- func (s *InMemory) GetSession(ctx context.Context, sID string) (*Session, error)
- func (s *InMemory) NewRequest(ctx context.Context, sID string, r Request) (rID string, _ error)
- func (s *InMemory) NewSession(ctx context.Context, session Session, id ...string) (sID string, _ error)
- type InMemoryOption
- type Redis
- func (s *Redis) AddSessionTTL(ctx context.Context, sID string, howMuch time.Duration) error
- func (s *Redis) DeleteAllRequests(ctx context.Context, sID string) error
- func (s *Redis) DeleteRequest(ctx context.Context, sID, rID string) error
- func (s *Redis) DeleteSession(ctx context.Context, sID string) error
- func (s *Redis) GetAllRequests(ctx context.Context, sID string) (map[string]Request, error)
- func (s *Redis) GetRequest(ctx context.Context, sID, rID string) (*Request, error)
- func (s *Redis) GetSession(ctx context.Context, sID string) (*Session, error)
- func (s *Redis) NewRequest(ctx context.Context, sID string, r Request) (rID string, _ error)
- func (s *Redis) NewSession(ctx context.Context, session Session, id ...string) (sID string, _ error)
- type RedisOption
- type Request
- type Session
- type Storage
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("not found") ErrSessionNotFound = fmt.Errorf("session %w", ErrNotFound) ErrRequestNotFound = fmt.Errorf("request %w", ErrNotFound) ErrClosed = errors.New("closed") )
Functions ¶
This section is empty.
Types ¶
type HttpHeader ¶
type InMemory ¶
type InMemory struct {
// contains filtered or unexported fields
}
func NewInMemory ¶
func NewInMemory(sessionTTL time.Duration, maxRequests uint32, opts ...InMemoryOption) *InMemory
NewInMemory creates a new in-memory storage with the given session TTL and the maximum number of stored requests. Note that the cleanup goroutine is started automatically if the cleanup interval is greater than zero. To stop the cleanup goroutine and close the storage, call the InMemory.Close method.
func (*InMemory) AddSessionTTL ¶
func (*InMemory) Close ¶
Close closes the storage and stops the cleanup goroutine. Any further calls to the storage methods will return ErrClosed.
func (*InMemory) DeleteAllRequests ¶
func (*InMemory) DeleteRequest ¶
func (*InMemory) DeleteSession ¶
func (*InMemory) GetAllRequests ¶
func (*InMemory) GetRequest ¶
func (*InMemory) GetSession ¶
func (*InMemory) NewRequest ¶
type InMemoryOption ¶
type InMemoryOption func(*InMemory)
func WithInMemoryCleanupInterval ¶
func WithInMemoryCleanupInterval(v time.Duration) InMemoryOption
WithInMemoryCleanupInterval sets the cleanup interval for expired sessions.
type Redis ¶
type Redis struct {
// contains filtered or unexported fields
}
func NewRedis ¶
func NewRedis(c redis.Cmdable, sTTL time.Duration, maxReq uint32, opts ...RedisOption) *Redis
NewRedis creates a new Redis storage. Notes:
- sTTL is the session TTL (redis accuracy is in milliseconds)
- maxReq is the maximum number of requests to store for the session
func (*Redis) AddSessionTTL ¶
func (*Redis) DeleteAllRequests ¶
func (*Redis) DeleteRequest ¶
func (*Redis) GetAllRequests ¶
func (*Redis) GetRequest ¶
func (*Redis) GetSession ¶
func (*Redis) NewRequest ¶
type RedisOption ¶
type RedisOption func(*Redis)
type Request ¶
type Request struct { ClientAddr string `json:"client_addr"` // client hostname or IP address Method string `json:"method"` // HTTP method name (i.e., 'GET', 'POST') Body []byte `json:"body"` // request body (payload) Headers []HttpHeader `json:"headers"` // HTTP request headers URL string `json:"url"` // Uniform Resource Identifier CreatedAtUnixMilli int64 `json:"created_at_unit_milli"` // creation time }
Request describes recorded request and additional meta-data.
type Session ¶
type Session struct { Code uint16 `json:"code"` // default server response code Headers []HttpHeader `json:"headers"` // server response headers ResponseBody []byte `json:"body"` // server response body (payload) Delay time.Duration `json:"delay"` // delay before response sending CreatedAtUnixMilli int64 `json:"created_at_unit_milli"` // creation time ExpiresAt time.Time `json:"-"` // expiration time (doesn't store in the storage) }
Session describes session settings (like response data and any additional information).
type Storage ¶
type Storage interface { // NewSession creates a new session and returns a session ID on success. // The Session.CreatedAt field will be set to the current time. NewSession(_ context.Context, _ Session, id ...string) (sID string, _ error) // GetSession retrieves session data. // If the session is not found, ErrSessionNotFound will be returned. GetSession(_ context.Context, sID string) (*Session, error) // AddSessionTTL adds the specified TTL to the session (and all its requests) with the specified ID. AddSessionTTL(_ context.Context, sID string, howMuch time.Duration) error // DeleteSession removes the session with the specified ID. // If the session is not found, ErrSessionNotFound will be returned. DeleteSession(_ context.Context, sID string) error // NewRequest creates a new request for the session with the specified ID and returns a request ID on success. // The session with the specified ID must exist. The Request.CreatedAtUnixMilli field will be set to the // current time. The storage may limit the number of requests per session - in this case the oldest request // will be removed. // If the session is not found, ErrSessionNotFound will be returned. NewRequest(_ context.Context, sID string, _ Request) (rID string, _ error) // GetRequest retrieves request data. // If the request or session is not found, ErrNotFound (ErrSessionNotFound or ErrRequestNotFound) will be returned. GetRequest(_ context.Context, sID, rID string) (*Request, error) // GetAllRequests returns all requests for the session with the specified ID. // If the session is not found, ErrSessionNotFound will be returned. If there are no requests, an empty map // will be returned. GetAllRequests(_ context.Context, sID string) (map[string]Request, error) // DeleteRequest removes the request with the specified ID. // If the request or session is not found, ErrNotFound (ErrSessionNotFound or ErrRequestNotFound) will be returned. DeleteRequest(_ context.Context, sID, rID string) error // DeleteAllRequests removes all requests for the session with the specified ID. // If the session is not found, ErrSessionNotFound will be returned. DeleteAllRequests(_ context.Context, sID string) error }
Storage manages Session and Request data.