github.com/google/fleetspeak@v0.1.15-0.20240426164851-4f31f62c1aea/fleetspeak/src/server/service/service.go (about)

     1  // Copyright 2017 Google Inc.
     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  //     https://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 service defines the interface that fleetspeak expects from its
    16  // service implementations.
    17  //
    18  // An installation can provide the set of ServiceFactories which it requires,
    19  // and then the factories will be used, according to the server configuration,
    20  // to create the server services.
    21  package service
    22  
    23  import (
    24  	"context"
    25  	"net"
    26  
    27  	"github.com/google/fleetspeak/fleetspeak/src/common"
    28  	"github.com/google/fleetspeak/fleetspeak/src/server/db"
    29  
    30  	fspb "github.com/google/fleetspeak/fleetspeak/src/common/proto/fleetspeak"
    31  	spb "github.com/google/fleetspeak/fleetspeak/src/server/proto/fleetspeak_server"
    32  )
    33  
    34  // A Service is a Fleetspeak module configured to send and processes messages on a
    35  // Fleetspeak server.
    36  type Service interface {
    37  	// Start tells the service to begin operations. Once started, the service may make
    38  	// calls into ServiceContext until Wait is called.
    39  	Start(sctx Context) error
    40  
    41  	// ProcessMessage requests that the service process a message. If it returns a
    42  	// net.Error which reports as a timeout or temporary error, the message will
    43  	// be retried according the ServerRetryPolicy. Otherwise the error will be
    44  	// assumed to be permanent.
    45  	ProcessMessage(context.Context, *fspb.Message) error
    46  
    47  	// Stop initiates and waits for an orderly shut down. It will only be
    48  	// called when there are no more active calls to ProcessMessage and
    49  	// should not return until all calls to ServiceContext are completed.
    50  	Stop() error
    51  }
    52  
    53  // Context allows a Fleetspeak Service to communicate back to the Fleetspeak system.
    54  type Context interface {
    55  	// Set sends a message to a client machine or other server component. It can be called
    56  	// anytime after Service.Start begins and before Service.Stop returns.
    57  	Send(context.Context, *fspb.Message) error
    58  
    59  	// GetClientData retrieves basic information about a client.
    60  	GetClientData(context.Context, common.ClientID) (*db.ClientData, error)
    61  }
    62  
    63  // A Factory is a function which creates a server service for the provided
    64  // configuration.
    65  type Factory func(*spb.ServiceConfig) (Service, error)
    66  
    67  // NOOPFactory is a services.Factory that creates a service which drops all messages.
    68  func NOOPFactory(conf *spb.ServiceConfig) (Service, error) {
    69  	return noopService{}, nil
    70  }
    71  
    72  type noopService struct{}
    73  
    74  func (s noopService) Start(sctx Context) error                                  { return nil }
    75  func (s noopService) ProcessMessage(ctx context.Context, m *fspb.Message) error { return nil }
    76  func (s noopService) Stop() error                                               { return nil }
    77  
    78  // TemporaryError wraps an error and indicates that ProcessMessage should be
    79  // retried some time in the future.
    80  type TemporaryError struct {
    81  	E error
    82  }
    83  
    84  // Error implements error by trivial delegation to E.
    85  func (e TemporaryError) Error() string {
    86  	return e.E.Error()
    87  }
    88  
    89  // Temporary implements net.Error.
    90  func (e TemporaryError) Temporary() bool {
    91  	return true
    92  }
    93  
    94  // Timeout implements net.Error.
    95  func (e TemporaryError) Timeout() bool {
    96  	return true
    97  }
    98  
    99  // IsTemporary determines if an error should be retried by the fleetspeak
   100  // system.
   101  func IsTemporary(err error) bool {
   102  	e, ok := err.(net.Error)
   103  	return ok && (e.Temporary() || e.Timeout())
   104  }