Skip to content

Commit

Permalink
Merge pull request #35 from kumparan/feature/mysql-connector
Browse files Browse the repository at this point in the history
feature: add mysql connector and up go version to 1.22
  • Loading branch information
fajrifernanda authored Jul 8, 2024
2 parents ea3c80c + 992ba1f commit b12f8e6
Show file tree
Hide file tree
Showing 9 changed files with 252 additions and 700 deletions.
17 changes: 15 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,24 @@
# go-connect

<a name="v1.18.0"></a>
## [v1.18.0] - 2024-07-08
### Fixes
- create proper ping ([#36](https://github.com/kumparan/go-connect/issues/36))

### New Features
- add mysql connector

### Other Improvements
- upgrade go to 1.22


<a name="v1.17.0"></a>
## [v1.17.0] - 2023-12-22
### Fixes
- use applied http conn options which is not nullable ([#33](https://github.com/kumparan/go-connect/issues/33))

### Other Improvements
- upgrade dependencies
- upgrade dependencies ([#34](https://github.com/kumparan/go-connect/issues/34))


<a name="v1.16.1"></a>
Expand Down Expand Up @@ -213,7 +225,8 @@
- init go-connect with http and redis connector ([#1](https://github.com/kumparan/go-connect/issues/1))


[Unreleased]: https://github.com/kumparan/go-connect/compare/v1.17.0...HEAD
[Unreleased]: https://github.com/kumparan/go-connect/compare/v1.18.0...HEAD
[v1.18.0]: https://github.com/kumparan/go-connect/compare/v1.17.0...v1.18.0
[v1.17.0]: https://github.com/kumparan/go-connect/compare/v1.16.1...v1.17.0
[v1.16.1]: https://github.com/kumparan/go-connect/compare/v1.16.0...v1.16.1
[v1.16.0]: https://github.com/kumparan/go-connect/compare/v1.15.1...v1.16.0
Expand Down
36 changes: 27 additions & 9 deletions cockroachdb_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"regexp"
"time"

"github.com/imdario/mergo"

"github.com/jpillora/backoff"
log "github.com/sirupsen/logrus"
"github.com/uptrace/opentelemetry-go-extra/otelgorm"
Expand All @@ -24,6 +26,7 @@ type CockroachDBConnectionOptions struct {
ConnMaxLifetime time.Duration
LogLevel string
UseOpenTelemetry bool
PingTimeout time.Duration
}

var (
Expand All @@ -42,6 +45,7 @@ var (
ConnMaxLifetime: 1 * time.Hour,
LogLevel: "info",
UseOpenTelemetry: false,
PingTimeout: 5 * time.Second,
}
)

Expand Down Expand Up @@ -81,9 +85,18 @@ func checkConnection(databaseDSN string, options *CockroachDBConnectionOptions,
ticker.Stop()
return
case <-ticker.C:
if _, err := CockroachDB.DB(); err != nil {
db, err := CockroachDB.DB()
if err != nil {
reconnectCockroachConn(databaseDSN, options)
continue
}

ctx, cancelFunc := context.WithTimeout(context.TODO(), options.PingTimeout)
if err = db.PingContext(ctx); err != nil {
log.Errorf("ping to db got err : %v", err)
reconnectCockroachConn(databaseDSN, options)
}
cancelFunc()
}
}
}
Expand All @@ -97,13 +110,15 @@ func reconnectCockroachConn(databaseDSN string, options *CockroachDBConnectionOp
}

for b.Attempt() < float64(options.RetryAttempts) {
log.Info("reconnecting to db")
conn, err := openCockroachConn(databaseDSN, options)
if err != nil {
log.WithField("databaseDSN", databaseDSN).Error("failed to connect cockroach database: ", err)
}

if conn != nil {
CockroachDB = conn
log.Info("db connected")
*CockroachDB = *conn
break
}
time.Sleep(b.Duration())
Expand Down Expand Up @@ -160,29 +175,29 @@ func (g *GormCustomLogger) LogMode(level gormLogger.LogLevel) gormLogger.Interfa
}

// Info :nodoc:
func (g *GormCustomLogger) Info(ctx context.Context, message string, values ...interface{}) {
func (g *GormCustomLogger) Info(_ context.Context, message string, values ...interface{}) {
if g.LogLevel >= gormLogger.Info {
log.WithFields(log.Fields{"data": values}).Info(message)
}
}

// Warn :nodoc:
func (g *GormCustomLogger) Warn(ctx context.Context, message string, values ...interface{}) {
func (g *GormCustomLogger) Warn(_ context.Context, message string, values ...interface{}) {
if g.LogLevel >= gormLogger.Warn {
log.WithFields(log.Fields{"data": values}).Warn(message)
}

}

// Error :nodoc:
func (g *GormCustomLogger) Error(ctx context.Context, message string, values ...interface{}) {
func (g *GormCustomLogger) Error(_ context.Context, message string, values ...interface{}) {
if g.LogLevel >= gormLogger.Error {
log.WithFields(log.Fields{"data": values}).Error(message)
}
}

// Trace :nodoc:
func (g *GormCustomLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
func (g *GormCustomLogger) Trace(_ context.Context, begin time.Time, fc func() (string, int64), err error) {
sql, rows := fc()
if g.LogLevel <= 0 {
return
Expand Down Expand Up @@ -213,8 +228,11 @@ func (g *GormCustomLogger) Trace(ctx context.Context, begin time.Time, fc func()
}

func applyCockroachDBConnectionOptions(opt *CockroachDBConnectionOptions) *CockroachDBConnectionOptions {
if opt != nil {
return opt
if opt == nil {
return defaultCockroachDBConnectionOptions
}
return defaultCockroachDBConnectionOptions

// if error occurs, also return options from input
_ = mergo.Merge(opt, *defaultCockroachDBConnectionOptions)
return opt
}
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/kumparan/go-connect

go 1.18
go 1.22

require (
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
Expand All @@ -23,8 +23,9 @@ require (
go.opentelemetry.io/otel/sdk v1.21.0
go.opentelemetry.io/otel/trace v1.21.0
google.golang.org/grpc v1.60.1
gorm.io/driver/mysql v1.5.6
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
gorm.io/gorm v1.25.7
)

require (
Expand All @@ -38,7 +39,7 @@ require (
github.com/globalsign/mgo v0.0.0-20181015135952-eeefdecb41b8 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/gofrs/uuid/v5 v5.0.0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/goodsign/monday v1.0.1 // indirect
Expand All @@ -65,11 +66,11 @@ require (
github.com/redis/go-redis/extra/rediscmd/v9 v9.0.5 // indirect
github.com/robfig/cron/v3 v3.0.1 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/smartystreets/goconvey v1.8.1 // indirect
github.com/spf13/cast v1.6.0 // indirect
github.com/uptrace/opentelemetry-go-extra/otelsql v0.2.3 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.opentelemetry.io/otel/exporters/otlp/internal/retry v1.17.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/proto/otlp v1.0.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
Expand Down
Loading

0 comments on commit b12f8e6

Please sign in to comment.