server icon indicating copy to clipboard operation
server copied to clipboard

Dynamic Removal of the Hooks

Open yusufcanb opened this issue 1 year ago • 2 comments

Use Case

@dgduncan, as a user I want to be able to remove, disable or re-enable hooks. This flexibility allows users to configure mochi on runtime which can be very beneficial on various concepts, such as tenant isolation.

Proposal

mqtt.Server has a public method which allows users to add hooks.

server := mqtt.New(nil)
server.AddHook(new(myCustomHook), ...)

For that, a mqtt.HookBase needs to be embedded into custom hook struct.

// ID returns the ID of the hook.
type myCustomHook struct {
	mqtt.HookBase	
}

func (h *myCustomHook ) ID() string {
	return "my-custom-hook"
}

Since every hook has to implement func (...) ID() string than we could find it's reference in the broker hence a server method like below would be straightforward and self-explanatory for the user.

// RemoveHook removes a hook by it's id.
func (s *Server) RemoveHook(id string) error

yusufcanb avatar Jul 23 '24 10:07 yusufcanb

@yusufcanb, can you elaborate further on the use-case? Providing an example would be helpful.

thedevop avatar Aug 03 '24 04:08 thedevop

Well the idea back then was having a dynamic hook that allow us to configure them on runtime.

A solid use case I thought of was a tenant based approach to the hooks where multiple tenants of the broker can dynamically configure their broker behavior by adding/removing hooks from the broker without causing any restart for others.

However, I've already implemented a similar approach without having a dynamic hook implementation.

yusufcanb avatar May 13 '25 07:05 yusufcanb