lorem_rate_limit

package
v0.0.0-...-d5bb415 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2017 License: GPL-3.0 Imports: 13 Imported by: 1

README

lorem-rate-limit

This is simple service module. Only for showing the micro service with HTTP and return json. The purpose for this service is only generating lorem ipsum paragraph and return the payload.

In this part I will demonstrate how to limit a request based on Token Bucket Limiter algorithm.

I am fully using all three functions from the golorem library.

Required libraries

go get github.com/go-kit/kit
go get github.com/drhodes/golorem
go get github.com/gorilla/mux
go get github.com/juju/ratelimit
service.go

Business logic will be put here

endpoint.go

Endpoint will be created here

transport.go

Handling about encode and decode json

logging.go

Logging function is under this file

instrument.go

Middleware function. For this sample, this function only for rate limiting only.

lorem-rate-limit.d

Go main function will be located under this folder. The dot d means daemon.

execute
cd $GOPATH/src/github.com/ru-rocker/gokit-playground
go run lorem-rate-limit/lorem-rate-limit.d/main.go

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrBadRouting is returned when an expected path variable is missing.
	ErrBadRouting = errors.New("inconsistent mapping between route and handler (programmer error)")
)
View Source
var ErrLimitExceed = errors.New("Rate Limit Exceed")
View Source
var (
	ErrRequestTypeNotFound = errors.New("Request type only valid for word, sentence and paragraph")
)

Functions

func MakeHttpHandler

func MakeHttpHandler(_ context.Context, endpoint Endpoints, logger log.Logger) http.Handler

Make Http Handler

func MakeLoremLoggingEndpoint

func MakeLoremLoggingEndpoint(svc Service) endpoint.Endpoint

creating Lorem Ipsum Endpoint

func NewTokenBucketLimiter

func NewTokenBucketLimiter(tb *ratelimit.Bucket) endpoint.Middleware

Types

type Endpoints

type Endpoints struct {
	LoremEndpoint endpoint.Endpoint
}

endpoints wrapper

type LoremRequest

type LoremRequest struct {
	RequestType string
	Min         int
	Max         int
}

request

type LoremResponse

type LoremResponse struct {
	Message string `json:"message"`
	Err     error  `json:"err,omitempty"`
}

response

type LoremService

type LoremService struct {
}

Implement service with empty struct

func (LoremService) Paragraph

func (LoremService) Paragraph(min, max int) string

func (LoremService) Sentence

func (LoremService) Sentence(min, max int) string

func (LoremService) Word

func (LoremService) Word(min, max int) string

Implement service functions

type Service

type Service interface {
	// generate a word with at least min letters and at most max letters.
	Word(min, max int) string

	// generate a sentence with at least min words and at most max words.
	Sentence(min, max int) string

	// generate a paragraph with at least min sentences and at most max sentences.
	Paragraph(min, max int) string
}

Define service interface

type ServiceMiddleware

type ServiceMiddleware func(Service) Service

create type that return function. this will be needed in main.go

func LoggingMiddleware

func LoggingMiddleware(logger log.Logger) ServiceMiddleware

implement function to return ServiceMiddleware

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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