How do I implement a rate limiter in p4?

I’m trying to make the leaky bucket but I have no idea how to do it

A P4 meter extern in v1model or PSA architectures typically already implements a token bucket two-rate, three-color policer for you “under the hood”, without you having to do so yourself. In PSA it is defined that it should use an algorithm that gives the same results that the method described in RFC 2698 - A Two Rate Three Color Marker would.

A P4 meter implementation typically uses the length of the current packet as a “hidden” argument, so you need not supply it, and returns an encoding of green, yellow, or red that you can then use in your P4 code to decide what to do with that packet.

Are you hoping to implement something like RFC 2698 from scratch yourself, without relying on someone else doing it for you, to learn more about it?

Or are you hoping to implement some leaky bucket algorithm that is different from that one in some way? If so, what algorithm do you want to implement? In general, a P4 register array can enable you to store some state for some state machine, like a leaky bucket, and then while processing a packet you can read that state, use arithmetic and if conditions in P4 to calculate a new state to remember, and then write that state back.