reader

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2017 License: Apache-2.0 Imports: 4 Imported by: 10

Documentation

Overview

Package reader contains logic for reading from a provider. Any types that implements the DataReader interface can be used in this system. The Result should provide a byte slice that is JSON unmarshallable, otherwise the data will be rejected.

Important Notes

When the token's context is cancelled, the reader should finish its job and return. The Time should be set when the data is read from the endpoint, otherwise it will lose its meaning. The engine will issue jobs based on the Interval, which is set in the configuration file.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrEmptyName is the error when the package name is empty.
	ErrEmptyName = fmt.Errorf("name cannot be empty")

	// ErrEmptyEndpoint is the error when the given endpoint is empty.
	ErrEmptyEndpoint = fmt.Errorf("endpoint cannot be empty")

	// ErrEmptyTypeName is the error when the type_name is an empty string.
	ErrEmptyTypeName = fmt.Errorf("type_name cannot be empty")

	// ErrBackoffExceeded is the error when the endpoint's absence has
	// exceeded the backoff value. It is not strictly an error, it is
	// however a pointer to an error in the past.
	ErrBackoffExceeded = fmt.Errorf("endpoint gone too long")

	// ErrPingNotCalled is the error if the caller calls the record without pinging.
	ErrPingNotCalled = fmt.Errorf("the caller forgot to ask me pinging")
)

Functions

This section is empty.

Types

type DataReader added in v0.1.2

type DataReader interface {
	// Name should return the representation string for this reader.
	// Choose a very simple and unique name.
	Name() string

	// Ping should ping the endpoint and return nil if was successful.
	// The Engine will not launch the reader if the ping result is an error.
	Ping() error

	// When the context is timed-out or cancelled, the reader should return.
	Read(*token.Context) (*Result, error)

	// Mapper should return an instance of the datatype mapper.
	// Engine uses this object to present the data to recorders.
	Mapper() datatype.Mapper

	// TypeName is usually the application name and is set by the user in
	// the configuration file.
	TypeName() string

	// Timeout is required by the Engine so it can read the time-outs.
	Timeout() time.Duration

	// Interval is required by the Engine so it can read the intervals.
	Interval() time.Duration
}

DataReader receives job requests to read from the target. It returns an error if the data cannot be read or the connection is refused.

Notes

Readers should not intercept the engine's decision on the TypeName, unless they have a valid reason.

Example (Read)

This example shows the reader hits the endpoint when the Read method is called.

package main

import (
	"context"
	"fmt"
	"io"
	"net/http"
	"net/http/httptest"

	"github.com/arsham/expvastic/internal/token"
	reader "github.com/arsham/expvastic/reader/testing"
)

func main() {

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		io.WriteString(w, `{"the key": "is the value!"}`)
	}))
	defer ts.Close()

	red := reader.GetReader(ts.URL) // this is a mocked version, but the example's principals stays the same.
	err := red.Ping()
	fmt.Println("Ping errors:", err)

	job := token.New(context.Background())
	res, err := red.Read(job) // Issuing a job

	if err == nil { // Lets check the errors
		fmt.Println("No errors reported")
	}

	fmt.Println("Result is:", string(res.Content))

}
Output:

Ping errors: <nil>
No errors reported
Result is: {"the key": "is the value!"}

type ErrEndpointNotAvailable added in v0.5.0

type ErrEndpointNotAvailable struct {
	Endpoint string
	Err      error
}

ErrEndpointNotAvailable is the error when the endpoint is not available.

func (ErrEndpointNotAvailable) EndpointNotAvailable added in v0.5.0

func (ErrEndpointNotAvailable) EndpointNotAvailable()

EndpointNotAvailable defines the behaviour of the error

func (ErrEndpointNotAvailable) Error added in v0.5.0

func (e ErrEndpointNotAvailable) Error() string

type ErrInvalidEndpoint added in v0.5.0

type ErrInvalidEndpoint string

ErrInvalidEndpoint is the error when the endpoint is not a valid url

func (ErrInvalidEndpoint) Error added in v0.5.0

func (e ErrInvalidEndpoint) Error() string

func (ErrInvalidEndpoint) InvalidEndpoint added in v0.5.0

func (ErrInvalidEndpoint) InvalidEndpoint()

InvalidEndpoint defines the behaviour of the error

type ErrLowBackoffValue added in v0.6.0

type ErrLowBackoffValue int64

ErrLowBackoffValue is the error when the endpoint is not a valid url

func (ErrLowBackoffValue) Error added in v0.6.0

func (e ErrLowBackoffValue) Error() string

func (ErrLowBackoffValue) LowBackoffValue added in v0.6.0

func (ErrLowBackoffValue) LowBackoffValue()

LowBackoffValue defines the behaviour of the error

type Result added in v0.7.0

type Result struct {
	// ID is the job ID given by the Engine.
	// This ID should not be changed until it is recorded.
	ID token.ID

	//Time is set after the request was successfully read.
	Time time.Time

	// TypeName comes from the configuration, but the Engine might decide
	// to change it.
	TypeName string

	// Content should be json unmarshallable, otherwise the job will be dropped.
	Content []byte

	// Mapper is the mapper set in the reader.
	Mapper datatype.Mapper
}

Result is constructed every time a new data is fetched.

Directories

Path Synopsis
Package expvar contains logic to read from an expvar provide.
Package expvar contains logic to read from an expvar provide.
Package self contains codes for recording expvastic's own metrics.
Package self contains codes for recording expvastic's own metrics.
Package testing is a test suit for readers.
Package testing is a test suit for readers.

Jump to

Keyboard shortcuts

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