-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add http resolver & balancer Co-authored-by: chenzhihui <[email protected]>
- Loading branch information
Showing
7 changed files
with
288 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package balancer | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-kratos/kratos/v2/registry" | ||
) | ||
|
||
// DoneInfo is callback when rpc done | ||
type DoneInfo struct { | ||
Err error | ||
Trailer map[string]string | ||
} | ||
|
||
// Balancer is node pick balancer | ||
type Balancer interface { | ||
Pick(ctx context.Context, pathPattern string, nodes []*registry.ServiceInstance) (node *registry.ServiceInstance, done func(DoneInfo), err error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package random | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
|
||
"github.com/go-kratos/kratos/v2/internal/balancer" | ||
"github.com/go-kratos/kratos/v2/registry" | ||
) | ||
|
||
var _ balancer.Balancer = &Balancer{} | ||
|
||
type Balancer struct { | ||
} | ||
|
||
func New() *Balancer { | ||
return &Balancer{} | ||
} | ||
|
||
func (b *Balancer) Pick(ctx context.Context, pathPattern string, nodes []*registry.ServiceInstance) (node *registry.ServiceInstance, done func(balancer.DoneInfo), err error) { | ||
if len(nodes) == 0 { | ||
return nil, nil, fmt.Errorf("no instances avaiable") | ||
} else if len(nodes) == 1 { | ||
return nodes[0], func(di balancer.DoneInfo) {}, nil | ||
} | ||
idx := rand.Intn(len(nodes)) | ||
return nodes[idx], func(di balancer.DoneInfo) {}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.