patrol icon indicating copy to clipboard operation
patrol copied to clipboard

High level library API

Open tsenart opened this issue 7 years ago • 4 comments

This issue tracks the development of a high level and ergonomic library API.

tsenart avatar Oct 24 '18 15:10 tsenart

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)
	}
}

tsenart avatar Oct 24 '18 15:10 tsenart

@peterbourgon: Is this taking it too far?

tsenart avatar Oct 24 '18 15:10 tsenart

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
}
// ...

peterbourgon avatar Oct 24 '18 15:10 peterbourgon

That's much easier to use indeed :-)

tsenart avatar Oct 24 '18 16:10 tsenart