events

package
v2.0.5 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2021 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Event stream implementation.

You may get used to idea of passing many different interfaces to collect metrics, track some activity, emit logs etc. To stop bloating interfaces, httransform chooses another approach: it implements event stream.

Basic idea is dumb and simple: each time when something interesting is happening, httransform or users of the library send special events. These events can be processed and used to identify start/stop of the request processing, failures etc.

Actually, you can extend this framework and use your own values and process them in the same fashion.

import "github.com/github.com/9seconds/httransform/v2/events"

const (
    MyEvent      events.EventType = events.EventTypeUserBase + iota
    AnotherEvent
)

If you do that, I recommend you to keep these constants in a single module so you can easily control a uniqueness of values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommonErrorMeta

type CommonErrorMeta struct {
	// URI is requested URI.
	URI fasthttp.URI

	// Method defines an HTTP verb.
	Method string

	// Addr defines a remote address of the client.
	Addr net.Addr

	// Err is an underlying error.
	Err error
}

CommonErrorMeta defines a metadata related to some generic error produced by HTTP server.

func (*CommonErrorMeta) Error

func (c *CommonErrorMeta) Error() string

Error conforms error interface.

func (*CommonErrorMeta) Unwrap

func (c *CommonErrorMeta) Unwrap() error

Unwrap conforms go1.13 error interface.

type ErrorMeta

type ErrorMeta struct {
	// RequestID is unique identifier of the request.
	RequestID string

	// Err is an underlying error.
	Err error
}

ErrorMeta defines a metadata related to some logical error related to the request.

func (*ErrorMeta) Error

func (e *ErrorMeta) Error() string

Error conforms an error interface.

func (*ErrorMeta) Unwrap

func (e *ErrorMeta) Unwrap() error

Unwrap conforms go1.13 error interface.

type Event

type Event struct {
	// Type defines a type of the event.
	Type EventType

	// Time defines a time when this event was generated.
	Time time.Time

	// Value defines an attached value with additional information.
	Value interface{}
}

Event defines event information.

func (*Event) IsUser

func (e *Event) IsUser() bool

IsUser is a shortcut for evt.Type.IsUser.

func (*Event) String

func (e *Event) String() string

String conforms fmt.Stringer interface.

type EventType

type EventType byte

EventType is a unique identifier of the event type. There is a set of predefined events which are raised by httransform itself + users can define their own constants which are started from EventTypeUserBase.

const (
	// EventTypeNotSet defines an empty event. If you see this type
	// somewhere, it is probably a bug.
	EventTypeNotSet EventType = iota

	// EventTypeCommonError defines a common errors produced by HTTP
	// server: cannot read request, timeouts on reading/writing, client
	// disconnects.
	//
	// Corresponding value is CommonErrorMeta instance.
	EventTypeCommonError

	// EventTypeNewCertificate defines an event when new TLS certificate
	// is GENERATED.
	//
	// Corresponding value is hostname (string).
	EventTypeNewCertificate

	// EventTypeDropCertificate defines an event when we evict TLS
	// certificate by either TTL or cache size limitation.
	//
	// Corresponding value is hostname (string).
	EventTypeDropCertificate

	// EventTypeFailedAuth is generated if user can't be authorized
	// by auth.Interface implementation.
	//
	// Corresponding value is nil (have no idea what to put there, tbh).
	EventTypeFailedAuth

	// EventTypeStartRequest is generated when auth is completed and
	// we just started to process a request.
	//
	// Corresponding value is RequestMeta instance.
	EventTypeStartRequest

	// EventTypeFailedRequest is generated when request is failed
	// for some logical reason (timeout etc).
	//
	// Corresponding value is ErrorMeta instance.
	EventTypeFailedRequest

	// EventTypeFinishRequest is generated when request is finished
	// OK and as expected.
	//
	// Corresponding value is ResponseMeta instance.
	EventTypeFinishRequest

	// EventTypeTraffic is generated when we've collected all traffic
	// for the request. Please pay attention that it could be that
	// this event will arrive after EventTypeFinishRequest.
	//
	// Corresponding value is TrafficMeta instance.
	EventTypeTraffic

	// EventTypeUserBase defines a constant you should use
	// to define your own event types.
	EventTypeUserBase
)

func (EventType) IsUser

func (e EventType) IsUser() bool

IsUser returns if this event type is user one or predefined.

func (EventType) String

func (e EventType) String() string

String conforms fmt.Stringer interface.

type Processor

type Processor interface {
	// Process should process an incoming event.
	Process(Event)

	// Shutdown is executed when httransform is going to terminate this
	// processor. No events are going to be passed to this processor
	// once this function is executed.
	Shutdown()
}

Processor defines an interface for structs which process events. These structs are not passed in any function and it is guaranteed that messages with the same shardKey are going to be routed to the same instance of processor.

It is possible that httransform will create many instances. Each instance is going to work independently from each other.

func NoopProcessorFactory

func NoopProcessorFactory() Processor

NoopProcessorFactory returns a processor which does nothing and skips all events.

type ProcessorFactory

type ProcessorFactory func() Processor

ProcessorFactory defines how to generate new Processor instances.

type RequestMeta

type RequestMeta struct {
	// RequestID is unique identifier of the request.
	RequestID string

	// Method is HTTP verb of the request.
	Method string

	// URI is requested URI.
	URI fasthttp.URI

	// User defines a name of the user populated by the given
	// authenticator.
	User string

	// Addr defines an IP address of the user.
	Addr net.Addr

	// RequestType defines a set of characteristics related to that
	// request.
	RequestType RequestType
}

RequestMeta defines a metadata related to a request.

func (*RequestMeta) String

func (r *RequestMeta) String() string

String conforms fmt.Stringer interface.

type RequestType

type RequestType byte

RequestType defines a bitmask which corresponds to different characteristics of the event.

const (
	RequestTypeTunneled RequestType = 1 << iota
	RequestTypeTLS
	RequestTypeUpgraded
)

func (RequestType) IsTLS

func (r RequestType) IsTLS() bool

func (RequestType) IsTunneled

func (r RequestType) IsTunneled() bool

func (RequestType) IsUpgraded

func (r RequestType) IsUpgraded() bool

func (RequestType) String

func (r RequestType) String() string

String conforms fmt.Stringer interface.

type ResponseMeta

type ResponseMeta struct {
	// RequestID is unique identifier of the request.
	RequestID string

	// StatusCode is HTTP status code of the response.
	StatusCode int
}

ResponseMeta defines a metadata related to a response.

func (*ResponseMeta) String

func (r *ResponseMeta) String() string

String conforms fmt.Stringer interface.

type Stream

type Stream interface {
	// Send sends EventType and interface to the stream respecting a
	// given interface and sharding key.
	Send(context.Context, EventType, interface{}, string)
}

Stream defines an interface to event stream.

func NewStream

func NewStream(ctx context.Context, factory ProcessorFactory) Stream

NewStream creates, initialized and returns a new ready Stream instance. It spawns a set of worker goroutines under the hood. Each goroutine corresponds to a its own processor instance (that's why you pass factory here). Processor is initialized within a goroutine.

type TrafficMeta

type TrafficMeta struct {
	// ID a unique identifier of the TrafficConn. Usually it is the same
	// as httransform ones but it is possible to pass your own id here
	// if you use TrafficConn somewhere else.
	ID string

	// Addr defines a 'netloc' IP address which was used.
	Addr net.Addr

	// ReadBytes defines how many bytes were read from the netloc.
	ReadBytes uint64

	// WrittenBytes defines how many bytes were written to the netloc.
	WrittenBytes uint64
}

TrafficMeta defines a metadata related to a TrafficConn completed work.

func (*TrafficMeta) String

func (t *TrafficMeta) String() string

String conforms fmt.Stringer interface.

Jump to

Keyboard shortcuts

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