Giter VIP home page Giter VIP logo

Comments (8)

OnlyPiglet avatar OnlyPiglet commented on June 13, 2024 2

may I have a try for this,write a go wasm plugin

from higress.

johnlanni avatar johnlanni commented on June 13, 2024 1

We will implement this functionality through plugins instead of annotations

This feature can be implemented by using routeCluster in the plugin and using it with httpcall.
Interested developers are welcome to contribute.

reference:
routeCluster:

type RouteCluster struct {

httpCall:
func HttpCall(cluster Cluster, method, path string, headers [][2]string, body []byte,

from higress.

OnlyPiglet avatar OnlyPiglet commented on June 13, 2024 1

@haifzhu
will finished this weekend, thanks a lot.

from higress.

johnlanni avatar johnlanni commented on June 13, 2024 1

@haifzhu 有点问题,应该在回调里迭代处理下一个trypath

from higress.

haifzhu avatar haifzhu commented on June 13, 2024

@OnlyPiglet Are you finished the feature, i also have the need for it。

from higress.

haifzhu avatar haifzhu commented on June 13, 2024

package main

import (
    "errors"
    "net/http"
    "strings"

    "github.com/alibaba/higress/plugins/wasm-go/pkg/wrapper"
    "github.com/higress-group/proxy-wasm-go-sdk/proxywasm"
    "github.com/higress-group/proxy-wasm-go-sdk/proxywasm/types"
    "github.com/tidwall/gjson"
)

var defaultTimeout uint32 = 60000

func main() {
    wrapper.SetCtx(
        "try-paths",
        wrapper.ParseConfigBy(parseConfig),
        wrapper.ProcessRequestHeadersBy(onHttpRequestHeaders),
    )
}

// 自定义插件配置
type TryPathsConfig struct {
    host     string
    port     int64
    tryPaths []string
    code     int
    client   wrapper.HttpClient
}

func Client(json gjson.Result) (wrapper.HttpClient, error) {
    serviceSource := json.Get("serviceSource").String()
    serviceName := json.Get("serviceName").String()
    servicePort := json.Get("servicePort").Int()
    if serviceName == "" || servicePort == 0 {
        return nil, errors.New("invalid service config")
    }
    switch serviceSource {
    case "k8s":
        namespace := json.Get("namespace").String()
        return wrapper.NewClusterClient(wrapper.K8sCluster{
            ServiceName: serviceName,
            Namespace:   namespace,
            Port:        servicePort,
        }), nil
    case "nacos":
        namespace := json.Get("namespace").String()
        return wrapper.NewClusterClient(wrapper.NacosCluster{
            ServiceName: serviceName,
            NamespaceID: namespace,
            Port:        servicePort,
        }), nil
    case "ip":
        return wrapper.NewClusterClient(wrapper.StaticIpCluster{
            ServiceName: serviceName,
            Port:        servicePort,
        }), nil
    case "dns":
        domain := json.Get("domain").String()
        return wrapper.NewClusterClient(wrapper.DnsCluster{
            ServiceName: serviceName,
            Port:        servicePort,
            Domain:      domain,
        }), nil
    default:
        return nil, errors.New("unknown service source: " + serviceSource)
    }
}

func parseConfig(json gjson.Result, config *TryPathsConfig, log wrapper.Log) error {
    // 解析出配置,更新到config中
    for _, result := range json.Get("tryPaths").Array() {
        config.tryPaths = append(config.tryPaths, result.String())
    }

    // code默认值为404
    if json.Get("code").String() == "" {
        config.code = http.StatusNotFound
    } else {
        config.code = int(json.Get("code").Int())
    }
    client, err := Client(json)
    if err != nil {
        return err
    }
    config.client = client
    return nil
}

func convHttpHeadersToStruct(responseHeaders http.Header) [][2]string {
    headerStruct := make([][2]string, len(responseHeaders))
    i := 0
    for key, values := range responseHeaders {
        headerStruct[i][0] = key
        headerStruct[i][1] = values[0]
        i++
    }
    return headerStruct

}

func onHttpRequestHeaders(ctx wrapper.HttpContext, config TryPathsConfig, log wrapper.Log) types.Action {
    path := ctx.Path()
    log.Infof("onHttpRequestHeaders path: %s, config: %s", path, config)
    for _, requestPath := range config.tryPaths {
        requestPath = strings.Replace(requestPath, "$uri", path, -1)
        config.client.Get(requestPath, nil,
            func(statusCode int, responseHeaders http.Header, responseBody []byte) {
                if statusCode != config.code {
                    proxywasm.SendHttpResponse(uint32(statusCode), convHttpHeadersToStruct(responseHeaders), responseBody, -1)
                    return
                }
                proxywasm.ResumeHttpRequest()
            }, defaultTimeout)
        return types.ActionPause
    }
    // 需要等待异步回调完成,返回Pause状态,可以被ResumeHttpRequest恢复
    return types.ActionPause
}

from higress.

haifzhu avatar haifzhu commented on June 13, 2024

主逻辑是不是这样的?

from higress.

haifzhu avatar haifzhu commented on June 13, 2024

可以添加一个nginx的proxy_cache 缓存功能做请求容灾

from higress.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.