const DB_ADDRESS = "db_address" const CALCULATE_VALUE = "calculate_value" func readDB(ctx context.Context, cost time.Duration) { fmt.Println("db address is", ctx.Value(DB_ADDRESS)) select { case <- time.After(cost): // 模拟数据库读取 fmt.Println("read data from db") case <-ctx....
Time, newLast time.Time, newTokens float64) { last := lim.last if now.Before(last) { last = now } // Calculate the new number of tokens, due to time that passed. elapsed := now.Sub(last) delta := lim.limit.tokensFromDuration(elapsed) tokens := lim.tokens + delta if burst :=...
func(lim*Limiter)reserveN(now time.Time,n int,maxFutureReserve time.Duration)Reservation{lim.mu.Lock()iflim.limit==Inf{lim.mu.Unlock()returnReservation{ok:true,lim:lim,tokens:n,timeToAct:now,}}now,last,tokens:=lim.advance(now)// Calculate the remaining number of tokens resulting from the ...
(startTime) // Calculate expected time for totalBytesCopied at speedLimit bytes/second expectedTime := time.Duration(totalBytesCopied) * time.Second / time.Duration(speedLimit) // Sleep if we're ahead of schedule if elapsedTime < expectedTime { time.Sleep(expectedTime - elapsedTime) } }...
time.Sleep(time.Duration(sleeptime) * time.Second) ch <- fmt.Sprintf("task id %d , sleep %d second", task_id, sleeptime) return } funcmain() { input := []int{3,2,1} timeout :=2 chLimit :=make(chanbool,1) chs :=make([]chanstring,len(input)) ...
Submit(syncCalculateSum) } wg.Wait() fmt.Printf("running goroutines: %d\n", ants.Running()) fmt.Printf("finish all tasks.\n") // Use the pool with a function, // set 10 to the capacity of goroutine pool and 1 second for expired duration. p, _ := ants.NewPoolWithFunc(10, ...
{func_mode="Calculate",func_name="funcName3",le="0.005"} 0 func_run_duration_bucket{func_mode="Calculate",func_name="funcName3",le="0.01"} 0 func_run_duration_bucket{func_mode="Calculate",func_name="funcName3",le="0.03"} 0 func_run_duration_bucket{func_mode="Calculate",func_...
// calculate tokens to restore // The duration between lim.lastEvent and r.timeToAct tells us how many tokens were reserved // after r was obtained. These tokens should not be restored. // 这行代码多减了一次最近申请的预支的 token ,用测试代码说明一下 ...
time.Duration// Max number of entries in life window. Used only to calculate initial size for cache shards.// When proper value is set then additional memory allocation does not occur.MaxEntriesInWindowint// Max size of entry in bytes. Used only to calculate initial size for cache shards....
1packagesimpleinterest23import"fmt"45/*6* init function added7*/8funcinit(){9fmt.Println("Simple interest package initialized")10}1112//Calculate calculates and returns the simple interest for principal p, rate of interest r for time duration t years13funcCalculate(pfloat64,rfloat64,tfloat64...