Customized Access Control

The Kitex framework provides a middleware builder to support adding customized access control that rejects requests under certain conditions. The belowing is a simple example that randomly rejects 1% of all requests:

package myaccesscontrol

import (

var errRejected = errors.New("1% rejected")

// Implements a judge function.
func reject1percent(ctx context.Context, request interface{}) (reason error) {
    if rand.Intn(100) == 0 {
        return errRejected // an error should be returned when a request is rejected
    return nil

var MyMiddleware = acl.NewACLMiddleware(reject1percent) // create the middleware

Then, you can enable this middleware with WithMiddleware(myaccesscontrol.MyMiddleware) at the creation of a client or a server.

Last modified December 2, 2022 : chore: add lark footer link (#460) (f41a884)