[agent-go] 新增线程池部分代码

This commit is contained in:
zeaslity
2023-03-28 13:47:04 +08:00
parent 31f9267401
commit 84fe111482
6 changed files with 54 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ package g
import (
"agent-go/config"
"github.com/panjf2000/ants/v2"
"github.com/spf13/viper"
)
@@ -9,6 +10,7 @@ type Global struct {
LOG *Logger
NacosConfig *viper.Viper
AgentServerInfo *config.AgentServerInfo
P *ants.Pool
}
const (
@@ -21,11 +23,19 @@ const (
var logger, _ = NewLogger()
var pool, _ = ants.NewPool(100, ants.WithNonblocking(true), ants.WithLogger(logger))
var G = NewGlobal(
logger,
pool,
)
// NewGlobal NewGlobal构造函数返回一个新的Global实例其中包含指定的Logger。
func NewGlobal(logger *Logger) *Global {
return &Global{LOG: logger}
func NewGlobal(logger *Logger, pool *ants.Pool) *Global {
return &Global{
LOG: logger,
NacosConfig: nil,
AgentServerInfo: nil,
P: pool,
}
}

View File

@@ -1,6 +1,7 @@
package g
import (
"fmt"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
@@ -34,16 +35,30 @@ func NewLogger() (*Logger, error) {
return &Logger{logger}, nil
}
func (l *Logger) Printf(msg string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf("%s ==> %v", msg, args))
}
// Debug logs a debug message.
func (l *Logger) Debug(msg string, fields ...zap.Field) {
l.Logger.Debug(msg, fields...)
}
func (l *Logger) DebugF(msg string, args ...interface{}) {
l.Logger.Debug(fmt.Sprintf(msg, args...))
}
// Info logs an info message.
func (l *Logger) Info(msg string, fields ...zap.Field) {
l.Logger.Info(msg, fields...)
}
// InfoF logs an info message with format
func (l *Logger) InfoF(msg string, args ...interface{}) {
l.Logger.Info(fmt.Sprintf(msg, args...))
}
// Warn logs a warning message.
func (l *Logger) Warn(msg string, fields ...zap.Field) {
l.Logger.Warn(msg, fields...)