github.com/polarismesh/polaris@v1.17.8/apiserver/xdsserverv3/resource/callback.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package resource
    19  
    20  import (
    21  	"context"
    22  
    23  	corev3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
    24  	discovery "github.com/envoyproxy/go-control-plane/envoy/service/discovery/v3"
    25  	"github.com/gogo/protobuf/jsonpb"
    26  	"google.golang.org/protobuf/proto"
    27  
    28  	commonlog "github.com/polarismesh/polaris/common/log"
    29  )
    30  
    31  func NewCallback(log *commonlog.Scope, nodeMgr *XDSNodeManager) *Callbacks {
    32  	return &Callbacks{
    33  		log:     log,
    34  		nodeMgr: nodeMgr,
    35  	}
    36  }
    37  
    38  type Callbacks struct {
    39  	log     *commonlog.Scope
    40  	nodeMgr *XDSNodeManager
    41  }
    42  
    43  func (cb *Callbacks) Report() {
    44  
    45  }
    46  
    47  func (cb *Callbacks) OnStreamOpen(_ context.Context, id int64, typ string) error {
    48  	if cb.log.DebugEnabled() {
    49  		cb.log.Debugf("stream %d open for %s", id, typ)
    50  	}
    51  	return nil
    52  }
    53  
    54  func (cb *Callbacks) OnStreamClosed(id int64, node *corev3.Node) {
    55  	if cb.log.DebugEnabled() {
    56  		cb.log.Debugf("stream %d closed", id)
    57  	}
    58  	cb.nodeMgr.DelNode(id)
    59  }
    60  
    61  func (cb *Callbacks) OnDeltaStreamOpen(_ context.Context, id int64, typ string) error {
    62  	if cb.log.DebugEnabled() {
    63  		cb.log.Debugf("delta stream %d open for %s", id, typ)
    64  	}
    65  	return nil
    66  }
    67  
    68  func (cb *Callbacks) OnDeltaStreamClosed(id int64, node *corev3.Node) {
    69  	if cb.log.DebugEnabled() {
    70  		cb.log.Debugf("delta stream %d closed", id)
    71  	}
    72  }
    73  
    74  func (cb *Callbacks) OnStreamRequest(id int64, req *discovery.DiscoveryRequest) error {
    75  	cb.nodeMgr.AddNodeIfAbsent(id, req.GetNode())
    76  	return nil
    77  }
    78  
    79  func (cb *Callbacks) OnStreamResponse(_ context.Context, id int64, req *discovery.DiscoveryRequest,
    80  	resp *discovery.DiscoveryResponse) {
    81  	if cb.log.DebugEnabled() {
    82  		cloneReq := proto.Clone(req).(*discovery.DiscoveryRequest)
    83  		cloneReq.Node = nil
    84  		marshaler := jsonpb.Marshaler{}
    85  		reqstr, _ := marshaler.MarshalToString(cloneReq)
    86  		respstr, _ := marshaler.MarshalToString(resp)
    87  		cb.log.Debugf("on stream %d type %s request %s response %s", id, req.TypeUrl, reqstr, respstr)
    88  	}
    89  }
    90  
    91  func (cb *Callbacks) OnStreamDeltaRequest(id int64, req *discovery.DeltaDiscoveryRequest) error {
    92  	cb.nodeMgr.AddNodeIfAbsent(id, req.GetNode())
    93  	return nil
    94  }
    95  
    96  func (cb *Callbacks) OnStreamDeltaResponse(id int64, req *discovery.DeltaDiscoveryRequest,
    97  	resp *discovery.DeltaDiscoveryResponse) {
    98  	if cb.log.DebugEnabled() {
    99  		cloneReq := proto.Clone(req).(*discovery.DeltaDiscoveryRequest)
   100  		cloneReq.Node = nil
   101  		marshaler := jsonpb.Marshaler{}
   102  		reqstr, _ := marshaler.MarshalToString(cloneReq)
   103  		respstr, _ := marshaler.MarshalToString(resp)
   104  		cb.log.Debugf("on delta stream %d type %s request %s response %s", id, req.TypeUrl, reqstr, respstr)
   105  	}
   106  }
   107  
   108  func (cb *Callbacks) OnFetchRequest(_ context.Context, req *discovery.DiscoveryRequest) error {
   109  	return nil
   110  }
   111  
   112  func (cb *Callbacks) OnFetchResponse(req *discovery.DiscoveryRequest, resp *discovery.DiscoveryResponse) {
   113  	if cb.log.DebugEnabled() {
   114  		cloneReq := proto.Clone(req).(*discovery.DiscoveryRequest)
   115  		cloneReq.Node = nil
   116  		marshaler := jsonpb.Marshaler{}
   117  		reqstr, _ := marshaler.MarshalToString(cloneReq)
   118  		respstr, _ := marshaler.MarshalToString(resp)
   119  		cb.log.Debugf("on fetch type %s request %s response %s", req.TypeUrl, reqstr, respstr)
   120  	}
   121  }