patrol
patrol copied to clipboard
High level library API
This issue tracks the development of a high level and ergonomic library API.
func ExamplePatrol() {
// Create a Patrol station.
station, err := patrol.NewStation(
patrol.ReceiveAddress("localhost:16000"),
)
if err != nil {
return err
}
// Start radio transmissions with other stations.
go station.Radio(context.Background())
// Radar returns a speed limiting Radar that reports infractions
// to the given station.
radar := station.Radar("88.12.33.97", patrol.Limit{
Freq: 100,
Per: time.Second,
})
// Returns a speed ticket or nil.
ticket := radar.Check(time.Now(), 1)
defer station.Register(ticket)
if ticket != nil {
// If needed, you can handle speed ticket by waiting.
time.Sleep(ticket.Wait)
}
}
@peterbourgon: Is this taking it too far?
I think the nouns and verbs are too cute, and I think you're still exposing too much functionality to the end user. I want:
station, _ := patrol.NewStation(peer, peer, peer, ...)
limiter := station.NewSimpleLimiter("name", 100, time.Second)
// In HTTP handler
if !limiter.Allow() {
return http.StatusTooManyRequests
}
// ...
That's much easier to use indeed :-)