Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

High level library API #2

Open
tsenart opened this issue Oct 24, 2018 · 4 comments
Open

High level library API #2

tsenart opened this issue Oct 24, 2018 · 4 comments

Comments

@tsenart
Copy link
Owner

tsenart commented Oct 24, 2018

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

@tsenart
Copy link
Owner Author

tsenart commented Oct 24, 2018

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
Copy link
Owner Author

tsenart commented Oct 24, 2018

@peterbourgon: Is this taking it too far?

@peterbourgon
Copy link

peterbourgon commented Oct 24, 2018

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

@tsenart
Copy link
Owner Author

tsenart commented Oct 24, 2018

That's much easier to use indeed :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants