add p2c peak ewma load balancer

This commit is contained in:
kevin
2020-08-06 20:55:38 +08:00
parent c7544d0e60
commit 63583d4744
24 changed files with 304 additions and 282 deletions

View File

@@ -18,20 +18,13 @@ import (
func gracefulHandler(ctx *svc.ServiceContext) http.HandlerFunc {
logger := executors.NewLessExecutor(time.Second)
return func(w http.ResponseWriter, r *http.Request) {
var resp types.Response
conn, ok := ctx.Client.Next()
if !ok {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
host, err := os.Hostname()
if err != nil {
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
return
}
conn := ctx.Client.Conn()
client := graceful.NewGraceServiceClient(conn)
rp, err := client.Grace(context.Background(), &graceful.Request{From: host})
if err != nil {
@@ -40,6 +33,7 @@ func gracefulHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return
}
var resp types.Response
resp.Host = rp.Host
logger.DoOrDiscard(func() {
fmt.Printf("%s from host: %s\n", time.Now().Format("15:04:05"), rp.Host)

View File

@@ -18,20 +18,13 @@ import (
func gracefulHandler(ctx *svc.ServiceContext) http.HandlerFunc {
logger := executors.NewLessExecutor(time.Second)
return func(w http.ResponseWriter, r *http.Request) {
var resp types.Response
conn, ok := ctx.Client.Next()
if !ok {
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
return
}
host, err := os.Hostname()
if err != nil {
http.Error(w, http.StatusText(http.StatusNotImplemented), http.StatusNotImplemented)
return
}
conn := ctx.Client.Conn()
client := graceful.NewGraceServiceClient(conn)
rp, err := client.Grace(context.Background(), &graceful.Request{From: host})
if err != nil {
@@ -40,6 +33,7 @@ func gracefulHandler(ctx *svc.ServiceContext) http.HandlerFunc {
return
}
var resp types.Response
resp.Host = rp.Host
logger.DoOrDiscard(func() {
fmt.Printf("%s from host: %s\n", time.Now().Format("15:04:05"), rp.Host)

View File

@@ -28,12 +28,7 @@ func main() {
for {
select {
case <-ticker.C:
conn, ok := client.Next()
if !ok {
time.Sleep(time.Second)
break
}
conn := client.Conn()
greet := unary.NewGreeterClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
resp, err := greet.Greet(ctx, &unary.Request{

View File

@@ -0,0 +1,37 @@
package main
import (
"context"
"fmt"
"time"
"zero/core/discov"
"zero/example/rpc/remote/unary"
"zero/rpcx"
)
func main() {
cli := rpcx.MustNewClient(rpcx.RpcClientConf{
Etcd: discov.EtcdConf{
Hosts: []string{"localhost:2379"},
Key: "rpcx",
},
})
greet := unary.NewGreeterClient(cli.Conn())
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
resp, err := greet.Greet(context.Background(), &unary.Request{
Name: "kevin",
})
if err != nil {
fmt.Println("X", err.Error())
} else {
fmt.Println("=>", resp.Greet)
}
}
}
}

View File

@@ -26,11 +26,7 @@ func main() {
log.Fatal(err)
}
conn, ok := client.Next()
if !ok {
log.Fatal("no server")
}
conn := client.Conn()
greet := stream.NewStreamGreeterClient(conn)
stm, err := greet.Greet(context.Background())
if err != nil {

View File

@@ -4,7 +4,6 @@ import (
"context"
"flag"
"fmt"
"log"
"time"
"zero/core/conf"
@@ -25,11 +24,7 @@ func main() {
for {
select {
case <-ticker.C:
conn, ok := client.Next()
if !ok {
log.Fatal("no server")
}
conn := client.Conn()
greet := unary.NewGreeterClient(conn)
resp, err := greet.Greet(context.Background(), &unary.Request{
Name: "kevin",

View File

@@ -1,17 +1,13 @@
{
"Name": "rpc.unary",
"MetricsUrl": "http://localhost:2222/add",
"Log": {
"Mode": "volume"
},
"ListenOn": "localhost:3457",
"Auth": false,
"Etcd": {
"Hosts": [
"localhost:2379"
],
"Key": "rpcx"
},
"Redis": {
"Host": "localhost:6379",
"Type": "node",
"Key": "apps"
}
}

View File

@@ -2,7 +2,6 @@ package main
import (
"flag"
"log"
"net/http"
"zero/core/conf"
@@ -20,11 +19,7 @@ var (
)
func handle(w http.ResponseWriter, r *http.Request) {
conn, ok := client.Next()
if !ok {
log.Fatal("no server")
}
conn := client.Conn()
greet := portal.NewPortalClient(conn)
resp, err := greet.Portal(r.Context(), &portal.PortalRequest{
Name: "kevin",

View File

@@ -2,7 +2,6 @@ package main
import (
"context"
"errors"
"flag"
"zero/core/conf"
@@ -33,11 +32,7 @@ func NewPortalServer(client *rpcx.RpcClient) *PortalServer {
}
func (gs *PortalServer) Portal(ctx context.Context, req *portal.PortalRequest) (*portal.PortalResponse, error) {
conn, ok := gs.userRpc.Next()
if !ok {
return nil, errors.New("internal error")
}
conn := gs.userRpc.Conn()
greet := user.NewUserClient(conn)
resp, err := greet.GetGrade(ctx, &user.UserRequest{
Name: req.Name,