Live video on the AT Protocol
at eli/docs-url-fix 47 lines 1.0 kB view raw
1package boring 2 3import ( 4 "bytes" 5 "context" 6 "fmt" 7 "io" 8 "net/http" 9 10 "stream.place/streamplace/pkg/aqhttp" 11 "stream.place/streamplace/pkg/log" 12) 13 14// boring HTTP replication mechanism 15type BoringReplicator struct { 16 Peers []string 17} 18 19func (rep *BoringReplicator) NewSegment(ctx context.Context, bs []byte) { 20 for _, p := range rep.Peers { 21 go func(peer string) { 22 ctx := log.WithLogValues(ctx, "peer", peer) 23 err := sendSegment(ctx, peer, bs) 24 if err != nil { 25 log.Log(ctx, "error replicating segment", "error", err) 26 } 27 }(p) 28 } 29} 30 31func sendSegment(ctx context.Context, peer string, bs []byte) error { 32 r := bytes.NewReader(bs) 33 peerURL := fmt.Sprintf("%s/api/segment", peer) 34 req, err := http.NewRequestWithContext(ctx, "POST", peerURL, r) 35 if err != nil { 36 return err 37 } 38 res, err := aqhttp.Client.Do(req) 39 if err != nil { 40 return err 41 } 42 if res.StatusCode != 204 { 43 body, _ := io.ReadAll(res.Body) 44 return fmt.Errorf("unexpected http code %d body=%s", res.StatusCode, body) 45 } 46 return nil 47}