上传选择器锁处理
This commit is contained in:
parent
253386266f
commit
39230faea3
@ -49,6 +49,7 @@ type Service struct {
|
|||||||
options *Options
|
options *Options
|
||||||
metadata string
|
metadata string
|
||||||
server *server.Server
|
server *server.Server
|
||||||
|
selectormutex sync.RWMutex
|
||||||
selectors map[string]ISelector
|
selectors map[string]ISelector
|
||||||
clientmutex sync.RWMutex
|
clientmutex sync.RWMutex
|
||||||
clients map[string]net.Conn
|
clients map[string]net.Conn
|
||||||
@ -266,16 +267,22 @@ func (this *Service) PostReadRequest(ctx context.Context, r *protocol.Message, e
|
|||||||
if isCallMessage := (r.MessageType() == protocol.Request); isCallMessage {
|
if isCallMessage := (r.MessageType() == protocol.Request); isCallMessage {
|
||||||
var (
|
var (
|
||||||
stag string
|
stag string
|
||||||
selector client.Selector
|
selector ISelector
|
||||||
ok bool
|
ok bool
|
||||||
)
|
)
|
||||||
req_metadata := r.Metadata
|
req_metadata := r.Metadata
|
||||||
this.Debugf("PreReadRequest ServicePath:%s ServicePath:%s Metadata:%v ", r.ServicePath, r.ServiceMethod, r.Metadata)
|
this.Debugf("PreReadRequest ServicePath:%s ServicePath:%s Metadata:%v ", r.ServicePath, r.ServiceMethod, r.Metadata)
|
||||||
if stag, ok = req_metadata[ServiceClusterTag]; ok {
|
if stag, ok = req_metadata[ServiceClusterTag]; ok {
|
||||||
if selector, ok = this.selectors[stag]; !ok {
|
this.selectormutex.RLock()
|
||||||
this.selectors[stag] = newSelector(nil)
|
selector, ok = this.selectors[stag]
|
||||||
selector = this.selectors[stag]
|
this.selectormutex.RUnlock()
|
||||||
|
if !ok {
|
||||||
|
selector = newSelector(nil)
|
||||||
|
this.selectormutex.Lock()
|
||||||
|
this.selectors[stag] = selector
|
||||||
|
this.selectormutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
if addr, ok := req_metadata[ServiceAddrKey]; ok {
|
if addr, ok := req_metadata[ServiceAddrKey]; ok {
|
||||||
if _, ok = this.clientmeta[addr]; !ok {
|
if _, ok = this.clientmeta[addr]; !ok {
|
||||||
if smeta, ok := req_metadata[ServiceMetaKey]; ok {
|
if smeta, ok := req_metadata[ServiceMetaKey]; ok {
|
||||||
@ -405,8 +412,12 @@ func (this *Service) call(ctx context.Context, clusterTag string, servicePath st
|
|||||||
ServiceAddrKey: "tcp@" + this.options.ServiceAddr,
|
ServiceAddrKey: "tcp@" + this.options.ServiceAddr,
|
||||||
ServiceMetaKey: this.metadata,
|
ServiceMetaKey: this.metadata,
|
||||||
})
|
})
|
||||||
if selector, ok = this.selectors[clusterTag]; !ok {
|
this.selectormutex.RLock()
|
||||||
|
selector, ok = this.selectors[clusterTag]
|
||||||
|
this.selectormutex.RUnlock()
|
||||||
|
if !ok {
|
||||||
err = fmt.Errorf("on found serviceTag:%s", clusterTag)
|
err = fmt.Errorf("on found serviceTag:%s", clusterTag)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
this.clientmutex.RLock()
|
this.clientmutex.RLock()
|
||||||
if clientaddr = selector.Select(ctx, spath[0], serviceMethod, args); clientaddr == "" {
|
if clientaddr = selector.Select(ctx, spath[0], serviceMethod, args); clientaddr == "" {
|
||||||
@ -462,7 +473,10 @@ func (this *Service) broadcast(ctx context.Context, clusterTag string, servicePa
|
|||||||
ServiceAddrKey: "tcp@" + this.options.ServiceAddr,
|
ServiceAddrKey: "tcp@" + this.options.ServiceAddr,
|
||||||
ServiceMetaKey: this.metadata,
|
ServiceMetaKey: this.metadata,
|
||||||
})
|
})
|
||||||
if selector, ok = this.selectors[clusterTag]; !ok {
|
this.selectormutex.RLock()
|
||||||
|
selector, ok = this.selectors[clusterTag]
|
||||||
|
this.selectormutex.RUnlock()
|
||||||
|
if !ok {
|
||||||
err = fmt.Errorf("on found serviceTag:%s", clusterTag)
|
err = fmt.Errorf("on found serviceTag:%s", clusterTag)
|
||||||
}
|
}
|
||||||
if clientaddrs = selector.Find(ctx, spath[0], serviceMethod, args); clientaddrs == nil || len(clientaddrs) == 0 {
|
if clientaddrs = selector.Find(ctx, spath[0], serviceMethod, args); clientaddrs == nil || len(clientaddrs) == 0 {
|
||||||
@ -556,6 +570,7 @@ func (this *Service) clusterbroadcast(ctx context.Context, servicePath string, s
|
|||||||
ServiceMetaKey: this.metadata,
|
ServiceMetaKey: this.metadata,
|
||||||
})
|
})
|
||||||
addrs := make(map[string]struct{})
|
addrs := make(map[string]struct{})
|
||||||
|
this.selectormutex.RLock()
|
||||||
for _, v := range this.selectors {
|
for _, v := range this.selectors {
|
||||||
if clientaddrs = v.Find(ctx, spath[0], serviceMethod, args); clientaddrs != nil && len(clientaddrs) >= 0 {
|
if clientaddrs = v.Find(ctx, spath[0], serviceMethod, args); clientaddrs != nil && len(clientaddrs) >= 0 {
|
||||||
for _, v1 := range clientaddrs {
|
for _, v1 := range clientaddrs {
|
||||||
@ -563,6 +578,7 @@ func (this *Service) clusterbroadcast(ctx context.Context, servicePath string, s
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this.selectormutex.RUnlock()
|
||||||
if len(addrs) == 0 {
|
if len(addrs) == 0 {
|
||||||
err = fmt.Errorf("on found service:%s", spath[0])
|
err = fmt.Errorf("on found service:%s", spath[0])
|
||||||
return
|
return
|
||||||
|
Loading…
Reference in New Issue
Block a user