reader

package
v0.8.2 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2017 License: Apache-2.0 Imports: 6 Imported by: 0

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

func SetBackoff added in v0.8.1

func SetBackoff(backoff int) func(Constructor) error

SetBackoff sets the backoff of the reader

func SetEndpoint added in v0.8.1

func SetEndpoint(endpoint string) func(Constructor) error

SetEndpoint sets the endpoint of the reader

func SetInterval added in v0.8.1

func SetInterval(interval time.Duration) func(Constructor) error

SetInterval sets the interval of the reader

func SetLogger added in v0.8.1

func SetLogger(log internal.FieldLogger) func(Constructor) error

SetLogger sets the log of the reader

func SetMapper added in v0.8.1

func SetMapper(mapper datatype.Mapper) func(Constructor) error

SetMapper sets the mapper of the reader

func SetName added in v0.8.1

func SetName(name string) func(Constructor) error

SetName sets the name of the reader

func SetTimeout added in v0.8.1

func SetTimeout(timeout time.Duration) func(Constructor) error

SetTimeout sets the timeout of the reader

func SetTypeName added in v0.8.1

func SetTypeName(typeName string) func(Constructor) error

SetTypeName sets the typeName of the reader

Types

type Constructor added in v0.8.1

type Constructor interface {
	// SetLogger is for setting the Logger
	SetLogger(logger internal.FieldLogger)

	// SetName is for setting the Name
	SetName(name string)

	// SetTypeName is for setting the TypeName
	SetTypeName(typeName string)

	// SetEndpoint is for setting the Endpoint
	SetEndpoint(endpoint string)

	// SetMapper is for setting the SetMapper
	SetMapper(mapper datatype.Mapper)

	// SetInterval is for setting the Interval
	SetInterval(interval time.Duration)

	// SetTimeout is for setting the Timeout
	SetTimeout(timeout time.Duration)

	// SetBackoff is for setting the Backoff
	SetBackoff(backoff int)
}

Constructor is an interface for setting up an object for testing.

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/expipe/internal/token"
	reader "github.com/arsham/expipe/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) 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

type ErrLowBackoffValue added in v0.6.0

type ErrLowBackoffValue int64

ErrLowBackoffValue is the error when the backoff value is lower than 5

func (ErrLowBackoffValue) Error added in v0.6.0

func (e ErrLowBackoffValue) Error() string

type ErrLowInterval added in v0.8.1

type ErrLowInterval time.Duration

ErrLowInterval is the error when the interval is zero

func (ErrLowInterval) Error added in v0.8.1

func (e ErrLowInterval) Error() string

type ErrLowTimeout added in v0.8.1

type ErrLowTimeout time.Duration

ErrLowTimeout is the error when the interval is zero

func (ErrLowTimeout) Error added in v0.8.1

func (e ErrLowTimeout) Error() string

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 expipe's own metrics.
Package self contains codes for recording expipe'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