autohttp

package
v0.0.0-...-d05a697 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2025 License: MulanPSL-2.0 Imports: 12 Imported by: 0

README

autohttp

使用Go语言编写,对于HTTP请求进行发送,如果失败会暂存并重试。

使用

在运行模块之前,需要初始化相关配置信息,具体说明参见代码中的注释:

import (
    "gitee.com/gxsshallot/gotool/autohttp"
)

func main() {
    // 设置表名称
    autohttp.DefaultImplementTableName = "xxx"
    // 设置循环处理参数
    autohttp.LoopInterval = 3
    autohttp.AutoCleanEnable = true
    autohttp.AutoCleanLoopInterval = 3600
    autohttp.AutoCleanSuccessTime = 7 * 24 * 3600
    autohttp.AutoCleanFailureTime = 0
    autohttp.AutoCleanStoppedTime = 0
    autohttp.DefaultRetryType = true
    autohttp.RetryLadderDefaultInterval = []int64{}
    autohttp.RetryStepDefaultInterval = 60
    autohttp.RetryStepDefaultTime = 0
    autohttp.TaskAddIfSuccess = true
    // 设置回调
    autohttp.StatusCallback = xxx
    dbcb := autohttp.DefaultImplement{}
    dbcb.SetDB(db)
    autohttp.DatabaseCallback = dbcb
}
数据库

模块会自动管理数据库,需要在数据库中,新建一张表,至少应包含id字段,下表是默认实现的数据库表结构:

字段名 类型 是否主键 默认值 备注
id uint64 - 唯一标识ID
business_type string - 业务类型,用户自定义
business_id string - 业务主键,同一业务类型不能出现重复
url string - HTTP请求URL
method string - HTTP请求方法
header string - HTTP请求头
body string - HTTP请求Body
retry_type bool - 重试类型
retry_ladder_interval string - 阶梯性重试的间隔,使用逗号分隔
retry_step_interval int64 - 周期性重试的间隔
retry_step_time int64 - 周期性重试的次数
retry_count int64 - 重试计数
status uint8 - 状态,参见常量StatusXXX
request_time int64 - 首次发起请求时间,Unix时间戳,单位秒
last_retry_time int64 - 上一次重试的时间,Unix时间戳,单位秒
next_retry_time int64 - 下一次重试时间,Unix时间戳,单位秒
success_time int64 - 成功时间,Unix时间戳,单位秒

Documentation

Index

Constants

View Source
const (
	StatusSending uint8 = iota + 1 // 发送中
	StatusSuccess                  // 成功
	StatusFailure                  // 失败
	StatusStopped                  // 停止重试
)

传输状态

Variables

View Source
var AutoCleanEnable = false

是否开启自动清理,默认不开启。 如果开启自动清理,则会根据选项不同,自动删除已完成记录或者未完成记录。

View Source
var AutoCleanFailureTime = int64(0)

自动清理多久之前的失败记录,单位秒。 仅在AutoCleanEnable=true时有效。 大于0表示开启,等于0表示不开启。

View Source
var AutoCleanLoopInterval = int64(3600)

自动清理循环的轮询时间间隔,单位秒,默认1小时。

View Source
var AutoCleanStoppedTime = int64(0)

自动清理多久之前的已停止记录,单位秒。 仅在AutoCleanEnable=true时有效。 大于0表示开启,等于0表示不开启。

View Source
var AutoCleanSuccessTime = int64(0)

自动清理多久之前的成功记录,单位秒。 仅在AutoCleanEnable=true时有效。 大于0表示开启,等于0表示不开启。

View Source
var DefaultImplementTableName = "autohttp"

默认实现的表名称

View Source
var DefaultRetryType = true

默认重试类型。 true表示周期性重试,false表示阶梯性重试。

View Source
var LoopInterval = int64(3)

主循环多长时间检查一次,单位秒,默认3秒。 每次检查需要轮询数据库,所以需要根据实际情况调整。

View Source
var RetryLadderDefaultInterval = []int64{}

阶梯性重试默认间隔,单位秒,默认为空。 样例:[]int64{60, 5*60, 30*60, 1*3600},表示在1分钟、5分钟、30分钟、1小时后重试。

View Source
var RetryStepDefaultInterval = int64(60)

周期性重试默认间隔,单位秒,默认为60秒。

View Source
var RetryStepDefaultTime = int64(0)

周期性重试次数,默认为0,0表示不限制次数。

View Source
var TaskAddIfSuccess = false

成功后是否写入任务,默认不开启。

Functions

func Get

func Get(
	ctx context.Context,
	businessType string,
	businessId string,
	url string,
	header map[string]string,
) (statusCode int, rspBytes []byte, err error)

发送GET请求

func PostForm

func PostForm(
	ctx context.Context,
	businessType string,
	businessId string,
	apiUrl string,
	header map[string]string,
	body interface{},
) (statusCode int, rspBytes []byte, err error)

POST请求,application/x-www-form-urlencoded

func PostJson

func PostJson(
	ctx context.Context,
	businessType string,
	businessId string,
	url string,
	header map[string]string,
	body interface{},
) (statusCode int, rspBytes []byte, err error)

POST请求,application/json

Types

type DatabaseCallbackInterface

type DatabaseCallbackInterface interface {
	// 创建初始任务
	CreateTask(c context.Context, item *Item) (err error)
	// 删除任务
	DeleteTask(c context.Context, taskId uint64) (err error)
	// 查询待重试的任务(仅ID)
	IncompleteTaskIdList(c context.Context, currentTime int64) (list []uint64, err error)
	// 待清理任务列表,status表示状态,timestamp表示某个Unix时间点之前
	TaskToCleanList(c context.Context, status uint8, timestamp int64) (list []uint64, err error)
	// 查询任务详细信息
	TaskDetail(c context.Context, taskId uint64) (item Item, err error)
	// 查询任务详细信息
	TaskDetailByBusiness(c context.Context, businessType string, businessId string) (item Item, err error)
	// 更新任务
	UpdateTask(c context.Context, item *Item) (err error)
}

数据库操作类回调函数

var DatabaseCallback DatabaseCallbackInterface = nil

全局数据库回调方法

type DefaultImplement

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

默认实现对象

func (DefaultImplement) CreateTask

func (impl DefaultImplement) CreateTask(c context.Context, item *Item) (err error)

创建初始任务

func (DefaultImplement) DeleteTask

func (impl DefaultImplement) DeleteTask(c context.Context, taskId uint64) (err error)

删除任务

func (DefaultImplement) IncompleteTaskIdList

func (impl DefaultImplement) IncompleteTaskIdList(c context.Context, currentTime int64) (list []uint64, err error)

查询待重试的任务(仅ID)

func (*DefaultImplement) SetDB

func (impl *DefaultImplement) SetDB(db *gorm.DB) (err error)

初始化结构体

func (DefaultImplement) TaskDetail

func (impl DefaultImplement) TaskDetail(c context.Context, taskId uint64) (item Item, err error)

查询任务详细信息

func (DefaultImplement) TaskDetailByBusiness

func (impl DefaultImplement) TaskDetailByBusiness(c context.Context, businessType string, businessId string) (item Item, err error)

查询任务详细信息

func (DefaultImplement) TaskToCleanList

func (impl DefaultImplement) TaskToCleanList(c context.Context, status uint8, timestamp int64) (list []uint64, err error)

待清理任务列表,status表示状态,beforeSecond表示多少秒之前(使用RequestTime计算)的任务

func (DefaultImplement) UpdateTask

func (impl DefaultImplement) UpdateTask(c context.Context, item *Item) (err error)

更新任务

type DefaultImplementTable

type DefaultImplementTable struct {
	ID                  uint64 `gorm:"auto_increment:true;primary_key"` // 唯一标识ID
	BusinessType        string `gorm:"size:255;default:''"`             // 业务类型,用户自定义
	BusinessId          string `gorm:"size:255;default:''"`             // 业务主键,同一业务类型不能出现重复
	Url                 string `gorm:"size:255;default:''"`             // HTTP请求URL
	Method              string `gorm:"size:20;default:''"`              // HTTP请求方法
	Header              string `gorm:"type:text"`                       // HTTP请求头
	Body                string `gorm:"type:text"`                       // HTTP请求Body
	RetryType           bool   `gorm:"default:0"`                       // 重试类型
	RetryLadderInterval string `gorm:"size:255;default:''"`             // 阶梯性重试的间隔,使用逗号分隔
	RetryStepInterval   int64  `gorm:"default:0"`                       // 周期性重试的间隔
	RetryStepTime       int64  `gorm:"default:0"`                       // 周期性重试的次数
	RetryCount          int64  `gorm:"default:0"`                       // 重试计数
	Status              uint8  `gorm:"default:0"`                       // 状态,参见常量StatusXXX
	RequestTime         int64  `gorm:"default:0"`                       // 首次发起请求时间,Unix时间戳,单位秒
	LastRetryTime       int64  `gorm:"default:0"`                       // 上一次重试的时间,Unix时间戳,单位秒
	NextRetryTime       int64  `gorm:"default:0"`                       // 下一次重试时间,Unix时间戳,单位秒
	SuccessTime         int64  `gorm:"default:0"`                       // 成功时间,Unix时间戳,单位秒
}

默认实现的表结构

type Item

type Item struct {
	ID                  uint64            // 唯一标识ID
	BusinessType        string            // 业务类型,用户自定义
	BusinessId          string            // 业务主键,同一业务类型不能出现重复
	Url                 string            // HTTP请求URL
	Method              string            // HTTP请求方法
	Header              map[string]string // HTTP请求头
	Body                []byte            // HTTP请求Body
	RetryType           bool              // 重试类型
	RetryLadderInterval []int64           // 阶梯性重试的间隔
	RetryStepInterval   int64             // 周期性重试的间隔
	RetryStepTime       int64             // 周期性重试的次数
	RetryCount          int64             // 重试计数
	Status              uint8             // 状态,参见常量StatusXXX
	RequestTime         int64             // 首次发起请求时间,Unix时间戳,单位秒
	LastRetryTime       int64             // 上一次重试的时间,Unix时间戳,单位秒
	NextRetryTime       int64             // 下一次重试时间,Unix时间戳,单位秒
	SuccessTime         int64             // 成功时间,Unix时间戳,单位秒
}

数据库项的通用格式

type StatusCallbackInterface

type StatusCallbackInterface interface {
	// 如果要实现发送前修改Item,则复写此方法
	OnTaskBuilding(c context.Context, item *Item)
	// 结果判断
	OnTaskResultValid(c context.Context, item Item, statusCode int, resultBytes []byte) (err error)
	// 更新任务状态
	OnTaskStatusUpdate(c context.Context, item Item, errMessage string)
}

状态类的回调,需要用户自己实现

var StatusCallback StatusCallbackInterface = nil

全局状态类回调方法

Jump to

Keyboard shortcuts

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