Skip to content

Golang package for creating a connection pool to MongoDB.

License

Notifications You must be signed in to change notification settings

podanypepa/connpool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

connpool

Golang package for creating a connection pool to MongoDB.

installation

go get github.com/podanypepa/connpool/mongodb

usage

package main

import (
	"context"
	"fmt"

	"github.com/podanypepa/connpool/mongodb"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

func main() {

	// create mongo connection pool with 20 independent connections
	cp, err := mongodb.Create("mongodb://localhost:27017", 20)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("connections pool length:", cp.Length())

	// get single connection from connection pool
	c, i := cp.GetRandom()
	fmt.Println("connection id:", i)

	// get data through geted connection
	getData(c, "test", "users")

	// close all created connections in pool
	cp.Destroy()
}

// getData is helper function for getting examples data from db
func getData(c *mongo.Client, db, coll string) {
	cur, err := c.
		Database(db).
		Collection(coll).
		Find(context.Background(), bson.D{})

	if err != nil {
		fmt.Println("ERR")
		fmt.Println(err)
	}
	defer cur.Close(context.Background())

	for cur.Next(context.Background()) {
		var result interface{}
		err := cur.Decode(&result)
		if err != nil {
			fmt.Println(err)
		}
		fmt.Println(result)
	}

}

About

Golang package for creating a connection pool to MongoDB.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages