-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit acd5346
Showing
46 changed files
with
3,455 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# If you prefer the allow list template instead of the deny list, see community template: | ||
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore | ||
# | ||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
*.dll | ||
*.so | ||
*.dylib | ||
|
||
# Test binary, built with `go test -c` | ||
*.test | ||
|
||
# Output of the go coverage tool, specifically when used with LiteIDE | ||
*.out | ||
|
||
# Dependency directories (remove the comment below to include it) | ||
# vendor/ | ||
|
||
# Go workspace file | ||
go.work | ||
.vscode | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# golib | ||
封装go依赖库 |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
|
||
log "github.com/huweihuang/golib/logger/logrus" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
const ( | ||
defaultConfigPath = "configs" | ||
defaultConfigType = "yaml" | ||
) | ||
|
||
var ( | ||
Get = viper.Get | ||
GetBool = viper.GetBool | ||
GetDuration = viper.GetDuration | ||
GetFloat64 = viper.GetFloat64 | ||
GetInt = viper.GetInt | ||
GetInt32 = viper.GetInt32 | ||
GetInt64 = viper.GetInt64 | ||
GetSizeInBytes = viper.GetSizeInBytes | ||
GetString = viper.GetString | ||
GetStringMap = viper.GetStringMap | ||
GetStringMapString = viper.GetStringMapString | ||
GetStringMapStringSlice = viper.GetStringMapStringSlice | ||
GetStringSlice = viper.GetStringSlice | ||
GetTime = viper.GetTime | ||
IsSet = viper.IsSet | ||
AllSettings = viper.AllSettings | ||
Unmarshal = viper.Unmarshal | ||
UnmarshalKey = viper.UnmarshalKey | ||
) | ||
|
||
func Init(configName string) error { | ||
return InitConfig(defaultConfigPath, configName, defaultConfigType) | ||
} | ||
|
||
func InitConfig(configPath, configName, configType string) error { | ||
viper.SetConfigName(configName) | ||
viper.SetConfigType(configType) | ||
viper.AddConfigPath(configPath) | ||
return viper.ReadInConfig() | ||
} | ||
|
||
func InitConfigByPath(configPath string) error { | ||
viper.SetConfigFile(configPath) | ||
return viper.ReadInConfig() | ||
} | ||
|
||
func InitConfigObject(configName string, configObject interface{}) error { | ||
filePath := fmt.Sprintf("%s/%s.%s", defaultConfigPath, configName, defaultConfigType) | ||
return InitConfigObjectByPath(filePath, configObject) | ||
} | ||
|
||
func InitConfigObjectByPath(configPath string, configObject interface{}) error { | ||
viper.SetConfigFile(configPath) | ||
if err := viper.ReadInConfig(); err != nil { | ||
return fmt.Errorf("failed to read in config by viper, err: %v", err) | ||
} | ||
|
||
if configObject != nil { | ||
err := viper.Unmarshal(configObject) | ||
if err != nil { | ||
return fmt.Errorf("failed to unmarshal, err: %v", err) | ||
} | ||
log.Logger.WithField("config", configObject).Debug("init config") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package db | ||
|
||
import ( | ||
"time" | ||
|
||
sql "github.com/go-sql-driver/mysql" | ||
"gorm.io/driver/mysql" | ||
"gorm.io/gorm" | ||
"gorm.io/gorm/logger" | ||
) | ||
|
||
var DB *gorm.DB | ||
|
||
func SetupDB(addr, dbName, user, passwd, logLevel string) (*gorm.DB, error) { | ||
dsn := FormatDSN(addr, dbName, user, passwd) | ||
level := formatLogLevel(logLevel) | ||
engine, err := gorm.Open(mysql.Open(dsn), &gorm.Config{ | ||
Logger: logger.Default.LogMode(level), | ||
}) | ||
if err != nil { | ||
return nil, err | ||
} | ||
db, err := engine.DB() | ||
if err != nil { | ||
return nil, err | ||
} | ||
// Set the maximum lifetime of the connection (less than server setting) | ||
db.SetConnMaxLifetime(30 * time.Minute) | ||
|
||
return engine, nil | ||
} | ||
|
||
func GetDB() *gorm.DB { | ||
return DB | ||
} | ||
|
||
func Close() error { | ||
db, err := DB.DB() | ||
if err != nil { | ||
return err | ||
} | ||
return db.Close() | ||
} | ||
|
||
// FormatDSN formats the given Config into a DSN string which can be passed to the driver. | ||
func FormatDSN(addr, dbName, user, passwd string) string { | ||
cfg := sql.Config{ | ||
User: user, | ||
Passwd: passwd, | ||
Net: "tcp", | ||
Addr: addr, | ||
DBName: dbName, | ||
ParseTime: true, | ||
AllowNativePasswords: true, | ||
} | ||
return cfg.FormatDSN() | ||
} | ||
|
||
func formatLogLevel(level string) logger.LogLevel { | ||
switch level { | ||
case "silent": | ||
return logger.Silent | ||
case "error": | ||
return logger.Error | ||
case "warn": | ||
return logger.Warn | ||
case "info": | ||
return logger.Info | ||
} | ||
return logger.Silent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# Etcd Queue | ||
|
||
> 基于 https://github.com/etcd-io/etcd/tree/main/client/v3/experimental/recipes 代码修改。 | ||
Etcd Queue是基于Etcd集群做后端存储来实现任务队列的功能。 | ||
|
||
- 写入任务 | ||
- 读取任务 | ||
|
||
|
||
# 使用方法 | ||
|
||
``` | ||
package main | ||
import ( | ||
"fmt" | ||
"github.com/huweihuang/golib/etcdqueue" | ||
) | ||
func main() { | ||
etcdConfig := &etcdqueue.EtcdConfig{ | ||
Endpoints: "https://127.0.0.1:2379", | ||
CaFile: "/etc/kubernetes/pki/etcd/ca.crt", | ||
KeyFile: "/etc/kubernetes/pki/apiserver-etcd-client.key", | ||
CertFile: "/etc/kubernetes/pki/apiserver-etcd-client.crt", | ||
} | ||
jobQueue, err := etcdqueue.NewEtcdQueue(etcdConfig, "/keyprefix") | ||
if err != nil { | ||
fmt.Errorf("faied to new etcd queue, error: %v", err) | ||
} | ||
err = jobQueue.Enqueue("{testjob}") | ||
if err != nil { | ||
fmt.Errorf("failed to enqueue") | ||
} | ||
job, err := jobQueue.Dequeue() | ||
if err != nil { | ||
fmt.Errorf("failed to dequeue") | ||
} | ||
fmt.Printf("queue: %v", job) | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
// Copyright 2016 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package etcdqueue | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"time" | ||
|
||
v3 "github.com/coreos/etcd/clientv3" | ||
"github.com/coreos/etcd/pkg/transport" | ||
) | ||
|
||
var ( | ||
defaultDialTimeout = 10 * time.Second | ||
defaultAotuSyncInterval = 10 * time.Second | ||
) | ||
|
||
// EtcdConfig etcd client arguments | ||
type EtcdConfig struct { | ||
Endpoints string // args for clientv3.Config | ||
DialTimeout time.Duration // args for clientv3.Config | ||
AutoSyncInterval time.Duration // args for clientv3.Config | ||
CaFile string // args for clientv3.Config.TLS | ||
CertFile string // args for clientv3.Config.TLS | ||
KeyFile string // args for clientv3.Config.TLS | ||
} | ||
|
||
// NewETCDClient new etcd client v3 | ||
func NewETCDClient(etcdConfig *EtcdConfig) (*v3.Client, error) { | ||
if etcdConfig.Endpoints == "" { | ||
return nil, fmt.Errorf("no endpoints specified") | ||
} | ||
if etcdConfig.DialTimeout <= 10 { | ||
etcdConfig.DialTimeout = defaultDialTimeout | ||
} | ||
if etcdConfig.AutoSyncInterval <= 30 { | ||
etcdConfig.AutoSyncInterval = defaultAotuSyncInterval | ||
} | ||
|
||
config := v3.Config{ | ||
Endpoints: strings.Split(etcdConfig.Endpoints, ","), | ||
DialTimeout: etcdConfig.DialTimeout, | ||
DialKeepAliveTime: time.Second * 2, | ||
DialKeepAliveTimeout: time.Second * 6, | ||
AutoSyncInterval: etcdConfig.AutoSyncInterval, | ||
} | ||
|
||
if etcdConfig.CaFile != "" && etcdConfig.KeyFile != "" && etcdConfig.CertFile != "" { | ||
tlsInfo := transport.TLSInfo{ | ||
CertFile: etcdConfig.CertFile, | ||
KeyFile: etcdConfig.KeyFile, | ||
TrustedCAFile: etcdConfig.CaFile, | ||
} | ||
|
||
tlsConfig, err := tlsInfo.ClientConfig() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to generate tlsConfig") | ||
} | ||
config.TLS = tlsConfig | ||
} | ||
|
||
cli, err := v3.New(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cli, nil | ||
} | ||
|
||
// NewEtcdQueue new a etcd queue | ||
func NewEtcdQueue(etcdConfig *EtcdConfig, keyPrefix string) (*Queue, error) { | ||
etcdClient, err := NewETCDClient(etcdConfig) | ||
if err != nil { | ||
return nil, fmt.Errorf("faied to new etcd client") | ||
} | ||
queue := NewQueue(etcdClient, keyPrefix) | ||
return queue, nil | ||
} |
Oops, something went wrong.