storage

package
v2.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 13, 2024 License: MIT Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 FS

type FS struct {
	// contains filtered or unexported fields
}

FS is an implementation of the Storage interface that organizes data on the filesystem using the following structure:

📂 {root}
├── 📂 {session-uuid}
│   ├── 📄 session.<expiration-time-unix-millis>.json
│   ├── 📄 request.<created-time-unix-millis>.{request-uuid}.json
│   └── …
└── …

func NewFS

func NewFS(root string, sessionTTL time.Duration, maxRequests uint32, opts ...FSOption) *FS

func (*FS) AddSessionTTL

func (s *FS) AddSessionTTL(ctx context.Context, sID string, howMuch time.Duration) error

func (*FS) Close

func (s *FS) Close() error

func (*FS) DeleteAllRequests

func (s *FS) DeleteAllRequests(ctx context.Context, sID string) error

func (*FS) DeleteRequest

func (s *FS) DeleteRequest(ctx context.Context, sID, rID string) error

func (*FS) DeleteSession

func (s *FS) DeleteSession(ctx context.Context, sID string) error

func (*FS) GetAllRequests

func (s *FS) GetAllRequests(ctx context.Context, sID string) (map[string]Request, error)

func (*FS) GetRequest

func (s *FS) GetRequest(ctx context.Context, sID, rID string) (*Request, error)

func (*FS) GetSession

func (s *FS) GetSession(ctx context.Context, sID string) (*Session, error)

func (*FS) NewRequest

func (s *FS) NewRequest(ctx context.Context, sID string, r Request) (rID string, _ error)

func (*FS) NewSession

func (s *FS) NewSession(ctx context.Context, session Session, id ...string) (sID string, _ error)

type FSOption

type FSOption func(*FS)

func WithFSCleanupInterval

func WithFSCleanupInterval(v time.Duration) FSOption

func WithFSTimeNow

func WithFSTimeNow(fn TimeFunc) FSOption

type HttpHeader

type HttpHeader struct {
	Name  string `json:"name"`  // the name of the header, e.g. "Content-Type"
	Value string `json:"value"` // the value of the header, e.g. "application/json"
}

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 (s *InMemory) AddSessionTTL(ctx context.Context, sID string, howMuch time.Duration) error

func (*InMemory) Close

func (s *InMemory) Close() error

Close closes the storage and stops the cleanup goroutine. Any further calls to the storage methods will return ErrClosed.

func (*InMemory) DeleteAllRequests

func (s *InMemory) DeleteAllRequests(ctx context.Context, sID string) error

func (*InMemory) DeleteRequest

func (s *InMemory) DeleteRequest(ctx context.Context, sID, rID string) error

func (*InMemory) DeleteSession

func (s *InMemory) DeleteSession(ctx context.Context, sID string) error

func (*InMemory) GetAllRequests

func (s *InMemory) GetAllRequests(ctx context.Context, sID string) (map[string]Request, error)

func (*InMemory) GetRequest

func (s *InMemory) GetRequest(ctx context.Context, sID, rID string) (*Request, error)

func (*InMemory) GetSession

func (s *InMemory) GetSession(ctx context.Context, sID string) (*Session, error)

func (*InMemory) NewRequest

func (s *InMemory) NewRequest(ctx context.Context, sID string, r Request) (rID string, _ error)

func (*InMemory) NewSession

func (s *InMemory) NewSession(ctx context.Context, session Session, id ...string) (sID string, _ error)

type InMemoryOption

type InMemoryOption func(*InMemory)

func WithInMemoryCleanupInterval

func WithInMemoryCleanupInterval(v time.Duration) InMemoryOption

WithInMemoryCleanupInterval sets the cleanup interval for expired sessions.

func WithInMemoryTimeNow

func WithInMemoryTimeNow(fn TimeFunc) InMemoryOption

WithInMemoryTimeNow sets the function that returns the current time.

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 (s *Redis) AddSessionTTL(ctx context.Context, sID string, howMuch time.Duration) error

func (*Redis) DeleteAllRequests

func (s *Redis) DeleteAllRequests(ctx context.Context, sID string) error

func (*Redis) DeleteRequest

func (s *Redis) DeleteRequest(ctx context.Context, sID, rID string) error

func (*Redis) DeleteSession

func (s *Redis) DeleteSession(ctx context.Context, sID string) error

func (*Redis) GetAllRequests

func (s *Redis) GetAllRequests(ctx context.Context, sID string) (map[string]Request, error)

func (*Redis) GetRequest

func (s *Redis) GetRequest(ctx context.Context, sID, rID string) (*Request, error)

func (*Redis) GetSession

func (s *Redis) GetSession(ctx context.Context, sID string) (*Session, error)

func (*Redis) NewRequest

func (s *Redis) NewRequest(ctx context.Context, sID string, r Request) (rID string, _ error)

func (*Redis) NewSession

func (s *Redis) NewSession(ctx context.Context, session Session, id ...string) (sID string, _ error)

type RedisOption

type RedisOption func(*Redis)

func WithRedisTimeNow

func WithRedisTimeNow(fn TimeFunc) RedisOption

WithRedisTimeNow sets the function that returns the current time.

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
}

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.

type TimeFunc

type TimeFunc func() time.Time

TimeFunc is a function that returns the current time.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL