Skip to content

Commit

Permalink
Updated log
Browse files Browse the repository at this point in the history
  • Loading branch information
dreamsxin committed May 1, 2024
1 parent 263726b commit 269cffb
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 49 deletions.
10 changes: 5 additions & 5 deletions endpoint/endpoint_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/dreamsxin/go-kit/sd/events"

"go.uber.org/zap"
"github.com/dreamsxin/go-kit/log"
)

// 缓存端点实例
Expand All @@ -19,7 +19,7 @@ type EndpointCache struct {
cache map[string]EndpointCloser
err error
endpoints []Endpoint
logger *zap.SugaredLogger
logger *log.Logger
invalidateDeadline time.Time
timeNow func() time.Time
}
Expand All @@ -29,7 +29,7 @@ type EndpointCloser struct {
io.Closer
}

func NewEndpointCache(factory Factory, logger *zap.SugaredLogger, options EndpointerOptions) *EndpointCache {
func NewEndpointCache(factory Factory, logger *log.Logger, options EndpointerOptions) *EndpointCache {
return &EndpointCache{
options: options,
factory: factory,
Expand All @@ -49,7 +49,7 @@ func (c *EndpointCache) Update(event events.Event) {
return
}

c.logger.Debugln("err", event.Err)
c.logger.Sugar().Debugln("err", event.Err)
if !c.options.InvalidateOnError {
return
}
Expand All @@ -73,7 +73,7 @@ func (c *EndpointCache) updateCache(instances []string) {

service, closer, err := c.factory(instance)
if err != nil {
c.logger.Debugln("instance", instance, "err", err)
c.logger.Sugar().Debugln("instance", instance, "err", err)
continue
}
cache[instance] = EndpointCloser{service, closer}
Expand Down
8 changes: 4 additions & 4 deletions examples/basic/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (

"github.com/dreamsxin/go-kit/endpoint"
"github.com/dreamsxin/go-kit/examples/common"
"github.com/dreamsxin/go-kit/log"
"github.com/dreamsxin/go-kit/sd/consul"
"github.com/dreamsxin/go-kit/sd/endpointer"
"github.com/dreamsxin/go-kit/sd/endpointer/balancer"
"github.com/dreamsxin/go-kit/sd/endpointer/executor"

capi "github.com/hashicorp/consul/api"
"go.uber.org/zap"
)

// go test -v -count=1 -run TestExecutorRetry .\executor_test.go
Expand All @@ -27,15 +27,15 @@ func TestExecutorRetry(t *testing.T) {
return ep, nil, nil
}

logger, _ := zap.NewDevelopment()
logger, _ := log.NewDevelopment()

client, err := capi.NewClient(capi.DefaultConfig())
if err != nil {
panic(err)
}
instrancer := consul.NewInstancer(consul.NewClient(client), logger.Sugar(), serverName, true)
instrancer := consul.NewInstancer(consul.NewClient(client), logger, serverName, true)

endpointer := endpointer.NewEndpointer(instrancer, factory, logger.Sugar())
endpointer := endpointer.NewEndpointer(instrancer, factory, logger)

robin := balancer.NewRoundRobin(endpointer)
retry := executor.Retry(5, time.Duration(1*time.Second), robin)
Expand Down
8 changes: 4 additions & 4 deletions examples/basic/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

"github.com/dreamsxin/go-kit/endpoint"
"github.com/dreamsxin/go-kit/examples/common"
"github.com/dreamsxin/go-kit/log"
"github.com/dreamsxin/go-kit/sd/consul"
"github.com/dreamsxin/go-kit/sd/endpointer"

capi "github.com/hashicorp/consul/api"
"go.uber.org/zap"
)

// go test -v -count=1 -run TestFactory .\factory_test.go
Expand All @@ -24,15 +24,15 @@ func TestFactory(t *testing.T) {
return ep, nil, nil
}

logger, _ := zap.NewDevelopment()
logger, _ := log.NewDevelopment()

client, err := capi.NewClient(capi.DefaultConfig())
if err != nil {
panic(err)
}
instrancer := consul.NewInstancer(consul.NewClient(client), logger.Sugar(), serverName, true)
instrancer := consul.NewInstancer(consul.NewClient(client), logger, serverName, true)

endpointer := endpointer.NewEndpointer(instrancer, factory, logger.Sugar())
endpointer := endpointer.NewEndpointer(instrancer, factory, logger)
endpoints, err := endpointer.Endpoints()
logger.Sugar().Debugln("-----------TestFactory--------", endpoints, err)
if len(endpoints) > 0 {
Expand Down
13 changes: 6 additions & 7 deletions examples/transport/client/http/http_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package http
import (
"context"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
Expand All @@ -14,20 +13,20 @@ import (

"github.com/dreamsxin/go-kit/endpoint"
"github.com/dreamsxin/go-kit/examples/common"
"github.com/dreamsxin/go-kit/log"
"github.com/dreamsxin/go-kit/sd/consul"
"github.com/dreamsxin/go-kit/sd/endpointer"
"github.com/dreamsxin/go-kit/sd/endpointer/balancer"
"github.com/dreamsxin/go-kit/sd/endpointer/executor"
transportclient "github.com/dreamsxin/go-kit/transport/http/client"

capi "github.com/hashicorp/consul/api"
"go.uber.org/zap"
)

// go test -v -count=1 -run TestExecutorHttpClient .\http_executor_test.go
func TestExecutorHttpClient(t *testing.T) {

logger, _ := zap.NewDevelopment()
logger, _ := log.NewDevelopment()

// 连接 consul
cfg := capi.DefaultConfig()
Expand Down Expand Up @@ -59,13 +58,13 @@ func TestExecutorHttpClient(t *testing.T) {

// 注册服务
serverName := "test"
registrar := consul.NewRegistrar(consul.NewClient(client), logger.Sugar(), serverName, serverURL.Host, port)
registrar := consul.NewRegistrar(consul.NewClient(client), logger, serverName, serverURL.Host, port)
registrar.Register()
defer registrar.Deregister()

// 创建端点工厂
factory := func(instance string) (endpoint.Endpoint, io.Closer, error) {
log.Println("instance", instance)
logger.Sugar().Debugln("instance", instance)
// 客户端
ep := transportclient.NewClient(
"POST",
Expand All @@ -82,10 +81,10 @@ func TestExecutorHttpClient(t *testing.T) {
}

// 创建服务发现器
instrancer := consul.NewInstancer(consul.NewClient(client), logger.Sugar(), serverName, true)
instrancer := consul.NewInstancer(consul.NewClient(client), logger, serverName, true)

// 创建端点生成器
endpointer := endpointer.NewEndpointer(instrancer, factory, logger.Sugar())
endpointer := endpointer.NewEndpointer(instrancer, factory, logger)

// 创建负载均衡器
robin := balancer.NewRoundRobin(endpointer)
Expand Down
4 changes: 4 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ import (
)

type Logger = zap.Logger

func NewDevelopment() (*Logger, error) {
return zap.NewDevelopment()
}
30 changes: 15 additions & 15 deletions sd/consul/instancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"time"

consul "github.com/hashicorp/consul/api"
"go.uber.org/zap"

"github.com/dreamsxin/go-kit/log"
"github.com/dreamsxin/go-kit/sd/events"
"github.com/dreamsxin/go-kit/sd/instance"
"github.com/dreamsxin/go-kit/utils"
Expand All @@ -21,7 +21,7 @@ var errStopped = errors.New("quit and closed consul instancer")
type Instancer struct {
cache *instance.Cache
client Client
logger *zap.SugaredLogger
logger *log.Logger
service string
tags []string
passingOnly bool // 只返回正常的实例
Expand All @@ -36,7 +36,7 @@ func TagsInstancerOptions(tags []string) InstancerOption {
}
}

func NewInstancer(client Client, logger *zap.SugaredLogger, service string, passingOnly bool) *Instancer {
func NewInstancer(client Client, logger *log.Logger, service string, passingOnly bool) *Instancer {
s := &Instancer{
cache: instance.NewCache(),
client: client,
Expand All @@ -48,9 +48,9 @@ func NewInstancer(client Client, logger *zap.SugaredLogger, service string, pass

instances, index, err := s.getInstances(defaultIndex, nil)
if err == nil {
s.logger.Debugln("instances", len(instances))
s.logger.Sugar().Errorln("instances", len(instances))
} else {
s.logger.Debugln("err", err)
s.logger.Sugar().Debugln("err", err)
}

s.cache.Update(events.Event{Instances: instances, Err: err})
Expand All @@ -74,24 +74,24 @@ func (s *Instancer) loop(lastIndex uint64) {
instances, index, err = s.getInstances(lastIndex, s.quitc)
switch {
case errors.Is(err, errStopped):
s.logger.Debugln("loop", errStopped)
s.logger.Sugar().Debugln("loop", errStopped)
return // stopped via quitc
case err != nil:
s.logger.Debugln("loop", err, d.Seconds())
s.logger.Sugar().Debugln("loop", err, d.Seconds())
time.Sleep(d)
d = utils.Exponential(d)
s.cache.Update(events.Event{Err: err})
case index == defaultIndex:
s.logger.Debugln("loop", "index is not sane", d.Seconds())
s.logger.Sugar().Debugln("loop", "index is not sane", d.Seconds())
time.Sleep(d)
d = utils.Exponential(d)
case index < lastIndex:
s.logger.Debugln("loop", "index is less than previous; resetting to default", d.Seconds())
s.logger.Sugar().Debugln("loop", "index is less than previous; resetting to default", d.Seconds())
lastIndex = defaultIndex
time.Sleep(d)
d = utils.Exponential(d)
default:
s.logger.Debugln("loop", "default", "index", index)
s.logger.Sugar().Debugln("loop", "default", "index", index)
lastIndex = index
s.cache.Update(events.Event{Instances: instances})
d = 10 * time.Millisecond
Expand All @@ -117,11 +117,11 @@ func (s *Instancer) getInstances(lastIndex uint64, interruptc chan struct{}) ([]
)

go func() {
s.logger.Debugln("getInstances", "lastIndex", lastIndex)
s.logger.Sugar().Debugln("getInstances", "lastIndex", lastIndex)
entries, meta, err := s.client.Service(s.service, tag, s.passingOnly, &consul.QueryOptions{
WaitIndex: lastIndex,
})
s.logger.Debugln("getInstances", entries, meta, err)
s.logger.Sugar().Debugln("getInstances", entries, meta, err)
if err != nil {
errc <- err
return
Expand All @@ -137,13 +137,13 @@ func (s *Instancer) getInstances(lastIndex uint64, interruptc chan struct{}) ([]

select {
case err := <-errc:
s.logger.Debugln("getInstances", err)
s.logger.Sugar().Debugln("getInstances", err)
return nil, 0, err
case res := <-resc:
s.logger.Debugln("getInstances", res)
s.logger.Sugar().Debugln("getInstances", res)
return res.instances, res.index, nil
case <-interruptc:
s.logger.Debugln("getInstances", errStopped)
s.logger.Sugar().Debugln("getInstances", errStopped)
return nil, 0, errStopped
}
}
Expand Down
15 changes: 8 additions & 7 deletions sd/consul/registrar.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@ package consul
import (
"strconv"

"github.com/dreamsxin/go-kit/log"

stdconsul "github.com/hashicorp/consul/api"
"go.uber.org/zap"
)

// 服务注册类
type Registrar struct {
client Client
registration *stdconsul.AgentServiceRegistration
logger *zap.SugaredLogger
logger *log.Logger
}

type RegistrarOption func(*Registrar)
Expand Down Expand Up @@ -40,7 +41,7 @@ func CheckRegistrarOptions(check *stdconsul.AgentServiceCheck) RegistrarOption {
}
}

func NewRegistrar(client Client, logger *zap.SugaredLogger, name string, address string, port int, options ...RegistrarOption) *Registrar {
func NewRegistrar(client Client, logger *log.Logger, name string, address string, port int, options ...RegistrarOption) *Registrar {

r := &Registrar{
client: client,
Expand All @@ -62,16 +63,16 @@ func NewRegistrar(client Client, logger *zap.SugaredLogger, name string, address

func (p *Registrar) Register() {
if err := p.client.Register(p.registration); err != nil {
p.logger.Debugln("err", err)
p.logger.Sugar().Debugln("err", err)
} else {
p.logger.Debugln("action", "register")
p.logger.Sugar().Debugln("action", "register")
}
}

func (p *Registrar) Deregister() {
if err := p.client.Deregister(p.registration); err != nil {
p.logger.Debugln("err", err)
p.logger.Sugar().Debugln("err", err)
} else {
p.logger.Debugln("action", "deregister")
p.logger.Sugar().Debugln("action", "deregister")
}
}
5 changes: 2 additions & 3 deletions sd/endpointer/endpointer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package endpointer

import (
"github.com/dreamsxin/go-kit/endpoint"
"github.com/dreamsxin/go-kit/log"
"github.com/dreamsxin/go-kit/sd/events"
"github.com/dreamsxin/go-kit/sd/interfaces"

"go.uber.org/zap"
)

// 端点生成器:根据服务发现类获取的服务地址以及端点构建工厂创建端点
type Endpointer interface {
Endpoints() ([]endpoint.Endpoint, error)
}

func NewEndpointer(src interfaces.Instancer, f endpoint.Factory, logger *zap.SugaredLogger, options ...endpoint.EndpointerOption) Endpointer {
func NewEndpointer(src interfaces.Instancer, f endpoint.Factory, logger *log.Logger, options ...endpoint.EndpointerOption) Endpointer {
opts := endpoint.EndpointerOptions{}
for _, opt := range options {
opt(&opts)
Expand Down
2 changes: 1 addition & 1 deletion sd/examples/basic/instancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestInstancer(t *testing.T) {
logger, _ := zap.NewDevelopment()

ch := make(chan events.Event)
instrancer := consul.NewInstancer(consul.NewClient(client), logger.Sugar(), "test", true)
instrancer := consul.NewInstancer(consul.NewClient(client), logger, "test", true)

var wait sync.WaitGroup
wait.Add(1)
Expand Down
6 changes: 3 additions & 3 deletions sd/examples/basic/registrar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func TestRegistrar(t *testing.T) {

logger, _ := zap.NewDevelopment()

registrar := consul.NewRegistrar(consul.NewClient(client), logger.Sugar(), "test", "localhost", 8500)
registrar := consul.NewRegistrar(consul.NewClient(client), logger, "test", "localhost", 8500)
registrar.Register()
//defer registrar.Deregister()
//time.Sleep(30 * time.Second)
Expand All @@ -35,7 +35,7 @@ func TestDeregister(t *testing.T) {

logger, _ := zap.NewDevelopment()

registrar := consul.NewRegistrar(consul.NewClient(client), logger.Sugar(), "test", "localhost", 8500)
registrar := consul.NewRegistrar(consul.NewClient(client), logger, "test", "localhost", 8500)
//删除旧的服务实例
registrar.Deregister()
}
Expand All @@ -50,7 +50,7 @@ func TestRegistrarCheck(t *testing.T) {

logger, _ := zap.NewDevelopment()

registrar := consul.NewRegistrar(consul.NewClient(client), logger.Sugar(), "news", "localhost", 7888, consul.CheckRegistrarOptions(&capi.AgentServiceCheck{
registrar := consul.NewRegistrar(consul.NewClient(client), logger, "news", "localhost", 7888, consul.CheckRegistrarOptions(&capi.AgentServiceCheck{
CheckID: "status",
HTTP: "http://localhost:7888/",
Timeout: "5s", //超时
Expand Down

0 comments on commit 269cffb

Please sign in to comment.