Skip to content

Commit

Permalink
Package httpserver:
Browse files Browse the repository at this point in the history
  - fix bug with context deadline

Package Database:
  - fix darwin/arm64 import C for gorm/sqlite (using shared lib sqlite)

Bump dependencies
  • Loading branch information
nabbar committed Apr 11, 2023
1 parent bd04ad8 commit c86e1dc
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 39 deletions.
3 changes: 3 additions & 0 deletions database/driver.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !arm && !arm64
// +build !arm,!arm64

/*
* MIT License
*
Expand Down
94 changes: 94 additions & 0 deletions database/driver_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
//go:build arm || arm64
// +build arm arm64

/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/

package database

import (
"strings"

drvclk "gorm.io/driver/clickhouse"
drvmys "gorm.io/driver/mysql"
drvpsq "gorm.io/driver/postgres"
drvsrv "gorm.io/driver/sqlserver"
gormdb "gorm.io/gorm"
)

const (
DriverNone = ""
DriverMysql = "mysql"
DriverPostgreSQL = "psql"
DriverSQLServer = "sqlserver"
DriverClikHouse = "clickhouse"
)

type Driver string

func DriverFromString(drv string) Driver {
switch strings.ToLower(drv) {

case strings.ToLower(DriverMysql):
return DriverMysql

case strings.ToLower(DriverPostgreSQL):
return DriverPostgreSQL

case strings.ToLower(DriverSQLServer):
return DriverSQLServer

case strings.ToLower(DriverClikHouse):
return DriverClikHouse

default:
return DriverNone
}
}

func (d Driver) String() string {
return string(d)
}

func (d Driver) Dialector(dsn string) gormdb.Dialector {
switch d {

case DriverMysql:
return drvmys.Open(dsn)

case DriverPostgreSQL:
return drvpsq.Open(dsn)

case DriverSQLServer:
return drvsrv.Open(dsn)

case DriverClikHouse:
return drvclk.Open(dsn)

default:
return nil
}
}
55 changes: 27 additions & 28 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ module github.com/nabbar/golib
go 1.20

require (
github.com/aws/aws-sdk-go-v2 v1.17.7
github.com/aws/aws-sdk-go-v2/config v1.18.19
github.com/aws/aws-sdk-go-v2/credentials v1.13.18
github.com/aws/aws-sdk-go-v2/service/iam v1.19.8
github.com/aws/aws-sdk-go-v2/service/s3 v1.31.1
github.com/aws/aws-sdk-go-v2 v1.17.8
github.com/aws/aws-sdk-go-v2/config v1.18.21
github.com/aws/aws-sdk-go-v2/credentials v1.13.20
github.com/aws/aws-sdk-go-v2/service/iam v1.19.10
github.com/aws/aws-sdk-go-v2/service/s3 v1.31.3
github.com/bits-and-blooms/bitset v1.5.0
github.com/c-bata/go-prompt v0.2.6
github.com/fatih/color v1.15.0
Expand Down Expand Up @@ -45,25 +45,24 @@ require (
github.com/xhit/go-simple-mail v2.2.2+incompatible
github.com/xujiajun/utils v0.0.0-20220904132955-5f7c5b914235
golang.org/x/exp v0.0.0-20230321023759-10a507213a29
golang.org/x/net v0.8.0
golang.org/x/oauth2 v0.6.0
golang.org/x/net v0.9.0
golang.org/x/oauth2 v0.7.0
golang.org/x/sync v0.1.0
golang.org/x/sys v0.7.0
golang.org/x/term v0.7.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/clickhouse v0.5.0
gorm.io/driver/mysql v1.4.7
gorm.io/driver/postgres v1.4.8
gorm.io/driver/sqlite v1.4.4
gorm.io/driver/sqlserver v1.4.2
gorm.io/gorm v1.24.6
gorm.io/driver/clickhouse v0.5.1
gorm.io/driver/mysql v1.5.0
gorm.io/driver/postgres v1.5.0
gorm.io/driver/sqlite v1.5.0
gorm.io/driver/sqlserver v1.4.3
gorm.io/gorm v1.25.0
)

require (
github.com/Azure/go-ntlmssp v0.0.0-20221128193559-754e69321358 // indirect
github.com/ClickHouse/ch-go v0.54.0 // indirect
github.com/ClickHouse/clickhouse-go/v2 v2.8.3 // indirect
github.com/DataDog/zstd v1.5.2 // indirect
github.com/Masterminds/goutils v1.1.1 // indirect
github.com/Masterminds/semver v1.5.0 // indirect
github.com/Masterminds/sprig v2.22.0+incompatible // indirect
Expand All @@ -75,18 +74,18 @@ require (
github.com/andybalholm/cascadia v1.3.1 // indirect
github.com/armon/go-metrics v0.4.1 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.4.10 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.31 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.25 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.32 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.23 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.13.2 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.32 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.26 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.33 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.0.24 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.9.11 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.26 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.25 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.12.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.18.7 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.1.27 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.26 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.14.1 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.12.8 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.8 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.18.9 // indirect
github.com/aws/smithy-go v1.13.5 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/bwmarrin/snowflake v0.3.0 // indirect
Expand Down Expand Up @@ -117,7 +116,7 @@ require (
github.com/google/btree v1.1.2 // indirect
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/pprof v0.0.0-20230405160723-4a4c7d95572b // indirect
github.com/google/pprof v0.0.0-20230406165453-00490a63f317 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
Expand Down Expand Up @@ -193,11 +192,11 @@ require (
go.opentelemetry.io/otel v1.14.0 // indirect
go.opentelemetry.io/otel/trace v1.14.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.7.0 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/mod v0.10.0 // indirect
golang.org/x/text v0.9.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/tools v0.8.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.30.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
Expand Down
2 changes: 1 addition & 1 deletion httpserver/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (o *srv) HealthCheck(ctx context.Context) error {
} else if e = o.r.ErrorsLast(); e != nil {
return e
} else {
return errNotRunning
return nil
}
}

Expand Down
42 changes: 33 additions & 9 deletions httpserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ import (
"log"
"net"
"net/http"
"net/url"
"strings"

liberr "github.com/nabbar/golib/errors"
srvtps "github.com/nabbar/golib/httpserver/types"
Expand Down Expand Up @@ -188,17 +190,27 @@ func (o *srv) PortInUse(ctx context.Context, listen string) liberr.Error {
}
}()

ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing)
con, err = dia.DialContext(ctx, "tcp", listen)
if strings.Contains(listen, ":") {
uri := &url.URL{
Host: listen,
}

if con != nil {
_ = con.Close()
con = nil
if h := uri.Hostname(); h == "0.0.0.0" || h == "::1" {
listen = "127.0.0.1:" + uri.Port()
}
}

cnl()
cnl = nil
if _, ok := ctx.Deadline(); !ok {
ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing)
defer cnl()
}

con, err = dia.DialContext(ctx, "tcp", listen)
defer func() {
if con != nil {
_ = con.Close()
}
}()
if err != nil {
return nil
}
Expand All @@ -215,8 +227,20 @@ func (o *srv) PortNotUse(ctx context.Context, listen string) error {
dia = net.Dialer{}
)

ctx, cnl = context.WithTimeout(context.TODO(), srvtps.TimeoutWaitingPortFreeing)
defer cnl()
if strings.Contains(listen, ":") {
uri := &url.URL{
Host: listen,
}

if h := uri.Hostname(); h == "0.0.0.0" || h == "::1" {
listen = "127.0.0.1:" + uri.Port()
}
}

if _, ok := ctx.Deadline(); !ok {
ctx, cnl = context.WithTimeout(ctx, srvtps.TimeoutWaitingPortFreeing)
defer cnl()
}

con, err = dia.DialContext(ctx, "tcp", listen)
defer func() {
Expand Down
2 changes: 1 addition & 1 deletion httpserver/types/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ package types
import "time"

const (
TimeoutWaitingPortFreeing = 100 * time.Microsecond
TimeoutWaitingPortFreeing = 250 * time.Microsecond
TimeoutWaitingStop = 5 * time.Second
BadHandlerName = "no handler"
)

0 comments on commit c86e1dc

Please sign in to comment.