conn

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2026 License: Apache-2.0 Imports: 12 Imported by: 3

Documentation

Overview

Package conn contains connection helpers for the most common outbound dependencies used by gomicro services.

The package currently includes:

- MySQL connection helpers for database/sql and GORM. - gRPC dial helpers with wait-for-ready behavior and resolver prefix handling.

The exported constructors validate their inputs so configuration problems fail fast instead of surfacing later as confusing connection errors.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func DialGrpcService

func DialGrpcService(ctx context.Context, opt *GrpcDialOptions) (*grpc.ClientConn, error)

DialGrpcService dials a gRPC service using gomicro defaults plus any caller-provided options.

Example
ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err := DialGrpcService(ctx, &GrpcDialOptions{
	Address: "localhost:9090",
	DialOptions: []grpc.DialOption{
		grpc.WithTransportCredentials(insecure.NewCredentials()),
	},
})
fmt.Println(err != nil)
Output:
true

func OpenGorm

func OpenGorm(opt *DbOptions) (*gorm.DB, error)

OpenGorm opens a GORM connection using the configured MySQL settings.

func OpenSql

func OpenSql(opt *DbOptions) (*sql.DB, error)

OpenSql opens a database/sql connection using the configured MySQL settings.

Example
_, err := OpenSql(&DbOptions{
	Name:    "users",
	Address: "127.0.0.1:3306",
	User:    "root",
	Schema:  "users",
	ConnPool: &DbPoolSettings{
		MaxIdleConns: 5,
		MaxOpenConns: 20,
	},
})
fmt.Println(err == nil)
Output:
true

Types

type DbOptions

type DbOptions struct {
	Name     string
	Dialect  string
	Address  string
	User     string
	Password string
	Schema   string
	ConnPool *DbPoolSettings
}

DbOptions contains parameters for connecting to a SQL database. Only MySQL is currently supported.

type DbPoolSettings

type DbPoolSettings struct {
	MaxIdleConns    uint
	MaxOpenConns    uint
	MaxLifetime     time.Duration
	MaxIdleLifetime time.Duration
}

DbPoolSettings controls SQL connection pool sizing and lifetimes.

type GrpcDialOptions

type GrpcDialOptions struct {
	ServiceName string
	// Address is the host:port pair or resolver target to dial.
	Address string
	// DialOptions are appended after gomicro's default wait-for-ready and balancing options.
	DialOptions []grpc.DialOption
	// K8Service switches the resolver prefix to dns:/// for Kubernetes service discovery.
	K8Service bool
}

GrpcDialOptions controls how DialGrpcService connects to a gRPC service.

Jump to

Keyboard shortcuts

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