GithubHelp home page GithubHelp logo

kfkp's Introduction

๐Ÿ“™ Introduction

Library kfkp implements a kafka producer pool, which is a high-performance Go package designed to efficiently manage Kafka producer connections.

๐Ÿ”๏ธ Features

  • Dynamic Scaling: Automatically manages the creation, acquisition, release, and destruction of Kafka producer connections, with support for dynamic scaling based on demand.

  • Synchronization Mechanisms: Utilizes synchronization mechanisms to handle connection contention and timeout scenarios, ensuring efficient and reliable connection management.

  • Configuration Options: Implements the Options pattern to configure pool parameters, allowing fine-tuning of the connection pool behavior according to application needs.

  • Timeout Queue Mechanism: Supports a timeout waiting queue mechanism to handle connection requests and timeouts effectively.

  • Connection Cleanup: Periodically cleans up long-unused connections to maintain optimal performance and resource utilization.

โš™๏ธ How to install

go get -u install "github.com/Sh-Fang/kfkp"

โœˆ๏ธ How to use

package main

import (
	"log"
	"strconv"

	"github.com/Sh-Fang/kfkp"
	"github.com/segmentio/kafka-go"
)

func main() {
	pool, err := kfkp.NewPool(
		kfkp.WithInitCapacity(10),
		kfkp.WithMaxCapacity(100),
		kfkp.WithMaxIdle(10),
		kfkp.WithBrokerAddress("localhost:9092"),
		kfkp.WithTopic("topic"),
		kfkp.WithRequiredAcks(kafka.RequireNone),
		kfkp.WithAsync(false),
	)
	if err != nil {
		log.Fatal(err)
	}

	pd, err := pool.GetConn()
	if err != nil {
		return
	}

	err = pd.SendMessage([]byte("123"), []byte(strconv.Itoa(111)))
	if err != nil {
		return
	}

	err = pool.PutConn(pd)
	if err != nil {
		return
	}

	err = pool.ClosePool()
	if err != nil {
		log.Fatal(err)
	}
}

Customize pool

You can invoke the NewPool method to instantiate a pool with options, as follows:

pool, err := kfkp.NewPool(
		kfkp.WithInitCapacity(10),
		kfkp.WithMaxCapacity(100),
		kfkp.WithMaxIdle(10),
		kfkp.WithBrokerAddress("localhost:9092"),
		kfkp.WithTopic("topic"),
		kfkp.WithRequiredAcks(kafka.RequireNone),
		kfkp.WithAsync(false),
	)

Get Connection

Connection can be getting from pool by calling pool.GetConn()

pd, err := pool.GetConn()

Put Connection

Connection can be putting back to pool by calling pool.PutConn(pd)

err = pool.PutConn(pd)

Close pool

You can close the connection pool and release all the resources.

err = pool.ClosePool()

๐Ÿ“„ License

The source code in kfkp is available under the MIT License.

๐Ÿ“š Relevant Projects

  • ants: A powerful and reliable pooling solution for Go.

kfkp's People

Contributors

sh-fang avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.