本文整理汇总了Golang中k8s/io/kubernetes/pkg/util.NewTokenBucketRateLimiter函数的典型用法代码示例。如果您正苦于以下问题:Golang NewTokenBucketRateLimiter函数的具体用法?Golang NewTokenBucketRateLimiter怎么用?Golang NewTokenBucketRateLimiter使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewTokenBucketRateLimiter函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: MakeClient
func MakeClient(token string) *github.Client {
var client *http.Client
var transport http.RoundTripper
if *useMemoryCache {
transport = httpcache.NewMemoryCacheTransport()
} else {
transport = http.DefaultTransport
}
if len(token) > 0 {
rateLimitTransport := &RateLimitRoundTripper{
delegate: transport,
// Global limit is 5000 Q/Hour, try to only use 1800 to make room for other apps
throttle: util.NewTokenBucketRateLimiter(0.5, 10),
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
client = &http.Client{
Transport: &oauth2.Transport{
Base: rateLimitTransport,
Source: oauth2.ReuseTokenSource(nil, ts),
},
}
} else {
rateLimitTransport := &RateLimitRoundTripper{
delegate: transport,
throttle: util.NewTokenBucketRateLimiter(0.01, 10),
}
client = &http.Client{
Transport: rateLimitTransport,
}
}
return github.NewClient(client)
}
示例2: PreExecute
func (config *GithubConfig) PreExecute() error {
if len(config.Org) == 0 {
glog.Fatalf("--organization is required.")
}
if len(config.Project) == 0 {
glog.Fatalf("--project is required.")
}
token := config.Token
if len(token) == 0 && len(config.TokenFile) != 0 {
data, err := ioutil.ReadFile(config.TokenFile)
if err != nil {
glog.Fatalf("error reading token file: %v", err)
}
token = string(data)
}
var client *http.Client
var transport http.RoundTripper
if config.useMemoryCache {
transport = httpcache.NewMemoryCacheTransport()
} else {
transport = http.DefaultTransport
}
if len(token) > 0 {
rateLimitTransport := &RateLimitRoundTripper{
delegate: transport,
// Global limit is 5000 Q/Hour, try to only use 1800 to make room for other apps
throttle: util.NewTokenBucketRateLimiter(0.5, 10),
}
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
client = &http.Client{
Transport: &oauth2.Transport{
Base: rateLimitTransport,
Source: oauth2.ReuseTokenSource(nil, ts),
},
}
} else {
rateLimitTransport := &RateLimitRoundTripper{
delegate: transport,
throttle: util.NewTokenBucketRateLimiter(0.01, 10),
}
client = &http.Client{
Transport: rateLimitTransport,
}
}
config.client = github.NewClient(client)
return nil
}
示例3: main
func main() {
var ingClient client.IngressInterface
if kubeClient, err := client.NewInCluster(); err != nil {
log.Fatalf("Failed to create client: %v.", err)
} else {
ingClient = kubeClient.Extensions().Ingress(os.Getenv("INGRESS_NAMESPACE"))
}
tmpl, _ := template.New("haproxy").Parse(haproxyConf)
rateLimiter := util.NewTokenBucketRateLimiter(0.1, 1)
known := &extensions.IngressList{}
// Controller loop
shellOut("haproxy -f /etc/haproxy/haproxy.cfg -p /var/run/haproxy-private.pid")
for {
rateLimiter.Accept()
ingresses, err := ingClient.List(api.ListOptions{})
if err != nil {
log.Printf("Error retrieving ingresses: %v", err)
continue
}
if reflect.DeepEqual(ingresses.Items, known.Items) {
log.Printf("Nothing Has Changed")
continue
}
known = ingresses
if w, err := os.Create("/etc/haproxy/haproxy.cfg"); err != nil {
log.Fatalf("Failed to open %v: %v", haproxyConf, err)
defer w.Close()
} else if err := tmpl.Execute(w, ingresses); err != nil {
log.Fatalf("Failed to write template %v", err)
}
restartHaproxy("haproxy_reload")
}
}
示例4: main
func main() {
nginx.Start()
kubeClient, err := client.NewInCluster()
if err != nil {
log.Fatalf("Failed to create client: %v.", err)
}
rateLimiter := util.NewTokenBucketRateLimiter(0.1, 1)
known := &model.RouterConfig{}
// Main loop
for {
rateLimiter.Accept()
routerConfig, err := model.Build(kubeClient)
if err != nil {
log.Printf("Error building model; not modifying certs or configuration: %v.", err)
continue
}
if reflect.DeepEqual(routerConfig, known) {
continue
}
log.Println("INFO: Router configuration has changed in k8s.")
err = nginx.WriteCerts(routerConfig, "/opt/nginx/ssl")
if err != nil {
log.Printf("Failed to write certs; continuing with existing certs and configuration: %v", err)
continue
}
err = nginx.WriteConfig(routerConfig, "/opt/nginx/conf/nginx.conf")
if err != nil {
log.Printf("Failed to write new nginx configuration; continuing with existing configuration: %v", err)
continue
}
nginx.Reload()
known = routerConfig
}
}
示例5: newIPVSController
// newIPVSController creates a new controller from the given config.
func newIPVSController(kubeClient *unversioned.Client, namespace string, useUnicast bool, password string) *ipvsControllerController {
ipvsc := ipvsControllerController{
client: kubeClient,
queue: workqueue.New(),
reloadRateLimiter: util.NewTokenBucketRateLimiter(reloadQPS, int(reloadQPS)),
reloadLock: &sync.Mutex{},
}
clusterNodes := getClusterNodesIP(kubeClient)
nodeInfo, err := getNodeInfo(clusterNodes)
if err != nil {
glog.Fatalf("Error getting local IP from nodes in the cluster: %v", err)
}
neighbors := getNodeNeighbors(nodeInfo, clusterNodes)
ipvsc.keepalived = &keepalived{
iface: nodeInfo.iface,
ip: nodeInfo.ip,
netmask: nodeInfo.netmask,
nodes: clusterNodes,
neighbors: neighbors,
priority: getNodePriority(nodeInfo.ip, clusterNodes),
useUnicast: useUnicast,
password: password,
}
enqueue := func(obj interface{}) {
key, err := keyFunc(obj)
if err != nil {
glog.Infof("Couldn't get key for object %+v: %v", obj, err)
return
}
ipvsc.queue.Add(key)
}
eventHandlers := framework.ResourceEventHandlerFuncs{
AddFunc: enqueue,
DeleteFunc: enqueue,
UpdateFunc: func(old, cur interface{}) {
if !reflect.DeepEqual(old, cur) {
enqueue(cur)
}
},
}
ipvsc.svcLister.Store, ipvsc.svcController = framework.NewInformer(
cache.NewListWatchFromClient(
ipvsc.client, "services", namespace, fields.Everything()),
&api.Service{}, resyncPeriod, eventHandlers)
ipvsc.epLister.Store, ipvsc.epController = framework.NewInformer(
cache.NewListWatchFromClient(
ipvsc.client, "endpoints", namespace, fields.Everything()),
&api.Endpoints{}, resyncPeriod, eventHandlers)
return &ipvsc
}
示例6: NewTurboScheduler
func NewTurboScheduler(kubeClient *client.Client, vmturboMeta *vmtmeta.VMTMeta) *TurboScheduler {
scheduledPodLister := &cache.StoreToPodLister{}
podQueue := cache.NewFIFO(cache.MetaNamespaceKeyFunc)
modeler := scheduler.NewSimpleModeler(&cache.StoreToPodLister{Store: podQueue}, scheduledPodLister)
bindPodsQPS := float32(15.0)
bindPodsBurst := 20
rateLimiter := util.NewTokenBucketRateLimiter(bindPodsQPS, bindPodsBurst)
config := &Config{
Modeler: modeler,
Binder: &binder{kubeClient},
BindPodsRateLimiter: rateLimiter,
}
eventBroadcaster := record.NewBroadcaster()
config.Recorder = eventBroadcaster.NewRecorder(api.EventSource{Component: "turboscheduler"})
eventBroadcaster.StartLogging(glog.Infof)
eventBroadcaster.StartRecordingToSink(kubeClient.Events(""))
vmtSched := vmtscheduler.NewVMTScheduler(kubeClient, vmturboMeta)
glog.V(3).Infof("VMTScheduler is set: %++v", vmtSched)
defaultSched := defaultscheduler.NewDefaultScheduler(kubeClient)
glog.V(3).Infof("DefaultScheduler is set: %++v", defaultSched)
return &TurboScheduler{
config: config,
vmtScheduler: vmtSched,
defaultScheduler: defaultSched,
}
}
示例7: main
func main() {
var ingClient client.IngressInterface
if kubeClient, err := client.NewInCluster(); err != nil {
log.Fatalf("Failed to create client: %v.", err)
} else {
ingClient = kubeClient.Extensions().Ingress(api.NamespaceAll)
}
tmpl, _ := template.New("nginx").Parse(nginxConf)
rateLimiter := util.NewTokenBucketRateLimiter(0.1, 1)
known := &extensions.IngressList{}
// Controller loop
shellOut("nginx")
for {
rateLimiter.Accept()
ingresses, err := ingClient.List(labels.Everything(), fields.Everything())
if err != nil || reflect.DeepEqual(ingresses.Items, known.Items) {
continue
}
known = ingresses
if w, err := os.Create("/etc/nginx/nginx.conf"); err != nil {
log.Fatalf("Failed to open %v: %v", nginxConf, err)
} else if err := tmpl.Execute(w, ingresses); err != nil {
log.Fatalf("Failed to write template %v", err)
}
shellOut("nginx -s reload")
}
}
示例8: CreateDeleteController
// CreateDeleteController constructs a BuildPodDeleteController
func (factory *BuildPodControllerFactory) CreateDeleteController() controller.RunnableController {
client := ControllerClient{factory.KubeClient, factory.OSClient}
queue := cache.NewDeltaFIFO(cache.MetaNamespaceKeyFunc, nil, nil)
cache.NewReflector(&buildPodDeleteLW{client, queue}, &kapi.Pod{}, queue, 5*time.Minute).Run()
buildPodDeleteController := &buildcontroller.BuildPodDeleteController{
BuildStore: factory.buildStore,
BuildUpdater: factory.BuildUpdater,
}
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
cache.MetaNamespaceKeyFunc,
controller.RetryNever,
kutil.NewTokenBucketRateLimiter(1, 10)),
Handle: func(obj interface{}) error {
deltas := obj.(cache.Deltas)
for _, delta := range deltas {
if delta.Type == cache.Deleted {
return buildPodDeleteController.HandleBuildPodDeletion(delta.Object.(*kapi.Pod))
}
}
return nil
},
}
}
示例9: Create
// Create constructs a BuildPodController
func (factory *BuildPodControllerFactory) Create() controller.RunnableController {
factory.buildStore = cache.NewStore(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&buildLW{client: factory.OSClient}, &buildapi.Build{}, factory.buildStore, 2*time.Minute).RunUntil(factory.Stop)
queue := cache.NewFIFO(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&podLW{client: factory.KubeClient}, &kapi.Pod{}, queue, 2*time.Minute).RunUntil(factory.Stop)
client := ControllerClient{factory.KubeClient, factory.OSClient}
buildPodController := &buildcontroller.BuildPodController{
BuildStore: factory.buildStore,
BuildUpdater: factory.BuildUpdater,
PodManager: client,
}
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
cache.MetaNamespaceKeyFunc,
retryFunc("BuildPod", nil),
kutil.NewTokenBucketRateLimiter(1, 10)),
Handle: func(obj interface{}) error {
pod := obj.(*kapi.Pod)
return buildPodController.HandlePod(pod)
},
}
}
示例10: Create
// Create constructs a BuildController
func (factory *BuildControllerFactory) Create() controller.RunnableController {
queue := cache.NewFIFO(cache.MetaNamespaceKeyFunc)
cache.NewReflector(&buildLW{client: factory.OSClient}, &buildapi.Build{}, queue, 2*time.Minute).Run()
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(factory.KubeClient.Events(""))
client := ControllerClient{factory.KubeClient, factory.OSClient}
buildController := &buildcontroller.BuildController{
BuildUpdater: factory.BuildUpdater,
ImageStreamClient: client,
PodManager: client,
BuildStrategy: &typeBasedFactoryStrategy{
DockerBuildStrategy: factory.DockerBuildStrategy,
SourceBuildStrategy: factory.SourceBuildStrategy,
CustomBuildStrategy: factory.CustomBuildStrategy,
},
Recorder: eventBroadcaster.NewRecorder(kapi.EventSource{Component: "build-controller"}),
}
return &controller.RetryController{
Queue: queue,
RetryManager: controller.NewQueueRetryManager(
queue,
cache.MetaNamespaceKeyFunc,
limitedLogAndRetry(factory.BuildUpdater, 30*time.Minute),
kutil.NewTokenBucketRateLimiter(1, 10)),
Handle: func(obj interface{}) error {
build := obj.(*buildapi.Build)
return buildController.HandleBuild(build)
},
}
}
示例11: NewRESTClient
// NewRESTClient creates a new RESTClient. This client performs generic REST functions
// such as Get, Put, Post, and Delete on specified paths. Codec controls encoding and
// decoding of responses from the server.
func NewRESTClient(baseURL *url.URL, versionedAPIPath string, config ContentConfig, maxQPS float32, maxBurst int, client *http.Client) *RESTClient {
base := *baseURL
if !strings.HasSuffix(base.Path, "/") {
base.Path += "/"
}
base.RawQuery = ""
base.Fragment = ""
if config.GroupVersion == nil {
config.GroupVersion = &unversioned.GroupVersion{}
}
if len(config.ContentType) == 0 {
config.ContentType = "application/json"
}
var throttle util.RateLimiter
if maxQPS > 0 {
throttle = util.NewTokenBucketRateLimiter(maxQPS, maxBurst)
}
return &RESTClient{
base: &base,
versionedAPIPath: versionedAPIPath,
contentConfig: config,
Throttle: throttle,
Client: client,
}
}
示例12: Create
// Create creates an ImportController.
func (f *ImportControllerFactory) Create() controller.RunnableController {
lw := &cache.ListWatch{
ListFunc: func() (runtime.Object, error) {
return f.Client.ImageStreams(kapi.NamespaceAll).List(labels.Everything(), fields.Everything())
},
WatchFunc: func(resourceVersion string) (watch.Interface, error) {
return f.Client.ImageStreams(kapi.NamespaceAll).Watch(labels.Everything(), fields.Everything(), resourceVersion)
},
}
q := cache.NewFIFO(cache.MetaNamespaceKeyFunc)
cache.NewReflector(lw, &api.ImageStream{}, q, 2*time.Minute).Run()
c := &ImportController{
streams: f.Client,
mappings: f.Client,
}
return &controller.RetryController{
Queue: q,
RetryManager: controller.NewQueueRetryManager(
q,
cache.MetaNamespaceKeyFunc,
func(obj interface{}, err error, retries controller.Retry) bool {
util.HandleError(err)
return retries.Count < 5
},
kutil.NewTokenBucketRateLimiter(1, 10),
),
Handle: func(obj interface{}) error {
r := obj.(*api.ImageStream)
return c.Next(r)
},
}
}
示例13: main
func main() {
var ingClient client.IngressInterface
var secretsClient client.SecretsInterface
/* Anon http client
config := client.Config{
Host: "http://localhost:8080",
Username: "admin",
Password: "admin",
}
kubeClient, err := client.New(&config)
*/
kubeClient, err := client.NewInCluster()
if err != nil {
log.Fatalf("Failed to create client: %v.", err)
} else {
ingClient = kubeClient.Extensions().Ingress(api.NamespaceAll)
secretsClient = kubeClient.Secrets(api.NamespaceAll)
}
tmpl := template.New("nginx.tmpl").Funcs(template.FuncMap{"hasprefix": hasPrefix, "hassuffix": hasSuffix})
if _, err := tmpl.ParseFiles("./nginx.tmpl"); err != nil {
log.Fatalf("Failed to parse template %v", err)
}
rateLimiter := util.NewTokenBucketRateLimiter(0.1, 1)
known := &extensions.IngressList{}
known_secrets := &api.SecretList{}
// Controller loop
shellOut("nginx")
for {
rateLimiter.Accept()
ingresses, err := ingClient.List(api.ListOptions{})
if err != nil {
log.Printf("Error retrieving ingresses: %v", err)
continue
}
secrets, err := secretsClient.List(api.ListOptions{})
if err != nil {
log.Printf("Error retrieving secrets: %v", err)
continue
}
if reflect.DeepEqual(ingresses.Items, known.Items) && reflect.DeepEqual(secrets.Items, known_secrets.Items) {
continue
}
// Process SSL context
// old values
known = ingresses
known_secrets = secrets
// context variable
context := &Context{Ingress: ingresses, Secrets: secrets}
if w, err := os.Create("/etc/nginx/nginx.conf"); err != nil {
log.Fatalf("Failed to open %v: %v", err)
} else if err := tmpl.Execute(w, context); err != nil {
log.Fatalf("Failed to write template %v", err)
}
shellOut("nginx -s reload")
}
}
示例14: newLoadBalancerController
// newLoadBalancerController creates a new controller from the given config.
func newLoadBalancerController(cfg *loadBalancerConfig, kubeClient *unversioned.Client, namespace string) *loadBalancerController {
lbc := loadBalancerController{
cfg: cfg,
client: kubeClient,
queue: workqueue.New(),
reloadRateLimiter: util.NewTokenBucketRateLimiter(
reloadQPS, int(reloadQPS)),
targetService: *targetService,
forwardServices: *forwardServices,
httpPort: *httpPort,
tcpServices: map[string]int{},
}
for _, service := range strings.Split(*tcpServices, ",") {
portSplit := strings.Split(service, ":")
if len(portSplit) != 2 {
glog.Errorf("Ignoring misconfigured TCP service %v", service)
continue
}
if port, err := strconv.Atoi(portSplit[1]); err != nil {
glog.Errorf("Ignoring misconfigured TCP service %v: %v", service, err)
continue
} else {
lbc.tcpServices[portSplit[0]] = port
}
}
enqueue := func(obj interface{}) {
key, err := keyFunc(obj)
if err != nil {
glog.Infof("Couldn't get key for object %+v: %v", obj, err)
return
}
lbc.queue.Add(key)
}
eventHandlers := framework.ResourceEventHandlerFuncs{
AddFunc: enqueue,
DeleteFunc: enqueue,
UpdateFunc: func(old, cur interface{}) {
if !reflect.DeepEqual(old, cur) {
enqueue(cur)
}
},
}
lbc.svcLister.Store, lbc.svcController = framework.NewInformer(
cache.NewListWatchFromClient(
lbc.client, "services", namespace, fields.Everything()),
&api.Service{}, resyncPeriod, eventHandlers)
lbc.epLister.Store, lbc.epController = framework.NewInformer(
cache.NewListWatchFromClient(
lbc.client, "endpoints", namespace, fields.Everything()),
&api.Endpoints{}, resyncPeriod, eventHandlers)
return &lbc
}
示例15: newAltTokenSource
func newAltTokenSource(tokenURL string) oauth2.TokenSource {
client := oauth2.NewClient(oauth2.NoContext, google.ComputeTokenSource(""))
a := &altTokenSource{
oauthClient: client,
tokenURL: tokenURL,
throttle: util.NewTokenBucketRateLimiter(tokenURLQPS, tokenURLBurst),
}
return oauth2.ReuseTokenSource(nil, a)
}