changed embedded mutex to private field (#217)
This commit is contained in:
@@ -22,8 +22,8 @@ type Broker struct {
|
|||||||
subscribers map[string][]*Subscriber
|
subscribers map[string][]*Subscriber
|
||||||
addr string
|
addr string
|
||||||
opts broker.Options
|
opts broker.Options
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
connected bool
|
connected bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type memoryMessage struct {
|
type memoryMessage struct {
|
||||||
@@ -72,9 +72,9 @@ func (b *Broker) newCodec(ct string) (codec.Codec, error) {
|
|||||||
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
ct = ct[:idx]
|
ct = ct[:idx]
|
||||||
}
|
}
|
||||||
b.RLock()
|
b.mu.RLock()
|
||||||
c, ok := b.opts.Codecs[ct]
|
c, ok := b.opts.Codecs[ct]
|
||||||
b.RUnlock()
|
b.mu.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
@@ -96,8 +96,8 @@ func (b *Broker) Connect(ctx context.Context) error {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Lock()
|
b.mu.Lock()
|
||||||
defer b.Unlock()
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
if b.connected {
|
if b.connected {
|
||||||
return nil
|
return nil
|
||||||
@@ -126,8 +126,8 @@ func (b *Broker) Disconnect(ctx context.Context) error {
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Lock()
|
b.mu.Lock()
|
||||||
defer b.Unlock()
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
if !b.connected {
|
if !b.connected {
|
||||||
return nil
|
return nil
|
||||||
@@ -183,12 +183,12 @@ func (b *Broker) fnPublish(ctx context.Context, topic string, messages ...broker
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.Message) error {
|
func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.Message) error {
|
||||||
b.RLock()
|
b.mu.RLock()
|
||||||
if !b.connected {
|
if !b.connected {
|
||||||
b.RUnlock()
|
b.mu.RUnlock()
|
||||||
return broker.ErrNotConnected
|
return broker.ErrNotConnected
|
||||||
}
|
}
|
||||||
b.RUnlock()
|
b.mu.RUnlock()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@@ -196,9 +196,9 @@ func (b *Broker) publish(ctx context.Context, topic string, messages ...broker.M
|
|||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
|
|
||||||
b.RLock()
|
b.mu.RLock()
|
||||||
subs, ok := b.subscribers[topic]
|
subs, ok := b.subscribers[topic]
|
||||||
b.RUnlock()
|
b.mu.RUnlock()
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -255,12 +255,12 @@ func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interfac
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
b.RLock()
|
b.mu.RLock()
|
||||||
if !b.connected {
|
if !b.connected {
|
||||||
b.RUnlock()
|
b.mu.RUnlock()
|
||||||
return nil, broker.ErrNotConnected
|
return nil, broker.ErrNotConnected
|
||||||
}
|
}
|
||||||
b.RUnlock()
|
b.mu.RUnlock()
|
||||||
|
|
||||||
sid, err := id.New()
|
sid, err := id.New()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -278,13 +278,13 @@ func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interfac
|
|||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
b.Lock()
|
b.mu.Lock()
|
||||||
b.subscribers[topic] = append(b.subscribers[topic], sub)
|
b.subscribers[topic] = append(b.subscribers[topic], sub)
|
||||||
b.Unlock()
|
b.mu.Unlock()
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
<-sub.exit
|
<-sub.exit
|
||||||
b.Lock()
|
b.mu.Lock()
|
||||||
newSubscribers := make([]*Subscriber, 0, len(b.subscribers)-1)
|
newSubscribers := make([]*Subscriber, 0, len(b.subscribers)-1)
|
||||||
for _, sb := range b.subscribers[topic] {
|
for _, sb := range b.subscribers[topic] {
|
||||||
if sb.id == sub.id {
|
if sb.id == sub.id {
|
||||||
@@ -293,7 +293,7 @@ func (b *Broker) fnSubscribe(ctx context.Context, topic string, handler interfac
|
|||||||
newSubscribers = append(newSubscribers, sb)
|
newSubscribers = append(newSubscribers, sb)
|
||||||
}
|
}
|
||||||
b.subscribers[topic] = newSubscribers
|
b.subscribers[topic] = newSubscribers
|
||||||
b.Unlock()
|
b.mu.Unlock()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
return sub, nil
|
return sub, nil
|
||||||
|
|||||||
@@ -14,16 +14,16 @@ type NoopBroker struct {
|
|||||||
funcPublish FuncPublish
|
funcPublish FuncPublish
|
||||||
funcSubscribe FuncSubscribe
|
funcSubscribe FuncSubscribe
|
||||||
opts Options
|
opts Options
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *NoopBroker) newCodec(ct string) (codec.Codec, error) {
|
func (b *NoopBroker) newCodec(ct string) (codec.Codec, error) {
|
||||||
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
if idx := strings.IndexRune(ct, ';'); idx >= 0 {
|
||||||
ct = ct[:idx]
|
ct = ct[:idx]
|
||||||
}
|
}
|
||||||
b.RLock()
|
b.mu.RLock()
|
||||||
c, ok := b.opts.Codecs[ct]
|
c, ok := b.opts.Codecs[ct]
|
||||||
b.RUnlock()
|
b.mu.RUnlock()
|
||||||
if ok {
|
if ok {
|
||||||
return c, nil
|
return c, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type httpProfile struct {
|
type httpProfile struct {
|
||||||
server *http.Server
|
server *http.Server
|
||||||
sync.Mutex
|
mu sync.Mutex
|
||||||
running bool
|
running bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -21,8 +21,8 @@ var DefaultAddress = ":6060"
|
|||||||
|
|
||||||
// Start the profiler
|
// Start the profiler
|
||||||
func (h *httpProfile) Start() error {
|
func (h *httpProfile) Start() error {
|
||||||
h.Lock()
|
h.mu.Lock()
|
||||||
defer h.Unlock()
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
if h.running {
|
if h.running {
|
||||||
return nil
|
return nil
|
||||||
@@ -30,9 +30,9 @@ func (h *httpProfile) Start() error {
|
|||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
if err := h.server.ListenAndServe(); err != nil {
|
if err := h.server.ListenAndServe(); err != nil {
|
||||||
h.Lock()
|
h.mu.Lock()
|
||||||
h.running = false
|
h.running = false
|
||||||
h.Unlock()
|
h.mu.Unlock()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@@ -43,8 +43,8 @@ func (h *httpProfile) Start() error {
|
|||||||
|
|
||||||
// Stop the profiler
|
// Stop the profiler
|
||||||
func (h *httpProfile) Stop() error {
|
func (h *httpProfile) Stop() error {
|
||||||
h.Lock()
|
h.mu.Lock()
|
||||||
defer h.Unlock()
|
defer h.mu.Unlock()
|
||||||
|
|
||||||
if !h.running {
|
if !h.running {
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ type profiler struct {
|
|||||||
cpuFile *os.File
|
cpuFile *os.File
|
||||||
memFile *os.File
|
memFile *os.File
|
||||||
opts profile.Options
|
opts profile.Options
|
||||||
sync.Mutex
|
mu sync.Mutex
|
||||||
running bool
|
running bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,8 +39,8 @@ func (p *profiler) writeHeap(f *os.File) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *profiler) Start() error {
|
func (p *profiler) Start() error {
|
||||||
p.Lock()
|
p.mu.Lock()
|
||||||
defer p.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
if p.running {
|
if p.running {
|
||||||
return nil
|
return nil
|
||||||
@@ -86,8 +86,8 @@ func (p *profiler) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *profiler) Stop() error {
|
func (p *profiler) Stop() error {
|
||||||
p.Lock()
|
p.mu.Lock()
|
||||||
defer p.Unlock()
|
defer p.mu.Unlock()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-p.exit:
|
case <-p.exit:
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ type memory struct {
|
|||||||
records map[string]services
|
records map[string]services
|
||||||
watchers map[string]*watcher
|
watchers map[string]*watcher
|
||||||
opts register.Options
|
opts register.Options
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// services is a KV map with service name as the key and a map of records as the value
|
// services is a KV map with service name as the key and a map of records as the value
|
||||||
@@ -57,7 +57,7 @@ func (m *memory) ttlPrune() {
|
|||||||
defer prune.Stop()
|
defer prune.Stop()
|
||||||
|
|
||||||
for range prune.C {
|
for range prune.C {
|
||||||
m.Lock()
|
m.mu.Lock()
|
||||||
for namespace, services := range m.records {
|
for namespace, services := range m.records {
|
||||||
for service, versions := range services {
|
for service, versions := range services {
|
||||||
for version, record := range versions {
|
for version, record := range versions {
|
||||||
@@ -72,24 +72,24 @@ func (m *memory) ttlPrune() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.Unlock()
|
m.mu.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *memory) sendEvent(r *register.Result) {
|
func (m *memory) sendEvent(r *register.Result) {
|
||||||
m.RLock()
|
m.mu.RLock()
|
||||||
watchers := make([]*watcher, 0, len(m.watchers))
|
watchers := make([]*watcher, 0, len(m.watchers))
|
||||||
for _, w := range m.watchers {
|
for _, w := range m.watchers {
|
||||||
watchers = append(watchers, w)
|
watchers = append(watchers, w)
|
||||||
}
|
}
|
||||||
m.RUnlock()
|
m.mu.RUnlock()
|
||||||
|
|
||||||
for _, w := range watchers {
|
for _, w := range watchers {
|
||||||
select {
|
select {
|
||||||
case <-w.exit:
|
case <-w.exit:
|
||||||
m.Lock()
|
m.mu.Lock()
|
||||||
delete(m.watchers, w.id)
|
delete(m.watchers, w.id)
|
||||||
m.Unlock()
|
m.mu.Unlock()
|
||||||
default:
|
default:
|
||||||
select {
|
select {
|
||||||
case w.res <- r:
|
case w.res <- r:
|
||||||
@@ -113,8 +113,8 @@ func (m *memory) Init(opts ...register.Option) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// add services
|
// add services
|
||||||
m.Lock()
|
m.mu.Lock()
|
||||||
defer m.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -124,8 +124,8 @@ func (m *memory) Options() register.Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *memory) Register(_ context.Context, s *register.Service, opts ...register.RegisterOption) error {
|
func (m *memory) Register(_ context.Context, s *register.Service, opts ...register.RegisterOption) error {
|
||||||
m.Lock()
|
m.mu.Lock()
|
||||||
defer m.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
options := register.NewRegisterOptions(opts...)
|
options := register.NewRegisterOptions(opts...)
|
||||||
|
|
||||||
@@ -197,8 +197,8 @@ func (m *memory) Register(_ context.Context, s *register.Service, opts ...regist
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...register.DeregisterOption) error {
|
func (m *memory) Deregister(ctx context.Context, s *register.Service, opts ...register.DeregisterOption) error {
|
||||||
m.Lock()
|
m.mu.Lock()
|
||||||
defer m.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
options := register.NewDeregisterOptions(opts...)
|
options := register.NewDeregisterOptions(opts...)
|
||||||
|
|
||||||
@@ -264,9 +264,9 @@ func (m *memory) LookupService(ctx context.Context, name string, opts ...registe
|
|||||||
|
|
||||||
// if it's a wildcard domain, return from all domains
|
// if it's a wildcard domain, return from all domains
|
||||||
if options.Namespace == register.WildcardNamespace {
|
if options.Namespace == register.WildcardNamespace {
|
||||||
m.RLock()
|
m.mu.RLock()
|
||||||
recs := m.records
|
recs := m.records
|
||||||
m.RUnlock()
|
m.mu.RUnlock()
|
||||||
|
|
||||||
var services []*register.Service
|
var services []*register.Service
|
||||||
|
|
||||||
@@ -286,8 +286,8 @@ func (m *memory) LookupService(ctx context.Context, name string, opts ...registe
|
|||||||
return services, nil
|
return services, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
m.RLock()
|
m.mu.RLock()
|
||||||
defer m.RUnlock()
|
defer m.mu.RUnlock()
|
||||||
|
|
||||||
// check the domain exists
|
// check the domain exists
|
||||||
services, ok := m.records[options.Namespace]
|
services, ok := m.records[options.Namespace]
|
||||||
@@ -319,9 +319,9 @@ func (m *memory) ListServices(ctx context.Context, opts ...register.ListOption)
|
|||||||
|
|
||||||
// if it's a wildcard domain, list from all domains
|
// if it's a wildcard domain, list from all domains
|
||||||
if options.Namespace == register.WildcardNamespace {
|
if options.Namespace == register.WildcardNamespace {
|
||||||
m.RLock()
|
m.mu.RLock()
|
||||||
recs := m.records
|
recs := m.records
|
||||||
m.RUnlock()
|
m.mu.RUnlock()
|
||||||
|
|
||||||
var services []*register.Service
|
var services []*register.Service
|
||||||
|
|
||||||
@@ -336,8 +336,8 @@ func (m *memory) ListServices(ctx context.Context, opts ...register.ListOption)
|
|||||||
return services, nil
|
return services, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
m.RLock()
|
m.mu.RLock()
|
||||||
defer m.RUnlock()
|
defer m.mu.RUnlock()
|
||||||
|
|
||||||
// ensure the domain exists
|
// ensure the domain exists
|
||||||
services, ok := m.records[options.Namespace]
|
services, ok := m.records[options.Namespace]
|
||||||
@@ -371,9 +371,9 @@ func (m *memory) Watch(ctx context.Context, opts ...register.WatchOption) (regis
|
|||||||
wo: wo,
|
wo: wo,
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Lock()
|
m.mu.Lock()
|
||||||
m.watchers[w.id] = w
|
m.watchers[w.id] = w
|
||||||
m.Unlock()
|
m.mu.Unlock()
|
||||||
|
|
||||||
return w, nil
|
return w, nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -51,13 +51,13 @@ func (r *rpcHandler) Options() HandlerOptions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type noopServer struct {
|
type noopServer struct {
|
||||||
h Handler
|
h Handler
|
||||||
wg *sync.WaitGroup
|
wg *sync.WaitGroup
|
||||||
rsvc *register.Service
|
rsvc *register.Service
|
||||||
handlers map[string]Handler
|
handlers map[string]Handler
|
||||||
exit chan chan error
|
exit chan chan error
|
||||||
opts Options
|
opts Options
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
registered bool
|
registered bool
|
||||||
started bool
|
started bool
|
||||||
}
|
}
|
||||||
@@ -125,10 +125,10 @@ func (n *noopServer) String() string {
|
|||||||
|
|
||||||
//nolint:gocyclo
|
//nolint:gocyclo
|
||||||
func (n *noopServer) Register() error {
|
func (n *noopServer) Register() error {
|
||||||
n.RLock()
|
n.mu.RLock()
|
||||||
rsvc := n.rsvc
|
rsvc := n.rsvc
|
||||||
config := n.opts
|
config := n.opts
|
||||||
n.RUnlock()
|
n.mu.RUnlock()
|
||||||
|
|
||||||
// if service already filled, reuse it and return early
|
// if service already filled, reuse it and return early
|
||||||
if rsvc != nil {
|
if rsvc != nil {
|
||||||
@@ -144,9 +144,9 @@ func (n *noopServer) Register() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
n.RLock()
|
n.mu.RLock()
|
||||||
registered := n.registered
|
registered := n.registered
|
||||||
n.RUnlock()
|
n.mu.RUnlock()
|
||||||
|
|
||||||
if !registered {
|
if !registered {
|
||||||
if config.Logger.V(logger.InfoLevel) {
|
if config.Logger.V(logger.InfoLevel) {
|
||||||
@@ -164,8 +164,8 @@ func (n *noopServer) Register() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Lock()
|
n.mu.Lock()
|
||||||
defer n.Unlock()
|
defer n.mu.Unlock()
|
||||||
|
|
||||||
n.registered = true
|
n.registered = true
|
||||||
if cacheService {
|
if cacheService {
|
||||||
@@ -178,9 +178,9 @@ func (n *noopServer) Register() error {
|
|||||||
func (n *noopServer) Deregister() error {
|
func (n *noopServer) Deregister() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
n.RLock()
|
n.mu.RLock()
|
||||||
config := n.opts
|
config := n.opts
|
||||||
n.RUnlock()
|
n.mu.RUnlock()
|
||||||
|
|
||||||
service, err := NewRegisterService(n)
|
service, err := NewRegisterService(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -195,29 +195,29 @@ func (n *noopServer) Deregister() error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Lock()
|
n.mu.Lock()
|
||||||
n.rsvc = nil
|
n.rsvc = nil
|
||||||
|
|
||||||
if !n.registered {
|
if !n.registered {
|
||||||
n.Unlock()
|
n.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
n.registered = false
|
n.registered = false
|
||||||
|
|
||||||
n.Unlock()
|
n.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
//nolint:gocyclo
|
//nolint:gocyclo
|
||||||
func (n *noopServer) Start() error {
|
func (n *noopServer) Start() error {
|
||||||
n.RLock()
|
n.mu.RLock()
|
||||||
if n.started {
|
if n.started {
|
||||||
n.RUnlock()
|
n.mu.RUnlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
config := n.Options()
|
config := n.Options()
|
||||||
n.RUnlock()
|
n.mu.RUnlock()
|
||||||
|
|
||||||
// use 127.0.0.1 to avoid scan of all network interfaces
|
// use 127.0.0.1 to avoid scan of all network interfaces
|
||||||
addr, err := maddr.Extract("127.0.0.1")
|
addr, err := maddr.Extract("127.0.0.1")
|
||||||
@@ -235,11 +235,11 @@ func (n *noopServer) Start() error {
|
|||||||
config.Logger.Info(n.opts.Context, "server [noop] Listening on "+config.Address)
|
config.Logger.Info(n.opts.Context, "server [noop] Listening on "+config.Address)
|
||||||
}
|
}
|
||||||
|
|
||||||
n.Lock()
|
n.mu.Lock()
|
||||||
if len(config.Advertise) == 0 {
|
if len(config.Advertise) == 0 {
|
||||||
config.Advertise = config.Address
|
config.Advertise = config.Address
|
||||||
}
|
}
|
||||||
n.Unlock()
|
n.mu.Unlock()
|
||||||
|
|
||||||
// use RegisterCheck func before register
|
// use RegisterCheck func before register
|
||||||
// nolint: nestif
|
// nolint: nestif
|
||||||
@@ -273,9 +273,9 @@ func (n *noopServer) Start() error {
|
|||||||
select {
|
select {
|
||||||
// register self on interval
|
// register self on interval
|
||||||
case <-t.C:
|
case <-t.C:
|
||||||
n.RLock()
|
n.mu.RLock()
|
||||||
registered := n.registered
|
registered := n.registered
|
||||||
n.RUnlock()
|
n.mu.RUnlock()
|
||||||
rerr := config.RegisterCheck(config.Context)
|
rerr := config.RegisterCheck(config.Context)
|
||||||
// nolint: nestif
|
// nolint: nestif
|
||||||
if rerr != nil && registered {
|
if rerr != nil && registered {
|
||||||
@@ -332,29 +332,29 @@ func (n *noopServer) Start() error {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// mark the server as started
|
// mark the server as started
|
||||||
n.Lock()
|
n.mu.Lock()
|
||||||
n.started = true
|
n.started = true
|
||||||
n.Unlock()
|
n.mu.Unlock()
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *noopServer) Stop() error {
|
func (n *noopServer) Stop() error {
|
||||||
n.RLock()
|
n.mu.RLock()
|
||||||
if !n.started {
|
if !n.started {
|
||||||
n.RUnlock()
|
n.mu.RUnlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
n.RUnlock()
|
n.mu.RUnlock()
|
||||||
|
|
||||||
ch := make(chan error)
|
ch := make(chan error)
|
||||||
n.exit <- ch
|
n.exit <- ch
|
||||||
|
|
||||||
err := <-ch
|
err := <-ch
|
||||||
n.Lock()
|
n.mu.Lock()
|
||||||
n.rsvc = nil
|
n.rsvc = nil
|
||||||
n.started = false
|
n.started = false
|
||||||
n.Unlock()
|
n.mu.Unlock()
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
20
service.go
20
service.go
@@ -96,9 +96,9 @@ func RegisterHandler(s server.Server, h interface{}, opts ...server.HandlerOptio
|
|||||||
}
|
}
|
||||||
|
|
||||||
type service struct {
|
type service struct {
|
||||||
done chan struct{}
|
done chan struct{}
|
||||||
opts Options
|
opts Options
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
stopped bool
|
stopped bool
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -321,9 +321,9 @@ func (s *service) Health() bool {
|
|||||||
func (s *service) Start() error {
|
func (s *service) Start() error {
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
s.RLock()
|
s.mu.RLock()
|
||||||
config := s.opts
|
config := s.opts
|
||||||
s.RUnlock()
|
s.mu.RUnlock()
|
||||||
|
|
||||||
for _, cfg := range s.opts.Configs {
|
for _, cfg := range s.opts.Configs {
|
||||||
if cfg.Options().Struct == nil {
|
if cfg.Options().Struct == nil {
|
||||||
@@ -380,9 +380,9 @@ func (s *service) Start() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *service) Stop() error {
|
func (s *service) Stop() error {
|
||||||
s.RLock()
|
s.mu.RLock()
|
||||||
config := s.opts
|
config := s.opts
|
||||||
s.RUnlock()
|
s.mu.RUnlock()
|
||||||
|
|
||||||
if config.Loggers[0].V(logger.InfoLevel) {
|
if config.Loggers[0].V(logger.InfoLevel) {
|
||||||
config.Loggers[0].Info(s.opts.Context, fmt.Sprintf("stoppping [service] %s", s.Name()))
|
config.Loggers[0].Info(s.opts.Context, fmt.Sprintf("stoppping [service] %s", s.Name()))
|
||||||
@@ -457,13 +457,13 @@ func (s *service) Run() error {
|
|||||||
// notifyShutdown marks the service as stopped and closes the done channel.
|
// notifyShutdown marks the service as stopped and closes the done channel.
|
||||||
// It ensures the channel is closed only once, preventing multiple closures.
|
// It ensures the channel is closed only once, preventing multiple closures.
|
||||||
func (s *service) notifyShutdown() {
|
func (s *service) notifyShutdown() {
|
||||||
s.Lock()
|
s.mu.Lock()
|
||||||
if s.stopped {
|
if s.stopped {
|
||||||
s.Unlock()
|
s.mu.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
s.stopped = true
|
s.stopped = true
|
||||||
s.Unlock()
|
s.mu.Unlock()
|
||||||
|
|
||||||
close(s.done)
|
close(s.done)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ type memorySync struct {
|
|||||||
locks map[string]*memoryLock
|
locks map[string]*memoryLock
|
||||||
options Options
|
options Options
|
||||||
|
|
||||||
mtx gosync.RWMutex
|
mu gosync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type memoryLock struct {
|
type memoryLock struct {
|
||||||
@@ -74,7 +74,7 @@ func (m *memorySync) Options() Options {
|
|||||||
|
|
||||||
func (m *memorySync) Lock(id string, opts ...LockOption) error {
|
func (m *memorySync) Lock(id string, opts ...LockOption) error {
|
||||||
// lock our access
|
// lock our access
|
||||||
m.mtx.Lock()
|
m.mu.Lock()
|
||||||
|
|
||||||
var options LockOptions
|
var options LockOptions
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
@@ -90,11 +90,11 @@ func (m *memorySync) Lock(id string, opts ...LockOption) error {
|
|||||||
release: make(chan bool),
|
release: make(chan bool),
|
||||||
}
|
}
|
||||||
// unlock
|
// unlock
|
||||||
m.mtx.Unlock()
|
m.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
m.mtx.Unlock()
|
m.mu.Unlock()
|
||||||
|
|
||||||
// set wait time
|
// set wait time
|
||||||
var wait <-chan time.Time
|
var wait <-chan time.Time
|
||||||
@@ -124,12 +124,12 @@ lockLoop:
|
|||||||
// wait for the lock to be released
|
// wait for the lock to be released
|
||||||
select {
|
select {
|
||||||
case <-lk.release:
|
case <-lk.release:
|
||||||
m.mtx.Lock()
|
m.mu.Lock()
|
||||||
|
|
||||||
// someone locked before us
|
// someone locked before us
|
||||||
lk, ok = m.locks[id]
|
lk, ok = m.locks[id]
|
||||||
if ok {
|
if ok {
|
||||||
m.mtx.Unlock()
|
m.mu.Unlock()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -141,7 +141,7 @@ lockLoop:
|
|||||||
release: make(chan bool),
|
release: make(chan bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
m.mtx.Unlock()
|
m.mu.Unlock()
|
||||||
|
|
||||||
break lockLoop
|
break lockLoop
|
||||||
case <-ttl:
|
case <-ttl:
|
||||||
@@ -160,8 +160,8 @@ lockLoop:
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *memorySync) Unlock(id string) error {
|
func (m *memorySync) Unlock(id string) error {
|
||||||
m.mtx.Lock()
|
m.mu.Lock()
|
||||||
defer m.mtx.Unlock()
|
defer m.mu.Unlock()
|
||||||
|
|
||||||
lk, ok := m.locks[id]
|
lk, ok := m.locks[id]
|
||||||
// no lock exists
|
// no lock exists
|
||||||
|
|||||||
@@ -137,7 +137,7 @@ type cache struct {
|
|||||||
|
|
||||||
opts Options
|
opts Options
|
||||||
|
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type cacheEntry struct {
|
type cacheEntry struct {
|
||||||
@@ -171,7 +171,7 @@ func (c *cache) put(req string, res string) {
|
|||||||
ttl = c.opts.MaxCacheTTL
|
ttl = c.opts.MaxCacheTTL
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Lock()
|
c.mu.Lock()
|
||||||
if c.entries == nil {
|
if c.entries == nil {
|
||||||
c.entries = make(map[string]cacheEntry)
|
c.entries = make(map[string]cacheEntry)
|
||||||
}
|
}
|
||||||
@@ -207,7 +207,7 @@ func (c *cache) put(req string, res string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.opts.Meter.Counter(semconv.CacheItemsTotal, "type", "dns").Inc()
|
c.opts.Meter.Counter(semconv.CacheItemsTotal, "type", "dns").Inc()
|
||||||
c.Unlock()
|
c.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *cache) get(req string) (res string) {
|
func (c *cache) get(req string) (res string) {
|
||||||
@@ -219,8 +219,8 @@ func (c *cache) get(req string) (res string) {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
c.RLock()
|
c.mu.RLock()
|
||||||
defer c.RUnlock()
|
defer c.mu.RUnlock()
|
||||||
|
|
||||||
if c.entries == nil {
|
if c.entries == nil {
|
||||||
return ""
|
return ""
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ type dnsConn struct {
|
|||||||
ibuf bytes.Buffer
|
ibuf bytes.Buffer
|
||||||
obuf bytes.Buffer
|
obuf bytes.Buffer
|
||||||
|
|
||||||
sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type roundTripper func(ctx context.Context, req string) (res string, err error)
|
type roundTripper func(ctx context.Context, req string) (res string, err error)
|
||||||
@@ -42,15 +42,15 @@ func (c *dnsConn) Read(b []byte) (n int, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *dnsConn) Write(b []byte) (n int, err error) {
|
func (c *dnsConn) Write(b []byte) (n int, err error) {
|
||||||
c.Lock()
|
c.mu.Lock()
|
||||||
defer c.Unlock()
|
defer c.mu.Unlock()
|
||||||
return c.ibuf.Write(b)
|
return c.ibuf.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *dnsConn) Close() error {
|
func (c *dnsConn) Close() error {
|
||||||
c.Lock()
|
c.mu.Lock()
|
||||||
cancel := c.cancel
|
cancel := c.cancel
|
||||||
c.Unlock()
|
c.mu.Unlock()
|
||||||
|
|
||||||
if cancel != nil {
|
if cancel != nil {
|
||||||
cancel()
|
cancel()
|
||||||
@@ -78,9 +78,9 @@ func (c *dnsConn) SetDeadline(t time.Time) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *dnsConn) SetReadDeadline(t time.Time) error {
|
func (c *dnsConn) SetReadDeadline(t time.Time) error {
|
||||||
c.Lock()
|
c.mu.Lock()
|
||||||
c.deadline = t
|
c.deadline = t
|
||||||
c.Unlock()
|
c.mu.Unlock()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,8 +90,8 @@ func (c *dnsConn) SetWriteDeadline(_ time.Time) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *dnsConn) drainBuffers(b []byte) (string, int, error) {
|
func (c *dnsConn) drainBuffers(b []byte) (string, int, error) {
|
||||||
c.Lock()
|
c.mu.Lock()
|
||||||
defer c.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
// drain the output buffer
|
// drain the output buffer
|
||||||
if c.obuf.Len() > 0 {
|
if c.obuf.Len() > 0 {
|
||||||
@@ -119,8 +119,8 @@ func (c *dnsConn) drainBuffers(b []byte) (string, int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *dnsConn) fillBuffer(b []byte, str string) (int, error) {
|
func (c *dnsConn) fillBuffer(b []byte, str string) (int, error) {
|
||||||
c.Lock()
|
c.mu.Lock()
|
||||||
defer c.Unlock()
|
defer c.mu.Unlock()
|
||||||
c.obuf.WriteByte(byte(len(str) >> 8))
|
c.obuf.WriteByte(byte(len(str) >> 8))
|
||||||
c.obuf.WriteByte(byte(len(str)))
|
c.obuf.WriteByte(byte(len(str)))
|
||||||
c.obuf.WriteString(str)
|
c.obuf.WriteString(str)
|
||||||
@@ -128,8 +128,8 @@ func (c *dnsConn) fillBuffer(b []byte, str string) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *dnsConn) childContext() (context.Context, context.CancelFunc) {
|
func (c *dnsConn) childContext() (context.Context, context.CancelFunc) {
|
||||||
c.Lock()
|
c.mu.Lock()
|
||||||
defer c.Unlock()
|
defer c.mu.Unlock()
|
||||||
if c.ctx == nil {
|
if c.ctx == nil {
|
||||||
c.ctx, c.cancel = context.WithCancel(context.Background())
|
c.ctx, c.cancel = context.WithCancel(context.Background())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ type clientTracer struct {
|
|||||||
tr tracer.Tracer
|
tr tracer.Tracer
|
||||||
activeHooks map[string]context.Context
|
activeHooks map[string]context.Context
|
||||||
root tracer.Span
|
root tracer.Span
|
||||||
mtx sync.Mutex
|
mu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewClientTrace(ctx context.Context, tr tracer.Tracer) *httptrace.ClientTrace {
|
func NewClientTrace(ctx context.Context, tr tracer.Tracer) *httptrace.ClientTrace {
|
||||||
@@ -83,8 +83,8 @@ func NewClientTrace(ctx context.Context, tr tracer.Tracer) *httptrace.ClientTrac
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ct *clientTracer) start(hook, spanName string, attrs ...interface{}) {
|
func (ct *clientTracer) start(hook, spanName string, attrs ...interface{}) {
|
||||||
ct.mtx.Lock()
|
ct.mu.Lock()
|
||||||
defer ct.mtx.Unlock()
|
defer ct.mu.Unlock()
|
||||||
|
|
||||||
if hookCtx, found := ct.activeHooks[hook]; !found {
|
if hookCtx, found := ct.activeHooks[hook]; !found {
|
||||||
var sp tracer.Span
|
var sp tracer.Span
|
||||||
@@ -104,8 +104,8 @@ func (ct *clientTracer) start(hook, spanName string, attrs ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ct *clientTracer) end(hook string, err error, attrs ...interface{}) {
|
func (ct *clientTracer) end(hook string, err error, attrs ...interface{}) {
|
||||||
ct.mtx.Lock()
|
ct.mu.Lock()
|
||||||
defer ct.mtx.Unlock()
|
defer ct.mu.Unlock()
|
||||||
if ctx, ok := ct.activeHooks[hook]; ok { // nolint:nestif
|
if ctx, ok := ct.activeHooks[hook]; ok { // nolint:nestif
|
||||||
if span, ok := tracer.SpanFromContext(ctx); ok {
|
if span, ok := tracer.SpanFromContext(ctx); ok {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -136,8 +136,8 @@ func (ct *clientTracer) getParentContext(hook string) context.Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (ct *clientTracer) span(hook string) (tracer.Span, bool) {
|
func (ct *clientTracer) span(hook string) (tracer.Span, bool) {
|
||||||
ct.mtx.Lock()
|
ct.mu.Lock()
|
||||||
defer ct.mtx.Unlock()
|
defer ct.mu.Unlock()
|
||||||
if ctx, ok := ct.activeHooks[hook]; ok {
|
if ctx, ok := ct.activeHooks[hook]; ok {
|
||||||
return tracer.SpanFromContext(ctx)
|
return tracer.SpanFromContext(ctx)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ type Buffer struct {
|
|||||||
vals []*Entry
|
vals []*Entry
|
||||||
size int
|
size int
|
||||||
|
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
// Entry is ring buffer data entry
|
// Entry is ring buffer data entry
|
||||||
@@ -35,8 +35,8 @@ type Stream struct {
|
|||||||
|
|
||||||
// Put adds a new value to ring buffer
|
// Put adds a new value to ring buffer
|
||||||
func (b *Buffer) Put(v interface{}) {
|
func (b *Buffer) Put(v interface{}) {
|
||||||
b.Lock()
|
b.mu.Lock()
|
||||||
defer b.Unlock()
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
// append to values
|
// append to values
|
||||||
entry := &Entry{
|
entry := &Entry{
|
||||||
@@ -63,8 +63,8 @@ func (b *Buffer) Put(v interface{}) {
|
|||||||
|
|
||||||
// Get returns the last n entries
|
// Get returns the last n entries
|
||||||
func (b *Buffer) Get(n int) []*Entry {
|
func (b *Buffer) Get(n int) []*Entry {
|
||||||
b.RLock()
|
b.mu.RLock()
|
||||||
defer b.RUnlock()
|
defer b.mu.RUnlock()
|
||||||
|
|
||||||
// reset any invalid values
|
// reset any invalid values
|
||||||
if n > len(b.vals) || n < 0 {
|
if n > len(b.vals) || n < 0 {
|
||||||
@@ -80,8 +80,8 @@ func (b *Buffer) Get(n int) []*Entry {
|
|||||||
|
|
||||||
// Since returns the entries since a specific time
|
// Since returns the entries since a specific time
|
||||||
func (b *Buffer) Since(t time.Time) []*Entry {
|
func (b *Buffer) Since(t time.Time) []*Entry {
|
||||||
b.RLock()
|
b.mu.RLock()
|
||||||
defer b.RUnlock()
|
defer b.mu.RUnlock()
|
||||||
|
|
||||||
// return all the values
|
// return all the values
|
||||||
if t.IsZero() {
|
if t.IsZero() {
|
||||||
@@ -109,8 +109,8 @@ func (b *Buffer) Since(t time.Time) []*Entry {
|
|||||||
// Stream logs from the buffer
|
// Stream logs from the buffer
|
||||||
// Close the channel when you want to stop
|
// Close the channel when you want to stop
|
||||||
func (b *Buffer) Stream() (<-chan *Entry, chan bool) {
|
func (b *Buffer) Stream() (<-chan *Entry, chan bool) {
|
||||||
b.Lock()
|
b.mu.Lock()
|
||||||
defer b.Unlock()
|
defer b.mu.Unlock()
|
||||||
|
|
||||||
entries := make(chan *Entry, 128)
|
entries := make(chan *Entry, 128)
|
||||||
id := id.MustNew()
|
id := id.MustNew()
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type stream struct {
|
|||||||
err error
|
err error
|
||||||
request *request
|
request *request
|
||||||
|
|
||||||
sync.RWMutex
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
type request struct {
|
type request struct {
|
||||||
@@ -57,9 +57,9 @@ func (s *stream) Request() server.Request {
|
|||||||
func (s *stream) Send(v interface{}) error {
|
func (s *stream) Send(v interface{}) error {
|
||||||
err := s.Stream.SendMsg(v)
|
err := s.Stream.SendMsg(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Lock()
|
s.mu.Lock()
|
||||||
s.err = err
|
s.err = err
|
||||||
s.Unlock()
|
s.mu.Unlock()
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -68,17 +68,17 @@ func (s *stream) Send(v interface{}) error {
|
|||||||
func (s *stream) Recv(v interface{}) error {
|
func (s *stream) Recv(v interface{}) error {
|
||||||
err := s.Stream.RecvMsg(v)
|
err := s.Stream.RecvMsg(v)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.Lock()
|
s.mu.Lock()
|
||||||
s.err = err
|
s.err = err
|
||||||
s.Unlock()
|
s.mu.Unlock()
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Error returns error that stream holds
|
// Error returns error that stream holds
|
||||||
func (s *stream) Error() error {
|
func (s *stream) Error() error {
|
||||||
s.RLock()
|
s.mu.RLock()
|
||||||
defer s.RUnlock()
|
defer s.mu.RUnlock()
|
||||||
return s.err
|
return s.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user