Closed braydonk closed 4 months ago
I think the same could be accomplished with the following code on a library user side. I don't think it is necessary to add a new API.
host.EnableBootTimeCache(false)
defer host.EnableBootTimeCache(true)
BootTimeWithContext(ctx)
I think the same could be accomplished with the following code on a library user side.
That would be better if it was possible, but BootTimeWithContext is internal. So I don't see any way on the user side to only read boot time once.
I will work on an alternative PR that instead factors boot time into the common package and provides entrypoints so that your suggestion is possible.
host.BootTimeWithContext
is a public function. Here is the complete code.
package main
import (
"context"
"fmt"
"github.com/shirou/gopsutil/v3/host"
)
func main() {
host.EnableBootTimeCache(false)
defer host.EnableBootTimeCache(true)
fmt.Println(host.BootTimeWithContext(context.Background()))
}
Closing in favour of #1618
Ah okay so using the host
function for boot time refreshes the same cache. I'll close the other PR too then.
This PR adds a function to
process
andhost
to manually refresh the cached boot time value. This is very helpful in scenarios where you are reading all the processes on the system at a given time, want the performance gains of using the boot time cache, but want to update the cached boot time when you read all the processes.