Enhance the durability of your API by automatically recovering from connectivity failure
Go
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.gitattributes
.gitignore :octocat: Added .gitattributes & .gitignore files Oct 9, 2015
.travis.yml
README.md
connectionbuilder.go
fallback.go
fallback_test.go
license Create license Oct 9, 2015

README.md

Image of insidethecpu

Go Fallback Package

Join the chat at https://gitter.im/daishisystems/month Build Status godoc

Package fallback enhances the durability of your API by automatically recovering from connectivity failure. It achieves this by providing an enhanced degree of redundancy to HTTP requests, introducing a Chain of Responsibility, consisting of a series of fallback HTTP requests designed to augment an initial HTTP request. Should the initial HTTP request fail, the next fallback HTTP request in the chain will execute.

Any number of fallback HTTP requests can be chained sequentially. Redundancy is achieved by executing each fallback HTTP request in a recursive manner until one of the requests succeeds, or all requests fail.

Icon

Installation

go get github.com/daishisystems/fallback

Sample Code

package main

import (
	"fmt"
	"github.com/daishisystems/fallback"
)

func main() {

	// BasicResponse represents the response issued from the first successful
	// HTTP request, if applicable.
	type BasicResponse struct {
		Text   string
		Detail string
	}

	// BasicError represents the error issued from the last unsuccessful
	// HTTP request, if applicable.
	type BasicError struct {
		Code    int
		Message string
	}

	//
	type PostBody struct {
		Name   string
		Amount int
	}

	basicResponse := &BasicResponse{}
	basicError := &BasicError{}

	postBody := PostBody{
		"Random", 100,
	}

	passPath := "http://demo7227109.mockable.io/get-basic"
	failPath2 := "http://demo7227109.mockable.io/fail-basic"
	failPath1 := "http://demo7227109.mockable.io/fail-basic-post"

	connectionManager := fallback.ConnectionManager{}

	// This Connection will execute last, and will succeed.
	passBuilder := fallback.NewConnectionBuilder("PASS", "GET", passPath, true,
		nil, nil, &basicResponse, &basicError, nil)
	connectionManager.CreateConnection(passBuilder)

	// This Connection will be the 2nd Connection to execute, and will fail.
	failBuilder2 := fallback.NewConnectionBuilder("FAIL2", "POST", failPath2,
		true, nil, nil, &basicResponse, &basicError, passBuilder.Connection)
	connectionManager.CreateConnection(failBuilder2)

	//This Connection will be the 1st Connection to execute, and will fail.
	failBuilder1 := fallback.NewConnectionBuilder("FAIL1", "POST", failPath1,
		true, postBody, nil, &basicResponse, &basicError,
		failBuilder2.Connection)
	connectionManager.CreateConnection(failBuilder1)

	// Each Connection will be invoked in a recursive manner until a
	// Connection succeeds, or all Connections fail. Please refer to the Chain
	// of Responsibility design for more information.
	statusCode, err := failBuilder1.Connection.ExecuteHTTPRequest()
	if err != nil {
		panic(err)
	}

	fmt.Printf("HTTP status code: %d\n", statusCode)
	fmt.Printf("Text: %s\n", basicResponse.Text)
	fmt.Printf("Detail: %s", basicResponse.Detail)
}

Contact the Developer

Please reach out and contact me for questions, suggestions, or to just talk tech in general.

RSSTwitterLinkedInGoogle+YouTube