github.com/matrixorigin/matrixone@v0.7.0/pkg/common/morpc/examples/stream/main.go (about)

     1  // Copyright 2021 - 2022 Matrix Origin
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"fmt"
    20  	"log"
    21  	"os"
    22  	"time"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/common/morpc"
    25  	"github.com/matrixorigin/matrixone/pkg/common/morpc/examples/message"
    26  )
    27  
    28  var (
    29  	addr = "unix:///tmp/pingpong.sock"
    30  	file = "/tmp/pingpong.sock"
    31  )
    32  
    33  func main() {
    34  	if err := os.RemoveAll(file); err != nil {
    35  		panic(err)
    36  	}
    37  
    38  	if err := startServer(); err != nil {
    39  		panic(err)
    40  	}
    41  
    42  	bf := morpc.NewGoettyBasedBackendFactory(newCodec())
    43  	cli, err := morpc.NewClient(bf, morpc.WithClientMaxBackendPerHost(1))
    44  	if err != nil {
    45  		panic(err)
    46  	}
    47  
    48  	st, err := cli.NewStream(addr, false)
    49  	if err != nil {
    50  		panic(err)
    51  	}
    52  
    53  	defer func() {
    54  		if err := st.Close(); err != nil {
    55  			panic(err)
    56  		}
    57  	}()
    58  
    59  	ctx, cancel := context.WithTimeout(context.TODO(), time.Second*10)
    60  	defer cancel()
    61  
    62  	if err := st.Send(ctx, &message.ExampleMessage{MsgID: st.ID(), Content: "first message"}); err != nil {
    63  		panic(err)
    64  	}
    65  
    66  	ch, err := st.Receive()
    67  	if err != nil {
    68  		panic(err)
    69  	}
    70  
    71  	for m := range ch {
    72  		if m == nil {
    73  			return
    74  		}
    75  		log.Printf("%s", m.DebugString())
    76  	}
    77  }
    78  
    79  func startServer() error {
    80  	s, err := morpc.NewRPCServer("test-unix-server", addr, newCodec())
    81  	if err != nil {
    82  		return err
    83  	}
    84  	s.RegisterRequestHandler(func(ctx context.Context, request morpc.Message, _ uint64, cs morpc.ClientSession) error {
    85  		// send more message back
    86  		go func() {
    87  			for i := 0; i < 10; i++ {
    88  				if err := cs.Write(ctx, &message.ExampleMessage{MsgID: request.GetID(), Content: fmt.Sprintf("stream-%d", i)}); err != nil {
    89  					panic(err)
    90  				}
    91  			}
    92  		}()
    93  		return nil
    94  	})
    95  
    96  	return s.Start()
    97  }
    98  
    99  func newCodec() morpc.Codec {
   100  	return morpc.NewMessageCodec(func() morpc.Message { return &message.ExampleMessage{} })
   101  }