-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
232 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
clock [![Build Status](https://drone.io/github.com/benbjohnson/clock/status.png)](https://drone.io/github.com/benbjohnson/clock/latest) [![Coverage Status](https://coveralls.io/repos/benbjohnson/clock/badge.png?branch=master)](https://coveralls.io/r/benbjohnson/clock?branch=master) [![GoDoc](https://godoc.org/github.com/benbjohnson/clock?status.png)](https://godoc.org/github.com/benbjohnson/clock) ![Project status](http://img.shields.io/status/experimental.png?color=red) | ||
===== | ||
|
||
Clock is a small library for mocking time in Go. It provides an interface | ||
around the standard library's [`time`][time] package so that the application | ||
can use the realtime clock while tests can use the mock clock. | ||
|
||
[time]: http://golang.org/pkg/time/ | ||
|
||
|
||
## Usage | ||
|
||
### Realtime Clock | ||
|
||
Your application can maintain a `Clock` variable that will allow realtime and | ||
mock clocks to be interchangable. For example, if you had an `Application` type: | ||
|
||
```go | ||
import "github.com/benbjohnson/clock" | ||
|
||
type Application struct { | ||
Clock clock.Clock | ||
} | ||
``` | ||
|
||
You could initialize it to use the realtime clock like this: | ||
|
||
```go | ||
var app Application | ||
app.Clock = clock.New() | ||
... | ||
``` | ||
|
||
Then all timers and time-related functionality should be performed from the | ||
`Clock` variable. | ||
|
||
|
||
### Mocking time | ||
|
||
In your tests, you will want to use a `Mock` clock: | ||
|
||
```go | ||
import ( | ||
"testing" | ||
|
||
"github.com/benbjohnson/clock" | ||
) | ||
|
||
func TestApplication_DoSomething(t *testing.T) { | ||
mock := clock.NewMock() | ||
app := Application{Clock: mock} | ||
... | ||
} | ||
``` | ||
|
||
Now that you've initialized your application to use the mock clock, you can | ||
adjust the time programmatically. The mock clock always starts from the Unix | ||
epoch (midnight, Jan 1, 1970 UTC). | ||
|
||
|
||
### Controlling time | ||
|
||
The mock clock provides the same functions that the standard library's `time` | ||
package provides. For example, to find the current time, you use the `Now()` | ||
function: | ||
|
||
```go | ||
mock := clock.NewMock() | ||
|
||
// Find the current time. | ||
mock.Now().UTC() // 1970-01-01 00:00:00 +0000 UTC | ||
|
||
// Move the clock forward. | ||
mock.Add(2 * time.Hour) | ||
|
||
// Check the time again. It's 2 hours later! | ||
mock.Now().UTC() // 1970-01-01 02:00:00 +0000 UTC | ||
``` | ||
|
||
Timers and Tickers are also controlled by this same mock clock. They will only | ||
execute when the clock is moved forward: | ||
|
||
``` | ||
mock := clock.NewMock() | ||
count := 0 | ||
// Kick off a timer to increment every 1 mock second. | ||
go func() { | ||
ticker := clock.Ticker(1 * time.Second) | ||
for { | ||
<-ticker.C | ||
count++ | ||
} | ||
}() | ||
runtime.Gosched() | ||
// Move the clock forward 10 second. | ||
mock.Add(10 * time.Second) | ||
// This prints 10. | ||
fmt.Println(count) | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Kubernetes | ||
|
||
[![Submit Queue Widget]][Submit Queue] [![GoDoc Widget]][GoDoc] [![CII Best Practices](https://bestpractices.coreinfrastructure.org/projects/569/badge)](https://bestpractices.coreinfrastructure.org/projects/569) | ||
|
||
<img src="https://github.com/kubernetes/kubernetes/raw/master/logo/logo.png" width="100"> | ||
|
||
---- | ||
|
||
Kubernetes is an open source system for managing [containerized applications] | ||
across multiple hosts; providing basic mechanisms for deployment, maintenance, | ||
and scaling of applications. | ||
|
||
Kubernetes builds upon a decade and a half of experience at Google running | ||
production workloads at scale using a system called [Borg], | ||
combined with best-of-breed ideas and practices from the community. | ||
|
||
Kubernetes is hosted by the Cloud Native Computing Foundation ([CNCF]). | ||
If you are a company that wants to help shape the evolution of | ||
technologies that are container-packaged, dynamically-scheduled | ||
and microservices-oriented, consider joining the CNCF. | ||
For details about who's involved and how Kubernetes plays a role, | ||
read the CNCF [announcement]. | ||
|
||
---- | ||
|
||
## To start using Kubernetes | ||
|
||
See our documentation on [kubernetes.io]. | ||
|
||
Try our [interactive tutorial]. | ||
|
||
Take a free course on [Scalable Microservices with Kubernetes]. | ||
|
||
## To start developing Kubernetes | ||
|
||
The [community repository] hosts all information about | ||
building Kubernetes from source, how to contribute code | ||
and documentation, who to contact about what, etc. | ||
|
||
If you want to build Kubernetes right away there are two options: | ||
|
||
##### You have a working [Go environment]. | ||
|
||
``` | ||
$ go get -d k8s.io/kubernetes | ||
$ cd $GOPATH/src/k8s.io/kubernetes | ||
$ make | ||
``` | ||
|
||
##### You have a working [Docker environment]. | ||
|
||
``` | ||
$ git clone https://github.com/kubernetes/kubernetes | ||
$ cd kubernetes | ||
$ make quick-release | ||
``` | ||
|
||
For the full story, head over to the [developer's documentation]. | ||
|
||
## Support | ||
|
||
If you need support, start with the [troubleshooting guide], | ||
and work your way through the process that we've outlined. | ||
|
||
That said, if you have questions, reach out to us | ||
[one way or another][communication]. | ||
|
||
[announcement]: https://cncf.io/news/announcement/2015/07/new-cloud-native-computing-foundation-drive-alignment-among-container | ||
[Borg]: https://research.google.com/pubs/pub43438.html | ||
[CNCF]: https://www.cncf.io/about | ||
[communication]: https://github.com/kubernetes/community/blob/master/communication.md | ||
[community repository]: https://github.com/kubernetes/community | ||
[containerized applications]: https://kubernetes.io/docs/concepts/overview/what-is-kubernetes/ | ||
[developer's documentation]: https://github.com/kubernetes/community/tree/master/contributors/devel#readme | ||
[Docker environment]: https://docs.docker.com/engine | ||
[Go environment]: https://golang.org/doc/install | ||
[GoDoc]: https://godoc.org/k8s.io/kubernetes | ||
[GoDoc Widget]: https://godoc.org/k8s.io/kubernetes?status.svg | ||
[interactive tutorial]: http://kubernetes.io/docs/tutorials/kubernetes-basics | ||
[kubernetes.io]: http://kubernetes.io | ||
[Scalable Microservices with Kubernetes]: https://www.udacity.com/course/scalable-microservices-with-kubernetes--ud615 | ||
[Submit Queue]: http://submit-queue.k8s.io/#/ci | ||
[Submit Queue Widget]: http://submit-queue.k8s.io/health.svg?v=1 | ||
[troubleshooting guide]: https://kubernetes.io/docs/tasks/debug-application-cluster/troubleshooting/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Kubernetes Community Code of Conduct | ||
|
||
Please refer to our [Kubernetes Community Code of Conduct](https://git.k8s.io/community/code-of-conduct.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Go rate limiter | ||
|
||
This package provides a Golang implementation of the leaky-bucket rate limit algorithm. | ||
This implementation refills the bucket based on the time elapsed between | ||
requests instead of requiring an interval clock to fill the bucket discretely. | ||
|
||
Create a rate limiter with a maximum number of operations to perform per second. | ||
Call Take() before each operation. Take will sleep until you can continue. | ||
|
||
```go | ||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"go.uber.org/ratelimit" | ||
) | ||
|
||
func main() { | ||
rl := ratelimit.New(100) // per second | ||
|
||
prev := time.Now() | ||
for i := 0; i < 10; i++ { | ||
now := rl.Take() | ||
fmt.Println(i, now.Sub(prev)) | ||
prev = now | ||
} | ||
|
||
// Output: | ||
// 0 0 | ||
// 1 10ms | ||
// 2 10ms | ||
// 3 10ms | ||
// 4 10ms | ||
// 5 10ms | ||
// 6 10ms | ||
// 7 10ms | ||
// 8 10ms | ||
// 9 10ms | ||
} | ||
``` |