It is best illustrated with the following Go code snippet.
var x int| // NewRateLimiter returns a rate limiter function returning false | |
| // when the event must be rejected, and true when it must be accepted. | |
| // The rate is expressed in Hz (events/seconds). It simply ensures that | |
| // the time interval between two accept satisfies the rate criteria. It | |
| // is equivalent to the token bucket algorithm with a bucket size of one. | |
| func NewRateLimiter(rate float64) func() bool { | |
| var stamp time.Time // time stamp of last accepted event | |
| return func() bool { | |
| now := time.Now() | |
| if now.Sub(stamp).Seconds() *rate < 1. { |
| package main | |
| import ( | |
| "image/color" | |
| "log" | |
| "os" | |
| "strings" | |
| "gioui.org/app" | |
| "gioui.org/f32" |
| package main | |
| import ( | |
| "fmt" | |
| "image" | |
| "image/color" | |
| "os" | |
| "gioui.org/app" | |
| "gioui.org/font/gofont" |
| // getlinefd reads a full line from fd into *lineptr. Initialize *lineptr to NULL and *n to 0. | |
| // getlinefd will store the line into *lineptr which is is a buffer that will grow as needed. | |
| // The buffer size is stored in *n. The line ends '\0' and with a '\n'when present. | |
| // getlinefd returns the number of characters read, or -1 in case of error or if the end of file is reached. | |
| ssize_t getlinefd(char **lineptr, size_t *n, int fd) { | |
| static char buf[10]; | |
| static size_t len = 0; | |
| const size_t blen = sizeof(buf); | |
| if (lineptr == NULL || n == NULL) { |
| res, err := http.GET(...) | |
| if res != nil { | |
| defer func() { | |
| _, err = io.Copy(ioutil.Discard, res.Body) | |
| res.Body.Close() | |
| }() | |
| } | |
| if err != nil { | |
| // . . . | |
| } |
| var ( | |
| cnNameOid = asn1.ObjectIdentifier{2, 5, 4, 3} | |
| emailOid = asn1.ObjectIdentifier{1, 2, 840, 113549, 1, 9, 1} | |
| userIDOid = asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 1} | |
| dcNameOid = asn1.ObjectIdentifier{0, 9, 2342, 19200300, 100, 1, 25} | |
| ) | |
| // RDNSToString returns the Relative Distinguish Name as a string. | |
| func RDNSToString(rdns *pkix.RDNSequence) string { | |
| var buf strings.Builder |
| func dumpByteSlice(b []byte) { | |
| var a [16]byte | |
| n := (len(b) + 15) &^ 15 | |
| for i := 0; i < n; i++ { | |
| if i%16 == 0 { | |
| fmt.Printf("%4d", i) | |
| } | |
| if i%8 == 0 { | |
| fmt.Print(" ") | |
| } |
| // Please use the package https://github.com/chmike/domain as is it maintained up to date with tests. | |
| // checkDomain returns an error if the domain name is not valid. | |
| // See https://tools.ietf.org/html/rfc1034#section-3.5 and | |
| // https://tools.ietf.org/html/rfc1123#section-2. | |
| func checkDomain(name string) error { | |
| switch { | |
| case len(name) == 0: | |
| return nil // an empty domain name will result in a cookie without a domain restriction | |
| case len(name) > 255: |
| #include <sys/time.h> | |
| // Return the current time as a double float in second units | |
| double getTimeAsDouble() | |
| { | |
| struct timeval t; | |
| ::gettimeofday( &t, NULL ); | |
| return double(t.tv_sec) + double(t.tv_usec)/1000000.0; | |
| } |