Import path:
gitlab.soludian.com/soludian/fountain/libs/fnet/endpoint
endpoint
import "gitlab.soludian.com/soludian/fountain/libs/fnet/endpoint"Index
func Nop
func Nop(context.Context, any) (any, error)Nop là một endpoint không làm gì và trả về nil error. Hữu ích cho việc viết tests.
type Endpoint
Endpoint là building block cơ bản cho cả server và client trong RPC endpoint chain. Đại diện cho một RPC method hoặc lời gọi RPC đơn lẻ.
type Endpoint func(ctx context.Context, request any) (response any, err error)type FailInf
FailInf có thể được implement bởi các response types của Fountain chứa thông tin lỗi business logic. Nếu Failed() trả về non-nil error, tầng server của Fountain sẽ hiểu đây là lỗi business logic và có thể encode khác so với response thành công thông thường.
Không bắt buộc response types phải implement FailInf, nhưng hữu ích cho các use cases phức tạp hơn ở phía server.
type FailInf interface {
Failed() error
}type Middleware
Middleware là một behavior modifier có thể chain cho server-side endpoints. Sử dụng để compose các cross-cutting concerns như circuit breaker, rate limiting, tracing, logging cho RPC/server endpoints.
type Middleware func(Endpoint) Endpointfunc Chain
func Chain(outer Middleware, others ...Middleware) MiddlewareChain là hàm helper để compose nhiều middlewares cho server-side endpoint. Requests sẽ đi qua các middleware theo thứ tự khai báo, middleware đầu tiên được coi là outermost middleware.
Example
package main
import (
"context"
"fmt"
"gitlab.soludian.com/soludian/fountain/libs/fnet/endpoint"
)
func main() {
e := endpoint.Chain(
annotate("first"),
annotate("second"),
annotate("third"),
)(myEndpoint)
if _, err := e(ctx, req); err != nil {
panic(err)
}
}
var (
ctx = context.Background()
req = struct{}{}
)
func annotate(s string) endpoint.Middleware {
return func(next endpoint.Endpoint) endpoint.Endpoint {
return func(ctx context.Context, request any) (any, error) {
fmt.Println(s, "pre")
defer fmt.Println(s, "post")
return next(ctx, request)
}
}
}
func myEndpoint(context.Context, any) (any, error) {
fmt.Println("my endpoint!")
return struct{}{}, nil
}Output
first pre
second pre
third pre
my endpoint!
third post
second post
first postGenerated by gomarkdoc