SoFunction
Updated on 2025-03-01

Efficient ban: Use Go packaging function to improve ban operation efficiency

This article is a banned business, and will talk about the implementation of banned business and add new functions to the library/framework.

Source code:/weloe/token-go

Ideas

Crowding business is also a common business in general projects. We also encapsulate it in the library as one of the features.

We also use adapter as the underlying storage structure and store the ban mark as a k-v structure.

Use the id and service name service as keys, and the blocked level level as value, so that we can implement some more common blocked services.

accomplish

The first thing is the method of banning. Of course, you need to verify the parameters and then store the banning flag. It should be noted that the level must be greater than or equal to 1, and we must also call our logger and watcher after blocking.

/weloe/token-go/blob/8bf577c0309332cb42d17e33b435f06fb74d8e7b/#L390

// Banned ban user, if time == 0,the timeout is not set
func (e *Enforcer) Banned(id string, service string, level int, time int64) error {
	if id == "" || service == "" {
		return ("parameter cannot be nil")
	}
	if level < 1 {
		return ("unexpected level = %v, level must large or equal 1", level)
	}
	err := ((id, service), (level), time)
	if err != nil {
		return err
	}
	// callback
	(, id, service, level, time)
	if  != nil {
		(, id, service, level, time)
	}
	return nil
}

Next is the implementation of unblocking. It is very simple. Just delete the record we blocked.

/weloe/token-go/blob/8bf577c0309332cb42d17e33b435f06fb74d8e7b/#LL412C6-L412C6

// UnBanned Unblock user account
func (e *Enforcer) UnBanned(id string, services ...string) error {
	if id == "" {
		return ("parmeter id can not be nil")
	}
	if len(services) == 0 {
		return ("parmeter services length can not be 0")
	}
	for _, service := range services {
		err := ((id, service))
		if err != nil {
			return err
		}
		(, id, service)
		if  != nil {
			(, id, service)
		}
	}
	return nil
}

Then there are several convenient methods to provide

Three methods are used to determine whether it is banned, obtain the level of ban, and obtain the remaining time of ban.

/weloe/token-go/blob/8bf577c0309332cb42d17e33b435f06fb74d8e7b/#L434

Determine whether it is banned

// IsBanned if banned return true, else return false
func (e *Enforcer) IsBanned(id string, services string) bool {
	s := ((id, services))
	return s != ""
}

/weloe/token-go/blob/8bf577c0309332cb42d17e33b435f06fb74d8e7b/#L441

Obtain the level of ban

// GetBannedLevel get banned level
func (e *Enforcer) GetBannedLevel(id string, service string) (int64, error) {
	str := ((id, service))
	if str == "" {
		return 0, ("loginId = %v, service = %v is not banned", id, service)
	}
	time, err := (str, 10, 64)
	if err != nil {
		return 0, err
	}
	return time, nil
}

/weloe/token-go/blob/8bf577c0309332cb42d17e33b435f06fb74d8e7b/#L454

Remaining the remaining time for ban

// GetBannedTime get banned time
func (e *Enforcer) GetBannedTime(id string, service string) int64 {
	timeout := ((id, service))
	return timeout
}

With these methods, we can use token-go to achieve login services more conveniently.

For general business, we can actually ignore the ban level. Just useBanned()Perform bans before each service that needs to be verified whether it has been bannedIsBanned()Just make a judgment.

Test example:

func TestEnforcer_Banned(t *) {
	err, enforcer, _ := NewTestEnforcer(t)
	if err != nil {
		("NewTestEnforcer() failed: %v", err)
	}
	err = ("1", "comment", 1, 100)
	if err != nil {
		("Banned() failed: %v", err)
	}
	isBanned := ("1", "comment")
	if !isBanned {
		("unexpected isBanned is false")
	}
	err = ("1", "comment")
	if err != nil {
		("UnBanned() failed: %v", err)
	}
	isBanned = ("1", "comment")
	if isBanned {
		("unexpected isBanned is false")
	}
}

Of course, we will have some more complex bans on a business, such asdiscussFor communication business, if the level is 1, we will prohibit comments, if the level is 2, we will prohibit comments and likes, and if the level is 3, we will prohibit comments, likes and collections.

At this time we just need to addGetBannedLevel(), obtaining the blocking level before each service and making judgments can be achieved simply.

Using Go language packaging function can improve the efficiency of blocking operations. Through abstract blocking operations, it provides a simple and easy-to-use interface, which can reduce code duplication and improve code maintainability and scalability. At the same time, using the concurrency characteristics of Go language, efficient concurrent blocking operations can be achieved and the system's processing capabilities can be improved. Therefore, using Go language for blocking is an efficient choice.

This is the article about efficient ban: using Go encapsulation function to improve the efficiency of ban operation. For more related contents of ban function, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!