connectpool

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

README

Connect Pool

Connect Pool is a Go package designed to efficiently manage a pool of connections. It abstracts the complexity of handling multiple concurrent connections, ensuring optimal resource utilization and simplifying the process of creating, using, and disposing of connections. This document outlines the Connect Pool's functionalities and provides a guide on how to use it in your Go applications.

Features

  • Dynamic Connection Management: Automatically creates and closes connections based on demand, up to a specified maximum pool size.
  • Connection Reuse: Allows for the reuse of idle connections, reducing the overhead of establishing new connections.
  • Concurrent Safe: Designed to be safe for concurrent use by multiple goroutines.
  • Auto-Clean: Periodically clears idle connections that exceed a specified maximum free time, helping to release unused resources.
  • Panic Handling: Provides a mechanism to handle panics that occur during connection operations, enhancing the robustness of your application.
  • Customizable Connection Logic: Supports custom connection creation, closing, and panic handling functions to fit specific requirements.

Usage

Below is a basic guide on how to integrate and use the Connect Pool in a Go application.

Installation

Ensure you have Go installed on your machine (visit Go's official site for installation instructions). Then, input the order on your Terminal:

$ go get -u github.com/HuXin0817/ConnectPool

import the Connect Pool package into your project:

import "github.com/HuXin0817/ConnectPool"
Creating a New Connection Pool

To create a new connection pool, you need to specify the maximum size of the pool and a function that defines how each connection is established:

pool := pool.NewConnectPool(100, mockConnectMethod)

mockConnectMethod is a function that returns a new connection object. This method is called whenever the pool needs to create a new connection.

Registering and Using a Connection

To use a connection from the pool:

conn, cancelFunc := pool.Register()
// Use the connection
cancelFunc() // Release the connection back to the pool
Setting Up Custom Handlers

You can set custom methods for handling connection closure and panic scenarios:

pool.SetCloseMethod(mockCloseFunc)
pool.SetDealPanicMethod(mockDealPanicMethod)
Configuring Auto-Clean Parameters

The pool can be configured to automatically clean up idle connections that exceed a certain duration:

pool.SetMaxFreeTime(30 * time.Second) // Set the maximum idle time before a connection is closed
pool.SetAutoClearInterval(1 * time.Minute) // Set the interval for the auto-clean process
Closing the Pool

To close the pool and release all its resources:

pool.Close()

Documentation

Index

Constants

View Source
const (
	DefaultMaxFreeTime       = 3 * time.Second // Default maximum idle wait time
	DefaultAutoCleanInterval = 2 * time.Second // Default auto-clean cycle execution
	DefaultCap               = 1000            // Default pool cap
)

Variables

View Source
var DefaultDealPanicMethod = func(panicInfo any) {
	log.Println(panicInfo)
}

Functions

func WithAutoClearInterval added in v1.0.3

func WithAutoClearInterval(autoClearInterval time.Duration) option

func WithCap added in v1.0.3

func WithCap(cap int) option

func WithCloseMethod added in v1.0.3

func WithCloseMethod(closeMethod func(connect any)) option

func WithDealPanicMethod added in v1.0.3

func WithDealPanicMethod(dealPanicMethod func(panicInfo any)) option

func WithMaxFreeTime added in v1.0.3

func WithMaxFreeTime(maxFreeTime time.Duration) option

Types

type ConnectPool

type ConnectPool interface {
	Register() (newConnect any, cancelFunc func())                                    // Registers a connection
	RegisterWithTimeLimit(deadLine time.Duration) (newConnect any, cancelFunc func()) // Registers a connection with a deadline
	WorkingNumber() int                                                               // Gets the number of active connections
	Size() int                                                                        // Gets the pool's cap
	Cap() int                                                                         // Gets the pool's maximum size
	MaxFreeTime() time.Duration                                                       // Gets the maximum idle time for connectors
	AutoClearInterval() time.Duration                                                 // Gets the interval for auto-clearing
	Close()                                                                           // Closes the pool
}

func NewConnectPool

func NewConnectPool(connectMethod func() any, options ...option) ConnectPool

NewConnectPool creates a new connection pool with a specified maximum size and connection creation method.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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