1653120300
go-rate
go-rate is a rate limiter designed for a range of use cases, including server side spam protection and preventing saturation of APIs you consume.
It is used in production at LangTrend to adhere to the GitHub API rate limits.
Import github.com/beefsack/go-rate
and create a new rate limiter with the rate.New(limit int, interval time.Duration)
function.
The rate limiter provides a Wait()
and a Try() (bool, time.Duration)
method for both blocking and non-blocking functionality respectively.
API documentation available at godoc.org.
This example demonstrates limiting the output rate to 3 times per second.
package main
import (
"fmt"
"time"
"github.com/beefsack/go-rate"
)
func main() {
rl := rate.New(3, time.Second) // 3 times per second
begin := time.Now()
for i := 1; i <= 10; i++ {
rl.Wait()
fmt.Printf("%d started at %s\n", i, time.Now().Sub(begin))
}
// Output:
// 1 started at 12.584us
// 2 started at 40.13us
// 3 started at 44.92us
// 4 started at 1.000125362s
// 5 started at 1.000143066s
// 6 started at 1.000144707s
// 7 started at 2.000224641s
// 8 started at 2.000240751s
// 9 started at 2.00024244s
// 10 started at 3.000314332s
}
This example demonstrates combining rate limiters, one limiting at once per second, the other limiting at 2 times per 3 seconds.
package main
import (
"fmt"
"time"
"github.com/beefsack/go-rate"
)
func main() {
begin := time.Now()
rl1 := rate.New(1, time.Second) // Once per second
rl2 := rate.New(2, time.Second*3) // 2 times per 3 seconds
for i := 1; i <= 10; i++ {
rl1.Wait()
rl2.Wait()
fmt.Printf("%d started at %s\n", i, time.Now().Sub(begin))
}
// Output:
// 1 started at 11.197us
// 2 started at 1.00011941s
// 3 started at 3.000105858s
// 4 started at 4.000210639s
// 5 started at 6.000189578s
// 6 started at 7.000289992s
// 7 started at 9.000289942s
// 8 started at 10.00038286s
// 9 started at 12.000386821s
// 10 started at 13.000465465s
}
This example demonstrates non-blocking rate limiting, such as would be used to limit spam in a chat client.
package main
import (
"fmt"
"time"
"github.com/beefsack/go-rate"
)
var rl = rate.New(3, time.Second) // 3 times per second
func say(message string) {
if ok, remaining := rl.Try(); ok {
fmt.Printf("You said: %s\n", message)
} else {
fmt.Printf("Spam filter triggered, please wait %s\n", remaining)
}
}
func main() {
for i := 1; i <= 5; i++ {
say(fmt.Sprintf("Message %d", i))
}
time.Sleep(time.Second / 2)
say("I waited half a second, is that enough?")
time.Sleep(time.Second / 2)
say("Okay, I waited a second.")
// Output:
// You said: Message 1
// You said: Message 2
// You said: Message 3
// Spam filter triggered, please wait 999.980816ms
// Spam filter triggered, please wait 999.976704ms
// Spam filter triggered, please wait 499.844795ms
// You said: Okay, I waited a second.
}
Author: Beefsack
Source Code: https://github.com/beefsack/go-rate
License: MIT license
1599854400
Go announced Go 1.15 version on 11 Aug 2020. Highlighted updates and features include Substantial improvements to the Go linker, Improved allocation for small objects at high core counts, X.509 CommonName deprecation, GOPROXY supports skipping proxies that return errors, New embedded tzdata package, Several Core Library improvements and more.
As Go promise for maintaining backward compatibility. After upgrading to the latest Go 1.15 version, almost all existing Golang applications or programs continue to compile and run as older Golang version.
#go #golang #go 1.15 #go features #go improvement #go package #go new features
1653120300
go-rate
go-rate is a rate limiter designed for a range of use cases, including server side spam protection and preventing saturation of APIs you consume.
It is used in production at LangTrend to adhere to the GitHub API rate limits.
Import github.com/beefsack/go-rate
and create a new rate limiter with the rate.New(limit int, interval time.Duration)
function.
The rate limiter provides a Wait()
and a Try() (bool, time.Duration)
method for both blocking and non-blocking functionality respectively.
API documentation available at godoc.org.
This example demonstrates limiting the output rate to 3 times per second.
package main
import (
"fmt"
"time"
"github.com/beefsack/go-rate"
)
func main() {
rl := rate.New(3, time.Second) // 3 times per second
begin := time.Now()
for i := 1; i <= 10; i++ {
rl.Wait()
fmt.Printf("%d started at %s\n", i, time.Now().Sub(begin))
}
// Output:
// 1 started at 12.584us
// 2 started at 40.13us
// 3 started at 44.92us
// 4 started at 1.000125362s
// 5 started at 1.000143066s
// 6 started at 1.000144707s
// 7 started at 2.000224641s
// 8 started at 2.000240751s
// 9 started at 2.00024244s
// 10 started at 3.000314332s
}
This example demonstrates combining rate limiters, one limiting at once per second, the other limiting at 2 times per 3 seconds.
package main
import (
"fmt"
"time"
"github.com/beefsack/go-rate"
)
func main() {
begin := time.Now()
rl1 := rate.New(1, time.Second) // Once per second
rl2 := rate.New(2, time.Second*3) // 2 times per 3 seconds
for i := 1; i <= 10; i++ {
rl1.Wait()
rl2.Wait()
fmt.Printf("%d started at %s\n", i, time.Now().Sub(begin))
}
// Output:
// 1 started at 11.197us
// 2 started at 1.00011941s
// 3 started at 3.000105858s
// 4 started at 4.000210639s
// 5 started at 6.000189578s
// 6 started at 7.000289992s
// 7 started at 9.000289942s
// 8 started at 10.00038286s
// 9 started at 12.000386821s
// 10 started at 13.000465465s
}
This example demonstrates non-blocking rate limiting, such as would be used to limit spam in a chat client.
package main
import (
"fmt"
"time"
"github.com/beefsack/go-rate"
)
var rl = rate.New(3, time.Second) // 3 times per second
func say(message string) {
if ok, remaining := rl.Try(); ok {
fmt.Printf("You said: %s\n", message)
} else {
fmt.Printf("Spam filter triggered, please wait %s\n", remaining)
}
}
func main() {
for i := 1; i <= 5; i++ {
say(fmt.Sprintf("Message %d", i))
}
time.Sleep(time.Second / 2)
say("I waited half a second, is that enough?")
time.Sleep(time.Second / 2)
say("Okay, I waited a second.")
// Output:
// You said: Message 1
// You said: Message 2
// You said: Message 3
// Spam filter triggered, please wait 999.980816ms
// Spam filter triggered, please wait 999.976704ms
// Spam filter triggered, please wait 499.844795ms
// You said: Okay, I waited a second.
}
Author: Beefsack
Source Code: https://github.com/beefsack/go-rate
License: MIT license
1665072840
❤️ Uptrace.dev - distributed traces, logs, and errors in one place
This package is based on rwz/redis-gcra and implements GCRA (aka leaky bucket) for rate limiting based on Redis. The code requires Redis version 3.2 or newer since it relies on replicate_commands feature.
redis_rate supports 2 last Go versions and requires a Go version with modules support. So make sure to initialize a Go module:
go mod init github.com/my/repo
And then install redis_rate/v9 (note v9 in the import; omitting it is a popular mistake):
go get github.com/go-redis/redis_rate/v9
package redis_rate_test
import (
"context"
"fmt"
"github.com/go-redis/redis/v8"
"github.com/go-redis/redis_rate/v9"
)
func ExampleNewLimiter() {
ctx := context.Background()
rdb := redis.NewClient(&redis.Options{
Addr: "localhost:6379",
})
_ = rdb.FlushDB(ctx).Err()
limiter := redis_rate.NewLimiter(rdb)
res, err := limiter.Allow(ctx, "project:123", redis_rate.PerSecond(10))
if err != nil {
panic(err)
}
fmt.Println("allowed", res.Allowed, "remaining", res.Remaining)
// Output: allowed 1 remaining 9
}
Author: Go-redis
Source Code: https://github.com/go-redis/redis_rate
License: BSD-2-Clause license
1591641000
It is essential to keep the correct time on a server. This is especially true when it comes to processing financial transactions or other vital functions which need to be handled in a specific order. Using the Network Time Protocol (or NTP), computers can synchronize their internal clock times with the internet standard reference clocks. In essence, NTP is a hierarchy of servers. The higher the Stratum number of a server, the more accurate the timing is and the lower the Stratum number of a server is, the lower the accuracy and time stability. Stratus are defined by the distance from the initial reference clock.
#tutorials #atomic clock #centos #chrony #clock #drift #internal time clock #network time protocol #ntp #offset #peers #pool.ntp.org #server time #stratum #system clock #time #time drift #timekeeping #ubuntu #utc
1591688078
Dealing with dates and times in Python can be a hassle. Thankfully, there’s a built-in way of making it easier: the Python datetime module.
datetime helps us identify and process time-related elements like dates, hours, minutes, seconds, days of the week, months, years, etc. It offers various services like managing time zones and daylight savings time. It can work with timestamp data. It can extract the day of the week, day of the month, and other date and time formats from strings.
#data science tutorials #calendar #date #dates #datetime #intermediate #python #time #time series #times #tutorial #tutorials