callwrapper

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: MIT Imports: 8 Imported by: 0

README

Callwrapper

Callwrapper is a package that helps you to wrap your function with a middleware.

Features

  • Caching support. Cache the result of your function. This will help to improve latency. Help to reduce the load on your external service.
  • Customize Caching Client. You can customize the caching client. We have provide some example on pkg/cache.
  • Singleflight support. You can use singleflight to prevent multiple requests from hitting the same function.
  • Hook support. You can add hook to your function. This will help you to add some extra logic to your function.
    • Before Hook. This hook will be called before the function is called.
    • After Hook. This hook will be called after the function is called.
    • OnErrorLog. This hook will be called when the function returns an error.
    • OnWarnLog. This hook will be called when the function returns a warning.

Roadmap

  • Support Circuit Breaker. You can use circuit breaker to prevent your service to call external service when the external service is down.
  • Support Telemetry. Help to monitoring your external call like latency, error rate and QPS.

How to use

This is an example of how to use callwrapper. You can find the example in example/.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(name string, opt Options) error

New creates a new callwrapper.

Types

type CallWrapper

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

CallWrapper is the wrapper for the call function.

func (*CallWrapper) Call

func (cw *CallWrapper) Call(ctx context.Context, key map[string]interface{}, fn callFunc) (resp interface{}, err error)

Call executes the call function.

type Hook

type Hook struct {
	// Hook that is called when success execution
	BeforeHook func(ctx context.Context) map[string]interface{}

	// Hook that is called when success execution
	AfterHook func(ctx context.Context, param map[string]interface{})

	// OnErrorLog is the hook that is called when error occurs
	OnErrorLog func(ctx context.Context, msg string, err error)

	// OnWarnLog is the hook that is called when error occurs but still tolerable.
	OnWarnLog func(ctx context.Context, msg string, err error)
}

type Interface

type Interface interface {
	Call(ctx context.Context, key map[string]interface{}, fn callFunc) (resp interface{}, err error)
}

Interface is the interface for the callwrapper.

func GetCallWrapper

func GetCallWrapper(name string) Interface

GetCallWrapper returns the callwrapper by name. Be careful when using this function. If the callwrapper is not found, it will return nil.

type Options

type Options struct {
	// Toggle for singleflight. Default is false.
	// Singleflight help to prevent multiple call to the same function.
	Singleflight bool

	// CacheExpiration is the expiration time for the cache. Default is 5 minutes.
	CacheExpiration time.Duration

	// Cache wll improve perfomance. But if you need realtime data response, set ths value to false
	Cache bool

	// CacheClient is the client for the cache.
	CacheClient cache.Client

	// Hook is the configuration for the hook.
	Hook Hook
}

Options is the configuration for the callwrapper

Directories

Path Synopsis
example
basic command
pkg

Jump to

Keyboard shortcuts

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