From c86e1dc1b51c935dffb459f5f29f5bf065c2b585 Mon Sep 17 00:00:00 2001 From: nabbar Date: Tue, 11 Apr 2023 14:41:07 +0200 Subject: [PATCH] Package httpserver: - fix bug with context deadline Package Database: - fix darwin/arm64 import C for gorm/sqlite (using shared lib sqlite) Bump dependencies --- database/driver.go | 3 ++ database/driver_darwin.go | 94 +++++++++++++++++++++++++++++++++++++++ go.mod | 55 +++++++++++------------ httpserver/monitor.go | 2 +- httpserver/server.go | 42 +++++++++++++---- httpserver/types/const.go | 2 +- 6 files changed, 159 insertions(+), 39 deletions(-) create mode 100644 database/driver_darwin.go diff --git a/database/driver.go b/database/driver.go index b8443c8e..0fa601a4 100644 --- a/database/driver.go +++ b/database/driver.go @@ -1,3 +1,6 @@ +//go:build !arm && !arm64 +// +build !arm,!arm64 + /* * MIT License * diff --git a/database/driver_darwin.go b/database/driver_darwin.go new file mode 100644 index 00000000..a5cd7cb5 --- /dev/null +++ b/database/driver_darwin.go @@ -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 + } +} diff --git a/go.mod b/go.mod index 7cd0dc1a..62608605 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/httpserver/monitor.go b/httpserver/monitor.go index b3e23c92..ee5b7ccb 100644 --- a/httpserver/monitor.go +++ b/httpserver/monitor.go @@ -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 } } diff --git a/httpserver/server.go b/httpserver/server.go index abb9cede..e233dd5e 100644 --- a/httpserver/server.go +++ b/httpserver/server.go @@ -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" @@ -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 } @@ -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() { diff --git a/httpserver/types/const.go b/httpserver/types/const.go index 77da982d..00d66d88 100644 --- a/httpserver/types/const.go +++ b/httpserver/types/const.go @@ -29,7 +29,7 @@ package types import "time" const ( - TimeoutWaitingPortFreeing = 100 * time.Microsecond + TimeoutWaitingPortFreeing = 250 * time.Microsecond TimeoutWaitingStop = 5 * time.Second BadHandlerName = "no handler" )