Исходники и презентации
This commit is contained in:
25
lessons/goroutines_and_scheduler/async_preemptible/main.go
Normal file
25
lessons/goroutines_and_scheduler/async_preemptible/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func infiniteLoop(str string) {
|
||||
for {
|
||||
log.Println(str)
|
||||
}
|
||||
}
|
||||
|
||||
func loop(str string) {
|
||||
for i := 0; i < 5; i++ {
|
||||
runtime.Gosched()
|
||||
log.Println(str)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(1)
|
||||
go infiniteLoop("infinite_loop")
|
||||
loop("loop")
|
||||
}
|
||||
19
lessons/goroutines_and_scheduler/endless_loop/main.go
Normal file
19
lessons/goroutines_and_scheduler/endless_loop/main.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
runtime.GOMAXPROCS(1)
|
||||
|
||||
var i int
|
||||
go func() {
|
||||
for {
|
||||
i++
|
||||
}
|
||||
}()
|
||||
|
||||
fmt.Println(i)
|
||||
}
|
||||
16
lessons/goroutines_and_scheduler/gmp_model/main.go
Normal file
16
lessons/goroutines_and_scheduler/gmp_model/main.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("GOMAXPROCS:", runtime.GOMAXPROCS(0))
|
||||
fmt.Println("CPU:", runtime.NumCPU())
|
||||
|
||||
runtime.GOMAXPROCS(16)
|
||||
|
||||
fmt.Println("GOMAXPROCS:", runtime.GOMAXPROCS(0))
|
||||
fmt.Println("CPU:", runtime.NumCPU())
|
||||
}
|
||||
16
lessons/goroutines_and_scheduler/goroutine_index/main.go
Normal file
16
lessons/goroutines_and_scheduler/goroutine_index/main.go
Normal file
@ -0,0 +1,16 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
for i := 0; i < 5; i++ {
|
||||
go func() {
|
||||
fmt.Print(i)
|
||||
}()
|
||||
}
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
}
|
||||
10
lessons/goroutines_and_scheduler/goroutines_number/main.go
Normal file
10
lessons/goroutines_and_scheduler/goroutines_number/main.go
Normal file
@ -0,0 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Printf("Goroutines: %d\n", runtime.NumGoroutine())
|
||||
}
|
||||
33
lessons/goroutines_and_scheduler/never_exit/main.go
Normal file
33
lessons/goroutines_and_scheduler/never_exit/main.go
Normal file
@ -0,0 +1,33 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
func task() {
|
||||
for {
|
||||
time.Sleep(time.Millisecond * 200)
|
||||
panic("unexpected situation")
|
||||
}
|
||||
}
|
||||
|
||||
func NeverExit(name string, action func()) {
|
||||
defer func() {
|
||||
if v := recover(); v != nil {
|
||||
log.Println(name, "is crashed - restarting...")
|
||||
go NeverExit(name, action)
|
||||
}
|
||||
}()
|
||||
|
||||
if action != nil {
|
||||
action()
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
go NeverExit("first_goroutine", task)
|
||||
go NeverExit("second_goroutine", task)
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
@ -0,0 +1,23 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func process() {
|
||||
defer func() {
|
||||
v := recover()
|
||||
fmt.Println("recovered:", v)
|
||||
}()
|
||||
|
||||
go func() {
|
||||
panic("error")
|
||||
}()
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
|
||||
func main() {
|
||||
process()
|
||||
}
|
||||
19
lessons/goroutines_and_scheduler/runtime_exit/main.go
Normal file
19
lessons/goroutines_and_scheduler/runtime_exit/main.go
Normal file
@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
go func() {
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println("tick")
|
||||
}
|
||||
}()
|
||||
|
||||
time.Sleep(3 * time.Second)
|
||||
runtime.Goexit()
|
||||
}
|
||||
@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
// nc 127.0.0.1 12345
|
||||
|
||||
func main() {
|
||||
listener, err := net.Listen("tcp", ":12345")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
go ClientHandler(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func ClientHandler(c net.Conn) {
|
||||
panic(errors.New("internal error"))
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
|
||||
// nc 127.0.0.1 12345
|
||||
|
||||
func main() {
|
||||
listener, err := net.Listen("tcp", ":12345")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
for {
|
||||
conn, err := listener.Accept()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
||||
go ClientHandler(conn)
|
||||
}
|
||||
}
|
||||
|
||||
func ClientHandler(c net.Conn) {
|
||||
defer func() {
|
||||
if v := recover(); v != nil {
|
||||
log.Println("captured panic:", v)
|
||||
}
|
||||
c.Close()
|
||||
}()
|
||||
|
||||
panic("internal error")
|
||||
}
|
||||
Reference in New Issue
Block a user