| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 3 | |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 4 | package memory |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "fmt" |
| 9 | "net" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | // NetworkStatus is example data that will be stored in a Value. |
| 14 | type NetworkStatus struct { |
| 15 | ExternalAddress net.IP |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 16 | DefaultGateway net.IP |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 17 | } |
| 18 | |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 19 | // NetworkService is a fake/example network service that is responsible for |
| 20 | // communicating the newest information about a machine's network configuration |
| 21 | // to consumers/watchers. |
| 22 | type NetworkService struct { |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 23 | Provider Value[NetworkStatus] |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | // Run pretends to execute the network service's main logic loop, in which it |
| 27 | // pretends to have received an IP address over DHCP, and communicates that to |
| 28 | // consumers/watchers. |
| 29 | func (s *NetworkService) Run(ctx context.Context) { |
| 30 | s.Provider.Set(NetworkStatus{ |
| 31 | ExternalAddress: nil, |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 32 | DefaultGateway: nil, |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 33 | }) |
| 34 | |
| 35 | select { |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 36 | case <-time.After(100 * time.Millisecond): |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 37 | case <-ctx.Done(): |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 38 | return |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | fmt.Printf("NS: Got DHCP Lease\n") |
| 42 | s.Provider.Set(NetworkStatus{ |
| 43 | ExternalAddress: net.ParseIP("203.0.113.24"), |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 44 | DefaultGateway: net.ParseIP("203.0.113.1"), |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 45 | }) |
| 46 | |
| 47 | select { |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 48 | case <-time.After(100 * time.Millisecond): |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 49 | case <-ctx.Done(): |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | fmt.Printf("NS: DHCP Address changed\n") |
| 54 | s.Provider.Set(NetworkStatus{ |
| 55 | ExternalAddress: net.ParseIP("203.0.113.103"), |
| Serge Bazanski | 68ca5ee | 2021-04-27 16:09:16 +0200 | [diff] [blame] | 56 | DefaultGateway: net.ParseIP("203.0.113.1"), |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 57 | }) |
| 58 | |
| 59 | time.Sleep(100 * time.Millisecond) |
| 60 | } |
| 61 | |
| 62 | // ExampleValue_full demonstrates a typical usecase for Event Values, in which |
| 63 | // a mock network service lets watchers know that the machine on which the code |
| 64 | // is running has received a new network configuration. |
| 65 | // It also shows the typical boilerplate required in order to wrap a Value (eg. |
| 66 | // MemoryValue) within a typesafe wrapper. |
| 67 | func ExampleValue_full() { |
| 68 | ctx, ctxC := context.WithCancel(context.Background()) |
| 69 | defer ctxC() |
| 70 | |
| 71 | // Create a fake NetworkService. |
| Tim Windelschmidt | 3b5a917 | 2024-05-23 13:33:52 +0200 | [diff] [blame] | 72 | var ns NetworkService |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 73 | |
| 74 | // Run an /etc/hosts updater. It will watch for updates from the NetworkService |
| 75 | // about the current IP address of the node. |
| 76 | go func() { |
| Serge Bazanski | 37110c3 | 2023-03-01 13:57:27 +0000 | [diff] [blame] | 77 | w := ns.Provider.Watch() |
| Serge Bazanski | c00318e | 2021-03-03 12:39:24 +0100 | [diff] [blame] | 78 | for { |
| 79 | status, err := w.Get(ctx) |
| 80 | if err != nil { |
| 81 | break |
| 82 | } |
| 83 | if status.ExternalAddress == nil { |
| 84 | continue |
| 85 | } |
| 86 | // Pretend to write /etc/hosts with the newest ExternalAddress. |
| 87 | // In production code, you would also check for whether ExternalAddress has |
| 88 | // changed from the last written value, if writing to /etc/hosts is expensive. |
| 89 | fmt.Printf("/etc/hosts: foo.example.com is now %s\n", status.ExternalAddress.String()) |
| 90 | } |
| 91 | }() |
| 92 | |
| 93 | // Run fake network service. |
| 94 | ns.Run(ctx) |
| 95 | |
| 96 | // Output: |
| 97 | // NS: Got DHCP Lease |
| 98 | // /etc/hosts: foo.example.com is now 203.0.113.24 |
| 99 | // NS: DHCP Address changed |
| 100 | // /etc/hosts: foo.example.com is now 203.0.113.103 |
| 101 | } |