adapto

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2024 License: MIT Imports: 3 Imported by: 0

README

adapto

adapto implements the adaptive timeout in Go.

With adapto, timeout values are dynamically adjusted similar to how additive-increase/multiplicative-decrease (AIMD) algorithm is used in TCP for congestion control.

Installation

go get github.com/hanapedia/adapto

Usage

package main

import (
    "context"
    "fmt"
    "time"

    "github.com/hanapedia/adapto"
)

func main() {
    config := adapto.Config{
        Id:             "example",              // Unique ID for the provider
        Interval:       5 * time.Second,        // Interval for resetting counts
        InitialTimeout: 2 * time.Second,        // Starting timeout duration
        Threshold:      0.5,                    // Ratio threshold for triggering adjustments
        IncBy:          1.5,                    // Multiplicative increase factor for timeouts
        DecBy:          200 * time.Millisecond, // Additive decrease amount for timeouts
		MinimumCount:   0,                      //Minimum number of generated timeouts to check the threshold
    }

    // GetTimeout returns the following
    // - adjusted timeout duration: time.Duration
    // - send only boolean channel to notify whether deadline was exceeded: chan<- bool
    // - err if there is error in config: error
    timeoutDuration, didDeadlineExceed, err := adapto.GetTimeout(config)
    // returns error when threshold is set greater or equal to 1 or negative
    // returns error when IncBy is smaller than or equal to 1
    if err != nil {
        panic(err)
    }

    ctx, cancel := context.WithTimeout(context.Background(), timeoutDuration)
    defer cancel()

    fmt.Printf("Iteration %d: Timeout duration: %v\n", i+1, timeoutDuration)

    // execute some code with context
    go SomeFunc(ctx)
    go func() {
        select {
        case <-ctx.Done():
            // Check if the context's deadline was exceeded
            if ctx.Err() == context.DeadlineExceeded {
                didDeadlineExceed <- true
            } else {
                didDeadlineExceed <- false
            }
        }
    }()
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AdaptoProviders map[string]*AdaptoProvider

Functions

func GetTimeout

func GetTimeout(config Config) (timeoutDuration time.Duration, didDeadlineExceed chan<- bool, err error)

Types

type AdaptoProvider

type AdaptoProvider struct {
	// contains filtered or unexported fields
}

func (*AdaptoProvider) NewTimeout

func (ap *AdaptoProvider) NewTimeout() (timeoutDuration time.Duration, didDeadlineExceed chan<- bool)

type Config

type Config struct {
	Id             string
	Interval       time.Duration
	InitialTimeout time.Duration
	Threshold      float32
	IncBy          float32
	DecBy          time.Duration
	MinimumCount   uint32
	Min            time.Duration
	Max            time.Duration
}

func (Config) Validate

func (c Config) Validate() error

type Counts

type Counts struct {
	// contains filtered or unexported fields
}

Counts holds the numbers of context with timeout set. adapto clears the internal Counts when new context is created by adapto and when context created by adapto exceeds deadline.

func (*Counts) DeadlineExceeded

func (c *Counts) DeadlineExceeded() uint32

func (*Counts) Ratio

func (c *Counts) Ratio() float32

func (*Counts) Total

func (c *Counts) Total() uint32

Jump to

Keyboard shortcuts

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