Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue 58 #72

Merged
merged 3 commits into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 72 additions & 13 deletions balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License.
*/

package grpcpolaris

Check warning on line 18 in balancer.go

View workflow job for this annotation

GitHub Actions / Run Revive Action (1.18.x)

should have a package comment

import (
"encoding/json"
Expand Down Expand Up @@ -117,9 +117,10 @@
subConns map[string]balancer.SubConn
scStates map[balancer.SubConn]connectivity.State

v2Picker balancer.Picker
consumerAPI polaris.ConsumerAPI
routerAPI polaris.RouterAPI
v2Picker balancer.Picker
consumerAPI polaris.ConsumerAPI
routerAPI polaris.RouterAPI
circuitBreakerAPI polaris.CircuitBreakerAPI

lbCfg *LBConfig

Expand Down Expand Up @@ -207,6 +208,7 @@
if nil == p.consumerAPI {
p.consumerAPI = polaris.NewConsumerAPIByContext(p.options.SDKContext)
p.routerAPI = polaris.NewRouterAPIByContext(p.options.SDKContext)
p.circuitBreakerAPI = polaris.NewCircuitBreakerAPIByContext(p.options.SDKContext)
}
// Successful resolution; clear resolver error and ensure we return nil.
p.resolverErr = nil
Expand Down Expand Up @@ -436,11 +438,18 @@
subSc, ok := pnp.readySCs[addr]
if ok {
reporter := &resultReporter{
method: info.FullMethodName,
instance: targetInstance,
consumerAPI: pnp.balancer.consumerAPI,
startTime: time.Now(),
sourceService: sourceService,
method: info.FullMethodName,
instance: targetInstance,
consumerAPI: pnp.balancer.consumerAPI,
circuitBreakerAPI: pnp.balancer.circuitBreakerAPI,
startTime: time.Now(),
sourceService: sourceService,
namespace: pnp.options.Namespace,
service: pnp.balancer.host,
circuitBreaker: false,
}
if pnp.options.CircuitBreaker {
reporter.circuitBreaker = true
}

return balancer.PickResult{
Expand Down Expand Up @@ -544,11 +553,15 @@
}

type resultReporter struct {
method string
instance model.Instance
consumerAPI polaris.ConsumerAPI
startTime time.Time
sourceService *model.ServiceInfo
method string
namespace string
service string
instance model.Instance
consumerAPI polaris.ConsumerAPI
circuitBreakerAPI polaris.CircuitBreakerAPI
startTime time.Time
sourceService *model.ServiceInfo
circuitBreaker bool
}

func (r *resultReporter) report(info balancer.DoneInfo) {
Expand All @@ -567,4 +580,50 @@
if err := r.consumerAPI.UpdateServiceCallResult(callResult); err != nil {
GetLogger().Error("[Polaris][Balancer] report grpc call info fail : %+v", err)
}
if r.circuitBreaker {
if err := r.reportCircuitBreak(r.instance, retStatus, strconv.Itoa(int(code)), r.startTime); err != nil {
GetLogger().Error("[Polaris][Balancer] report grpc circuit breaker info fail : %+v", err)
}
}
}

func (r *resultReporter) reportCircuitBreak(instance model.Instance, status model.RetStatus,

Check warning on line 590 in balancer.go

View workflow job for this annotation

GitHub Actions / golangci-lint (1.15.x)

import-shadowing: The name 'status' shadows an import name (revive)
retCode string, start time.Time) error {

caller := &model.ServiceKey{}
if r.sourceService != nil {
caller.Service = r.sourceService.Service
caller.Namespace = r.sourceService.Namespace
}

insRes, err := model.NewInstanceResource(&model.ServiceKey{
Namespace: r.namespace,
Service: r.service,
}, caller, "http", instance.GetHost(), instance.GetPort())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 protocol 应该是 instance.GetProtocol() ? 如果没有默认应该是 grpc?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个 protocol 应该是 instance.GetProtocol() ? 如果没有默认应该是 grpc?

已修复

if err != nil {
return fmt.Errorf("report circuitBreaker for service %v get instance resource failed: %v",
insRes, err)
}

GetLogger().Debug("report circuitBreaker status [%v] code [%s] for instance %s/%s:%d "+
"caller [%s] "+
"delay [%v] "+
"circuitBreaker status [%v]\n",
status, retCode,
instance.GetService(),
instance.GetHost(), instance.GetPort(),
r.service, time.Since(start),
instance.GetCircuitBreakerStatus())

if err := r.circuitBreakerAPI.Report(&model.ResourceStat{
Delay: time.Since(start),
RetStatus: status,
RetCode: retCode,
Resource: insRes,
}); err != nil {
return fmt.Errorf("report circuitBreaker for service %v failed: %v",
insRes, err)
}

return nil
}
1 change: 1 addition & 0 deletions examples/circuitbreak/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* specific language governing permissions and limitations under the License.
*/

package main

Check warning on line 18 in examples/circuitbreak/consumer/main.go

View workflow job for this annotation

GitHub Actions / Run Revive Action (1.17.x)

should have a package comment

Check warning on line 18 in examples/circuitbreak/consumer/main.go

View workflow job for this annotation

GitHub Actions / Run Revive Action (1.18.x)

should have a package comment

import (
"context"
Expand Down Expand Up @@ -52,6 +52,7 @@
conn, err := polaris.DialContext(ctx, "polaris://CircuitBreakerEchoServerGRPC/",
polaris.WithGRPCDialOptions(grpc.WithTransportCredentials(insecure.NewCredentials())),
polaris.WithEnableCircuitBreaker(),
polaris.WithClientNamespace("default"),
)
if err != nil {
log.Fatal(err)
Expand Down
Loading